Variable changes to previously calculated value each iteration.
이전 댓글 표시
Here is the equation I am dealing with
p2 = p1./(exp((50)./(29.3.*((T)))));
T = 1:340
p1 = 99977
I need to calculate p2 for all T values and have p1 change to the previously calculate p2 value each iteration.
I wrote this:
for i = 1:length(T)
p2(i) = p1./(exp((50)./(29.3.*((T)))));
p1 = p2(i)
end
I keep getting an error "Unable to perform assignment because the left and right sides have a different number of elements."
Do I need an embedded for loop to do this and if so, how so?
채택된 답변
추가 답변 (1개)
Erivelton Gualter
2019년 5월 4일
0 개 추천
Since T is an array and you are using a for loop to find each value of p2(T), you should you T(i) instead of T.
Check the correct code:
clear all
T = 1:340;
p1 = 99977;
for i = 1:length(T)
p2(i) = p1/(exp(50/(29.3*T(i))));
p1 = p2(i);
end
plot(T, p2);
I am also plotting the P2 vs T for further verification.
댓글 수: 3
Adam Kevin Francis Baker
2019년 5월 4일
Erivelton Gualter
2019년 5월 4일
Hi Adam,
It does work as well.
Adam Kevin Francis Baker
2019년 5월 4일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!