Solving a linear equation using for loop and equationsToMatrix

Hi
I am trying to solve a linear equation whose number of variables are defined by the user
I do not know how to define a matrix for equations
(I know how to solve this with another method but I am trying to use equationsToMatrix)
Is my approach to define a matrix like eq(n) correct?
clc
clear all
%This program will solve 1D Heat Transfer through a wire
T_A=10; %input('please specify the temperature at left \n') %Bounndary condition
T_B= 40; %input('please specify the temperature at Right \n') %Boundary condition
k= 100; %input('please specify the Conductivity \n')
node=input('please specify the number of nodes \n')
L=5 %input('please specify the Length of the wire \n')
S=100; %input('Heat generation rate in the wire \n')
syms T [1 node];
d=L/node;
D=k/d;
A=0.1;
V=A*d;
for n= 1:node
switch n
case n==1
a_L=0;
a_R=D.*A;
S_p=-2.*D.*A;
S_u=T_A.*(2.*D.*A+(S.*V));
a_p=a_L+a_R-S_p;
eq(n)= (a_p.*T(n))-(a_L.*T_B)-(a_R.*T(n+1))==S_u;
case n==node
a_R=0;
a_L=D.*A;
S_p=-2.*D.*A;
S_u=T_B.*(2.*D.*A+(S.*V));
a_p=a_R+a_L-S_p;
eq(n)= (a_p.*T(n))-(a_R.*T_A)-(a_L.*T(n-1))==s_u;
otherwise
S_p=0;
S_u=S.*V;
a_R=A.*D;
a_L=A.*D;
a_p=a_L+a_R-S_p;
eq(n)= (T(n).*a_p)-((T(n-1)).*a_L)-((T(n+1)).*a_R)==S_u;
end
end
[Z,Y] = equationsToMatrix(eq , T)

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by