Data in array being overwritten when if statement is satisfied.
5 次查看(过去 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);
0 个评论
采纳的回答
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 ?
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!