Data in array being overwritten when if statement is satisfied.

2 次查看(过去 30 天)
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);

采纳的回答

Stephen23
Stephen23 2016-7-27
编辑:Stephen23 2016-7-27
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 个评论
Nathan Dick
Nathan Dick 2016-7-27
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by