why doesn't this matlab code work? HELP!!!

3 views (last 30 days)
My problem is that this code doesn't work and I need the graph. Help please.
clear all
clc;
es=0.007;%m
d=2800;%kg/m^3
k=180;%W/m K
E=0.8;
C=900;%J/kg K
Tamb=298;%K
As=0.04;%m^2
qh=12500; %W/m^2 K
h=10;%W/m^2 K
Tfp=408;%K
cteEB=5.67*10^-8;
V=0.00028;%m^3
to=0;
deltat=0.1;% seconds
t=[];
Tf=[];
while (Tfp>=Tf)
Tf(i+1)=(As/(d*C*V))*(qh-(h*(Tf(i)-Tamb))-(E*cteEB*(Tf(i)^4-Tamb^4)))*deltat+Tf(i);
t(i+1)=t(i)+deltat;
hold on
plot(Tf,t);
i=i+1;
end
xlabel ('Temperature');
ylabel ('Time [s]');
  2 Comments
Erika Abril
Erika Abril on 6 Mar 2021
clear all
clc;
es=0.007;%m
d=2800;%kg/m^3
k=180;%W/m K
E=0.8;
C=900;%J/kg K
Tamb=298;%K
As=0.04;%m^2
qh=12500; %W/m^2 K
h=10;%W/m^2 K
Tfp=408;%K
cteEB=5.67*10^-8;
V=0.00028;%m^3
to=0;
deltat=0.1;% seconds
t=[];
Tf=[];
while (Tfp>=Tf)
Tf(i+1)=(As/(d*C*V))*(qh-(h*(Tf(i)-Tamb))-(E*cteEB*(Tf(i)^4-Tamb^4)))*deltat+Tf(i);
t(i+1)=t(i)+deltat;
hold on
plot(Tf,t);
i=i+1;
end
xlabel ('Temperature');
ylabel ('Time [s]');

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Mar 2021
Edited: Walter Roberson on 7 Mar 2021
You did not initialize i so it will have its default value of sqrt(-1). Then inside the loop when you assign to Tf(i+1) that is a destination of 1+1i which is not a valid index.
For that code to work you need to initialize i to 1.
  1 Comment
Walter Roberson
Walter Roberson on 7 Mar 2021
Tf=[];
while (Tfp>=Tf)
You initialize Tf to empty. When you compare Tfp to empty, the result is false, so your while loop never executes.

Sign in to comment.

More Answers (1)

Basil C.
Basil C. on 6 Mar 2021
The error could be in the while loop. Try using the following.
while(Tfp>=Tf(end))
If you could share the error, it would be more helpful
  2 Comments
Erika Abril
Erika Abril on 6 Mar 2021
The problem is the graph, the code doesn't work :(

Sign in to comment.

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!