Loop never stops?
1 回表示 (過去 30 日間)
古いコメントを表示
Why does my loop never stops after I assign k1 & k2 as symbolic variables?
n=10;
T=sym(zeros(n,n));
T(1,:) = 410; % Top
T(end,:) = 420; % Bottom
T(:,1) = 400; % Left
T(:,end) = 400; % Right
T_old = T;
for k = 1:20 %time steps
for j = 2: (n-1)
for i = 2: (n-1)
%k1 = 0.02; k2=0.33;
syms k1 k2
T(i,j) = T_old(i,j)*(1-2*k1-2*k2)+k1*(T_old(i-1,j)+T_old(i+1,j))+k2*(T_old(i,j-1)+T_old(i,j+1));
end
end
T_old = T;
end
2 件のコメント
KSSV
2021 年 6 月 16 日
This line
syms k1 k2
need not to be loop, keep it at the begining. Are you sure you want k1, k2 to be symbolic?
採用された回答
Walter Roberson
2021 年 6 月 16 日
The code does finish. It just takes a long time. You are building some complicated expressions. You can improve performance by putting in an expand() call:
On my system, execution time is about 22 seconds, but on the demonstration system here it is over 55 seconds so I cannot show you the result.
tic
n = 10;
T=sym(zeros(n,n));
T(1,:) = 410; % Top
T(end,:) = 420; % Bottom
T(:,1) = 400; % Left
T(:,end) = 400; % Right
T_old = T;
syms k1 k2
for k = 1:20 %time steps
for j = 2: (n-1)
for i = 2: (n-1)
T(i,j) = expand(T_old(i,j)*(1-2*k1-2*k2)+k1*(T_old(i-1,j)+T_old(i+1,j))+k2*(T_old(i,j-1)+T_old(i,j+1)));
end
end
T_old = T;
end
toc
T(1:5,1:5)
Tn = subs(T, {k1, k2}, {0.02, 0.33});
Tn(1:5,1:5)
vpa(Tn,5)
2 件のコメント
Walter Roberson
2021 年 6 月 16 日
Numeric results look like
[400.0, 410.0, 410.0, 410.0, 410.0, 410.0, 410.0, 410.0, 410.0, 400.0]
[400.0, 346.97, 300.07, 265.17, 246.58, 246.58, 265.17, 300.07, 346.97, 400.0]
[400.0, 327.52, 263.81, 216.56, 191.43, 191.43, 216.56, 263.81, 327.52, 400.0]
[400.0, 323.92, 257.06, 207.47, 181.11, 181.11, 207.47, 257.06, 323.92, 400.0]
[400.0, 323.48, 256.23, 206.35, 179.84, 179.84, 206.35, 256.23, 323.48, 400.0]
[400.0, 323.48, 256.23, 206.36, 179.84, 179.84, 206.36, 256.23, 323.48, 400.0]
[400.0, 323.94, 257.09, 207.51, 181.15, 181.15, 207.51, 257.09, 323.94, 400.0]
[400.0, 327.67, 264.07, 216.89, 191.8, 191.8, 216.89, 264.07, 327.67, 400.0]
[400.0, 348.09, 301.89, 267.39, 248.99, 248.99, 267.39, 301.89, 348.09, 400.0]
[400.0, 420.0, 420.0, 420.0, 420.0, 420.0, 420.0, 420.0, 420.0, 400.0]
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!