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

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 Comments

Where is B? Where you want to supply?
Thank you for replying!
% Constants of B
% k1 = 0.04;
% k2 = 0.67;
My B constants are also k1 and k2.
I want to supply into this function in my for loop.
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));
But you are already doing it. Your question is not clear.
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.

Sign in to comment.

 Accepted Answer

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 Comments

Yes, my approach is to define k1 and k2 as symbolic variables and subtitute different values of k1&k2 later on. I will proceed with your method and get back to you soon. Thank you!
I've transformed both of my constants of A and of B into Matrix so that at different position will have different values of k1 and k2. I changed my n to 20 but my final Tn is of size 400x400?
tic
n = 20;
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)
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
% Constant A
c1 = 0.02;
c2 = 0.33;
% ConstantB at (x<=a & y<=b) & (x>=L-d & y<=c)
c3 = 0.04;
c4 = 0.67;
% Dimension A
L = 20;
H = 5;
% Dimension B
a = 1;
b = 3;
c = 1;
d = 3;
% Matrix for k1
k1 = c1*ones(n,n); %constant A (c1)
k1(ceil(n*b/H : n) , ceil(1 : n*a/L)) = c3; %(x<=a & y<=b)
k1(ceil(n*(H-c)/H : n) , ceil(n*(L-d)/L : n)) = c3; %(x>=L-d & y<=c)
% Matrix for k2
k2 = c2*ones(n,n); %constant A (c2)
k2(ceil(n*b/H : n) , ceil(1 : n*a/L)) = c4; %(x<=a & y<=b)
k2(ceil(n*(H-c)/H) : n , ceil(n*(L-d)/L : n)) = c4; %(x>=L-d & y<=c)
Tn = subs(T);
vpa(Tn,5)
H is not defined here, and you forgot to
syms k1 k2
Also
k1 = c1*ones(n,n); %constant A (c1)
k2 = c2*ones(n,n); %constant A (c2)
those are each 20 x 20
Tn = subs(T);
T is 20 x 20, and involves symbolic scalar variables k1 and k2.
When you subs() an array (k1, k2 are 20 x 20 each) in place of symbolic variables, then in every array element in T, the 20 x 20 numeric arrays k1 and k2 will be substituted for the symbolic variables k1, k2. So what was T(3,7) for example that involved symbolic scalars k1 and k2, will become a 20 x 20 array. But the same is true for all 400 (20 x 200) elements of T, so you end up with an array that is 400 x 400.
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.
Instead of getting a 400 x 400 array, with a little work you could get a 20 x 20 x 20 x 20 array -- like
T4(:,:,K,L) = subs(T, {sym('k1'), sym('k2')}, {k1(K,L), k2(K,L)})
However, if what you wanted to do was have
Tn(I,J) = subs(T(I,J), {sym('k1'), sym('k2')}, {k1(K,L), k2(K,L)})
which is to say that you want to substitute in corresponding k1, k2, then you need to do a bit more work. Take subsets of T and subs() scalar values into the subsets.
%do not construct numeric k1, k2 matrices.
rb1 = ceil(n*b/H);
cb1 = floor(n*a/L);
rb2 = ceil(N*(H-c)/H);
cb2 = ceil(N*(L-d)/L);
Tn = subs(T, {k1,k2}, {c1,c2});
Tn(rb1:n, cb1:n) = subs(T(rb1:n, cb1:n), {k1,k2}, {c3, c4});
Tn(rb2:n, cb2:n) = subs(T(rb2:n, cb2:n), {k1,k2}, {c3, c4});
Finally, i got everything to work! I've spent 1 week on this. Thank you so much!
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!

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Tags

Community Treasure Hunt

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

Start Hunting!