MATLAB Answers

How do I store values from a while loop and plot the values in fonction of the number of iteration?

7 views (last 30 days)
Sarah Mignon
Sarah Mignon on 15 Oct 2020
Commented: Sarah Mignon on 15 Oct 2020
Hello!
In the loop while I would like to store the values of each loop, and plot it with the corresponding iteration t.
m_cafe=0.2; %kg
m_cream=0.05; %kg
Cp_water=4179,6;
T_cafe=5+273.15; %K
T_cream=90+273.15;
m_mix=m_cafe+m_cream;
Tm=(m_cafe*Cp_water*T_cafe+m_cream*Cp_water*T_cream)/(m_mix);
U=20; %W/m2.K
Tamb=20+273.15; %K
d=0.05; %m
r=d/2; %m
Vol_cafe=m_cafe*0.001 % 1 kg=1 liter= 0.001m3
Vol_mix=m_mix*0.001 %
h_cafe=Vol_cafe/(3.14*r^2)
h_mix=Vol_mix/(3.14*r^2)
Air_cafe=3.14*d*h_cafe
Air_mix=3.14*d*h_mix
step=0.5;
e=1
t=0
while e>=0.01
y=(Tm-Tamb)*exp(((-Air_mix*U)/(m_mix*Cp_water))*t)+Tamb;
Temp(t)=y;
e=abs(Temp-Tamb);
t=t+step
end
plot (t,Temp,'r')

  1 Comment

Rik
Rik on 15 Oct 2020
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

Sign in to comment.

Accepted Answer

Rik
Rik on 15 Oct 2020
Indices in Matlab need to be positive integers. You should create a separate time vector.
And don't forget to index Temp, otherwise your condition will be a vector.

  3 Comments

Rik
Rik on 15 Oct 2020
Comment posted as answer by Sarah Mignon:
e=1
t=1
while e>=0.01
y=(Tm-Tamb)*exp(((-Air_mix*U)/(m_mix*Cp_water))*t)+Tamb;
Temp(t)=y;
e=abs(Temp(t)-Tamb);
t=t+step
Time(t)=t
end
plot (t,Temp,'r')
Thank you for your answer. Is it what you meant? I have tried to set t=1 to have a positive integers but it still gives me the same errror "Array indices must be positive integers or logical values." Is it the right way to store values in a vector?Is there a way to use "Append" in matlab? Also How can I have a graph starting from t=0 then with the corresponding values of Temp?
Rik
Rik on 15 Oct 2020
You need to use a separate index, which will increment only by integer values:
t=0;
Temp=0;
n=1;
e=1;
while e>=0.01
n=n+1;
Temp(n)=___
t(n)=t(n-1)+step;
end
plot(t,Temp)

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by