Variable changes to previously calculated value each iteration.
Show older comments
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?
Accepted Answer
More Answers (1)
Erivelton Gualter
on 4 May 2019
0 votes
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 Comments
Adam Kevin Francis Baker
on 4 May 2019
Erivelton Gualter
on 4 May 2019
Hi Adam,
It does work as well.
Adam Kevin Francis Baker
on 4 May 2019
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!