Using a FOR loop to iterate simultaneous equations
이전 댓글 표시
I find myself using MATLAB to solve simultaneous equations quite often, with up to 6 variables.
I've written a simple program using SYMS/SOL for equations with each number of variables, up to 6.
Long story short, I am trying to make a catch-all program for use by others that will:
1.) Accept an input of "number of variables in the system", say n, and initialize a variable for the current iteration, say e
2.) Iterate a FOR loop, from 1:n, that will:
a.) request the e-th equation to be entered by the user, in terms of a, b, c, ..., and store it as eqn(value of e)
b.) use SOL to solve the system
c.) output the solutions as a usual SYMS/SOL command would, in a format that is easy to understand
For some reason, I am struggling intensely with getting this to work. It does not help that I'm not super versed in outputting variables as text (a la FPRINTF).
I have attached my initial script below; this was the 6-variable version.
pause(1);
syms a b c d f g
eqn1=input('Please enter the first equation (terms of a,b,c,d,f,g): ');
eqn2=input('Please enter the second equation (terms of a,b,c,d,f,g): ');
eqn3=input('Please enter the third equation (terms of a,b,c,d,f,g): ');
eqn4=input('Please enter the fourth equation (terms of a,b,c,d,f,g): ');
eqn5=input('Please enter the fifth equation (terms of a,b,c,d,f,g): ');
eqn6=input('Please enter the sixth equation (terms of a,b,c,d,f,g): ');
sol=solve([eqn1,eqn2,eqn3,eqn4,eqn5,eqn6], [a,b,c,d,f,g]);
aSol=sol.a
bSol=sol.b
cSol=sol.c
dSol=sol.d
fSol=sol.f
gSol=sol.g
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!