How to save the results of all iterations?
14 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
David Fletcher
2021-4-5
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 个评论
David Fletcher
2021-4-5
编辑:David Fletcher
2021-4-5
You sure? When I run my amended version I get 5000 values for T
更多回答(1 个)
Walter Roberson
2021-4-5
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!