How do I supply different constants into a function of a loop?

1 回表示 (過去 30 日間)
Amanda Liu
Amanda Liu 2021 年 6 月 16 日
編集済み: Amanda Liu 2021 年 6 月 16 日
My code is currently supplying 2 constants of A only. How do I supply another 2 constants of B into the same function?
n=30;
% Initialization
T=zeros(n,n);
T(1,:) = 410; % Top
T(end,:) = 420; % Bottom
T(:,1) = 400; % Left
T(:,end) = 400; % Right
T_old = T;
% Constants of A
k1 = 0.02;
k2 = 0.33;
% Constants of B
% k1 = 0.04;
% k2 = 0.67;
for k = 1:20 %time steps
for j = 2: (n-1)
for i = 2: (n-1)
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
  4 件のコメント
KSSV
KSSV 2021 年 6 月 16 日
But you are already doing it. Your question is not clear.
Amanda Liu
Amanda Liu 2021 年 6 月 16 日
編集済み: Amanda Liu 2021 年 6 月 16 日
I'm so sorry that I'm not good in English and I didn't explain it clearly enough.
What I meant was I have 2 values of k1 and 2 values of k2. But the function in my loop only takes 1 value of k1 and 1 value of k2 from A.
There are 2 values of k1 and of k2 due to different x and y intervals.

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

採用された回答

Walter Roberson
Walter Roberson 2021 年 6 月 16 日
Start with the code I posted in response to another one of your Questions, https://www.mathworks.com/matlabcentral/answers/857275-loop-never-stops#answer_725790
and subs() different k1, k2 values as desired.
If you were careful enough about how you proceeded, you could create 2D array of combinations of k1, k2 values, move those into the 3rd and 4th dimension, and subs() once, to get a single 4D array.... though visualizing the result could be tricky!
  5 件のコメント
Amanda Liu
Amanda Liu 2021 年 6 月 16 日
編集済み: Amanda Liu 2021 年 6 月 16 日
Finally, i got everything to work! I've spent 1 week on this. Thank you so much!
Amanda Liu
Amanda Liu 2021 年 6 月 16 日
I've also tried the numeric matrices approach by turning the scalar k into vector k(i,j). It is much faster compared with symbolic method. But anyway, thank you!

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

その他の回答 (0 件)

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by