How to solve a system of two linear equations in matrix form under a loop /
1 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1080705/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1080710/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1080715/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1080720/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1080725/image.jpeg)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1080730/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1080735/image.png)
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 个评论
回答(1 个)
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 个评论
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!