How to solve a system of two linear equations in matrix form under a loop /

1 回表示 (過去 30 日間)
Adnan Hayat
Adnan Hayat 2022 年 7 月 28 日
コメント済み: Jon 2022 年 8 月 2 日
Suppose that I have two coordinates, time and space , and two dependent variables c and T such that and . Now I have a system of two coupled equations which are given in matrices form as
I define the t and x vectors in the code as follows. How to calculate the matrix X inside the loops given below. Initial data is available, i.e. and are given.
for j=1:50
t(j)=j*0.05; % t vector
end
for i=1:50
x(i)=i*0.05; % space vector
end
%___________________________ running the loop_______________________________________
for j=1:50
for i=1:50
%???????? How to calculate the matric X here ????????????????????
end
end
  2 件のコメント
Jon
Jon 2022 年 7 月 28 日
Is this a typo?
x(i)=i*0.05; % space vector
Did you mean
c(i)=i*0.05; % space vector
Adnan Hayat
Adnan Hayat 2022 年 7 月 29 日
No, it is not a typo. Actually i define x and t vectors like that. YOu can ignore that and take x=1,2,3,...,50 and t=1,2,3,...,50

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

回答 (1 件)

Jon
Jon 2022 年 7 月 28 日
編集済み: Jon 2022 年 7 月 28 日
I'll assume you know how to calculate A and B based upon your c and T values. Then to solve for X use
X = M\(J*A + B)
Note that using the \ operator is more numerically robust and efficient than actually computing the matrix inverse of M
  2 件のコメント
Adnan Hayat
Adnan Hayat 2022 年 7 月 29 日
The problem is how to store the value of M\(J*A + B) in X?
Jon
Jon 2022 年 8 月 2 日
You could store the results in a 3 dimensional array, so
X = zeros(2,50,50); % preallocate
for i = 1:50
for j = 1:50
.
.
.
X(:,i,j) = M\(J*A + B)
end
end

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by