필터 지우기
필터 지우기

Data in array being overwritten when if statement is satisfied.

조회 수: 2 (최근 30일)
Nathan Dick
Nathan Dick 2016년 7월 27일
댓글: Stephen23 2016년 7월 27일
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