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!