Borrar filtros
Borrar filtros

Data in array being overwritten when if statement is satisfied.

2 visualizaciones (últimos 30 días)
The first part of my array Cur2 is populated with 0 as I would expect from the following IF statement. When the IF statement is satisfied and the equation in the ELSE statement is used the first half of the array is overwritten with the value that would be calculated by the eq'n.
clearvars
L=80*10^3; x1=0:1000:1.5*10^5; U= 0.2; t1=x1./U;
Cs= 9; C0= 8;
M0 = 0.75*10^6;
M1= M0;
Q= 50*10^3;
Kr=0.4/24/60/60; Kd=Kr; Ka=0.8/24/60/60;
for n = 1:151
Cur1(1:n) = Kd*M0*(exp(-t1(1:n)*Kr)-exp(-t1(1:n)*Ka))/(Q*(Ka-Kr));
if (n<= 80)
Cur2(1:n) = 0;
else
Cur2(1:n) = Kd*M1*(exp(-((x1(1:n)-L)/U)*Kr)-exp(-((x1(1:n)-L)/U)*Ka))/(Q*(Ka-Kr));
end
C(1:n) = Cs - Cur1(1:n) - (Cs- C0)*exp(-t1(1:n)*Ka)-Cur2(1:n);
end
plot(x1,C);

Respuesta aceptada

Stephen23
Stephen23 el 27 de Jul. de 2016
Editada: Stephen23 el 27 de Jul. de 2016
Your data is being "overwritten" because that is what you wrote your code to do:
Cur2(1:n) = 0;
puts zeros in elements 1 to n. The your else puts something else in elements 1 to n:
Cur2(1:n) = Kd*M1*(...)
What do you want your code to do ?
  4 comentarios
Nathan Dick
Nathan Dick el 27 de Jul. de 2016
Thanks again for the reply. That is exactly what I meant. I got the syntax confused as I though by using(1:n) I was simply saying that it was a 1D array. For uni I have done some VB but this current course requires MATLAB so it is a learning experience.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by