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)

2 commentaires

What error message do you get ?
Mohammad Assar
Mohammad Assar le 26 Mai 2021
Modifié(e) : Mohammad Assar le 26 Mai 2021
The error was "Index exceeds the number of array elements" at line 47 (the last equation that I defined after "otherwise")

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by