How can I put the results from a while loop in a matrix?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Kaicheng Pan
am 14 Mai 2020
Kommentiert: Kaicheng Pan
am 14 Mai 2020
So basically I am solving an equation using trial and error. Here's the code
clear;clc;
syms x y
a1=13.7819; b1=2726.81; c1=217.512;
a2=13.9726; b2=3259.93; c2=212.3;
for x=0:10
t=70;
psat1=exp(a1-b1/(t+c1));
psat2=exp(a2-b2/(t+c2));
p=0.1*x*psat1+(1-0.1*x)*psat2;
while abs(p-90)>5
t=t+1;
psat1=exp(a1-b1/(t+c1));
psat2=exp(a2-b2/(t+c2));
p=0.1*x*psat1+(1-0.1*x)*psat2;
end
t(x+1)=t
end
I want to put the t from each for loop into one matrix, but Matlab spits out this:
t=131
t=119 119
t=111 0 111
t=103 0 0 103 and so on
I don't know why! Please help
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 14 Mai 2020
Kaicheng - you probably can remove the line
syms x y
since I can't see the y being used in the code, and the x is overridden by the loop step variable. A problem is that you are treating t as if it were an array (at one line of code) and then as a scalar (at another line of code)
for x=0:10
t=70; % <---- here t is a scalar and is being reset on each iteration
psat1=exp(a1-b1/(t+c1));
psat2=exp(a2-b2/(t+c2));
p=0.1*x*psat1+(1-0.1*x)*psat2;
while abs(p-90)>5
t=t+1;
psat1=exp(a1-b1/(t+c1));
psat2=exp(a2-b2/(t+c2));
p=0.1*x*psat1+(1-0.1*x)*psat2;
end
t(x+1)=t % <---- here t is an array
end
Try creating an array for your t values:
tArray = zeros(11,1);
for x=0:10
t=70;
psat1=exp(a1-b1/(t+c1));
psat2=exp(a2-b2/(t+c2));
p=0.1*x*psat1+(1-0.1*x)*psat2;
while abs(p-90)>5
t=t+1;
psat1=exp(a1-b1/(t+c1));
psat2=exp(a2-b2/(t+c2));
p=0.1*x*psat1+(1-0.1*x)*psat2;
end
tArray(x+1)=t;
end
I'm assuming that you mean to reset t to 70 on each iteration of the loop. Is this correct?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!