Coding an equation (with summation within equation) with nested for Loops

Hello,
I am attempting to code the following equation in matlab using three for loops:
However, my initial values are incorrect and I believe my issue is related to how I am handling the the summation inside the equation. I would appreciate any suggestions on how to alter my for loop.
for i=1:length(x)
for j=1:length(Temp(:,1))
for n=1:20
T_xt(i,j,n)= T_amb2 + (q_p./k).*(((exp(m.*(x(i)-2.*L))+...
exp(-m.*x(i)))/(m.*(1-exp(-2.*m.*L))))-...
((2.*exp(-m^2.*a.*t(j)))*(1./(2.*m.^2*L)+...
(L.*((cos((n.*pi.*x(i))./L).*...
exp((-n.^2.*pi.^2.*a.*t(j))./L.^2))./(m.^2.*L.^2 + n.^2*pi^2))))));
end
end
end

 採用された回答

Walter Roberson
Walter Roberson 2020 年 11 月 20 日

0 投票

You are probably summing T_xt(:,:,:) over the third dimension to get T(x,t) . However, when you do that, you would be adding T_amb2 once for each n value, which is not correct.
You can meaningfully create T_xt(i,j,n) but it should only be the cos*exp/sum term. Then you would sum those along the third dimension, multiply by 2*exp(-m^2*alpha*t), subtract from the [] and so on.

1 件のコメント

AbeSal
AbeSal 2020 年 11 月 21 日
Thank you for the suggestion. After updating my code my plot looked better but there was an issue at t=0. Are there additionally changes you might suggest I make to the code?
S(1)=0;
for i=1:length(x)
for j=1:length(Temp(:,1))
T_xt(i,j)= T_amb + (q_p./k).*(((exp(m.*(x(i)-2.*L))+...
exp(-m.*x(i)))/(m.*(1-exp(-2.*m.*L))))-...
((2.*exp(-m^2.*a.*t(j)))*(1./(2.*m.^2*L)+...
(L.*S(end)))));
for n = 1:20
S(n+1) = S(n)+(cos((n.*pi.*x(i))./L).*...
exp((-n.^2.*pi.^2.*a.*t(j))./L.^2))./(m.^2*L.^2 + n.^2.*pi.^2);
end
end
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 11 月 20 日

コメント済み:

2020 年 11 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by