Solving a linear equation using for loop and equationsToMatrix

조회 수: 9 (최근 30일)
Mohammad Assar
Mohammad Assar 2021년 5월 26일
편집: Mohammad Assar 2021년 5월 26일
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)
  댓글 수: 2
Torsten
Torsten 2021년 5월 26일
What error message do you get ?
Mohammad Assar
Mohammad Assar 2021년 5월 26일
편집: Mohammad Assar 2021년 5월 26일
The error was "Index exceeds the number of array elements" at line 47 (the last equation that I defined after "otherwise")

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by