Using a FOR loop to iterate simultaneous equations
14 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Manikanta Aditya
on 6 Mar 2024
Moved: Torsten
on 6 Mar 2024
Hey,
To create a MATLAB script that can handle a variable number of equations and unknowns.
Here’s a modified version of your script that should do what you want:
% Ask for the number of variables
n = input('Enter the number of variables: ');
% Initialize symbolic variables
syms_vars = sym('x', [1 n]);
% Initialize equations
eqns = sym(zeros(1, n));
% Loop over each equation
for i = 1:n
eqn = input(sprintf('Please enter equation %d in terms of x1, x2, ..., x%d: ', i, n), 's');
eqns(i) = str2sym(eqn);
end
% Solve the system
sol = solve(eqns, syms_vars);
% Display the solutions
for i = 1:n
fprintf('x%d = %s\n', i, sol.(sprintf('x%d', i)));
end
This script first asks for the number of variables. It then initializes the symbolic variables and equations. It loops over each equation, asking the user to input it. After all equations are entered, it solves the system and displays the solutions.
Please replace x1, x2, ..., xn with your actual variable names when entering the equations. For example, if n=3, you should enter your equation in terms of x1, x2, x3.
Thanks!
5 Comments
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!