How to save the results of all iterations?

36 views (last 30 days)
I have an equation with a summation term in it, so i used for loop to evaluate the summation term. however, i only get the final result of the for loop. I have tried pre-allocatation but still did not work. I am not sure if the mistake is in the for loop or somewhere else in my code. could you please take a look and advice my how to tweek my code? Thank you
R=1.1/100; %m
k=1; %W/m.K
rho=1100; %kg.m^3
Cp=4180; %J/kg.K
h=2700; %W.m^2.K
alpha=k/(rho*Cp);
tmax=5000;
Tb=120;
Ti=5;
T=zeros(tmax,1);
for t=1:tmax;
Y=zeros(20,1);
for n=1:20;
X=(-1).^n.*exp(-alpha.*n.^2.*pi^2.*t/R^2);
end
Y=sum(X)
T=Tb+2*(Tb-Ti).*Y;
end
plot(t,T);
hold on

Accepted Answer

David Fletcher
David Fletcher on 5 Apr 2021
Something like this?
R=1.1/100; %m
k=1; %W/m.K
rho=1100; %kg.m^3
Cp=4180; %J/kg.K
h=2700; %W.m^2.K
alpha=k/(rho*Cp);
tmax=5000;
Tb=120;
Ti=5;
T=zeros(tmax,1);
for t=1:tmax;
Y=zeros(20,1);
for n=1:20;
X(n)=(-1).^n.*exp(-alpha.*n.^2.*pi^2.*t/R^2);
end
Y=sum(X)
T(t)=Tb+2*(Tb-Ti).*Y;
end
plot(1:tmax,T);
hold on
  3 Comments
David Fletcher
David Fletcher on 5 Apr 2021
Edited: David Fletcher on 5 Apr 2021
You sure? When I run my amended version I get 5000 values for T
Alaa H
Alaa H on 5 Apr 2021
it totally worked now!! I forgot to change the T to T(t) the 1st time I ran the code for some reason, but now I ran it again with all the modifications you made and it worked just fine. Thank you so much!!!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 5 Apr 2021
T=zeros(tmax,1);
Okay, good so far
for t=1:tmax;
Each time you iterate the for loop, t will be a scalar containing one of those values. You declared T to be tmax rows, same as the maximum value for t, so you are on track.
T=Tb+2*(Tb-Ti).*Y;
Each iteration of the for loop, you are overwriting all of T. So after the for loop finishes, T is going to contain a single value that is the one calculated in the last iteration of the loop.
Y=zeros(20,1);
for n=1:20;
That is setting up to write into 20 different rows for Y, which is fine so far.
X=(-1).^n.*exp(-alpha.*n.^2.*pi^2.*t/R^2);
end
Each iteration of the n loop, you overwrite all of X, and you do not write into Y at all.
Y=sum(X)
but X is a scalar... and what is the point of having initialized Y if you are going to overwrite it with sum(X) ?
plot(t,T);
As described above, at each iteration of the for t loop, t will be a scalar. At the end of the loop, t will have the last value assigned to it, which would be tmax, and will be a scalar.
As explored above, you are overwriting T with a scalar for each iteration of the loop, so T is going to end up a scalar.
Hint:
for K = 1 : 5
x(K) = K.^2;
end
Also
X=(-1).^n.*exp(-alpha.*n.^2.*pi^2.*t/R^2);
with n = 20 and t = 5000 the exp() term will be exp(-35479) approximately, which is far far too small to bother with.
You can demonstrate that with those coefficients and the maximum possible n, that there is no point doing t > 100 -- that beyond that you would underflow to 0.
Are you certain you want to proceed at integer t up to 5000? It would be more common to have a factor to convert that to a shorter time scale, such as t/1000 for bound of 5 seconds. Time 5000 seconds for an exponential process is not going to have anything useful left of the equation by 5000.
  1 Comment
Alaa H
Alaa H on 5 Apr 2021
Edited: Alaa H on 5 Apr 2021
Thank you for your respnse. I get now that I have to write X and T as functions of n and t respectivly. you are right about the n and t values, they were just initial guesses since i did not know the approximate time at which T conveges to Tb eventually, I guess there are some equations i can use to get an estimates of the time but i just went with trial and error process. Thank you for taking the time to review the code!

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!