Solve System of Algebraic Equations
This topic shows you how to solve a system of equations symbolically using Symbolic Math Toolbox™. This toolbox offers both numeric and symbolic equation solvers. For a comparison of numeric and symbolic solvers, see Select Numeric or Symbolic Solver.
Handle the Output of
Suppose you have the system
and you want to solve for and . First, create the necessary symbolic objects.
syms x y a
There are several ways to address the output of
solve. One way is to use a two-output call. The call returns the following.
[solx,soly] = solve(x^2*y^2 == 0, x-y/2 == a)
Modify the first equation to . The new system has more solutions. Four distinct solutions are produced.
[solx,soly] = solve(x^2*y^2 == 1, x-y/2 == a)
Since you did not specify the dependent variables,
symvar to determine the variables.
This way of assigning output from
solve is quite successful for “small” systems. For instance, if you have a 10-by-10 system of equations, typing the following is both awkward and time consuming.
[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] = solve(...)
To circumvent this difficulty,
solve can return a structure whose fields are the solutions. For example, solve the system of equations
u^2 - v^2 = a^2,
u + v = 1,
a^2 - 2*a = 3. The solver returns its results enclosed in a structure.
syms u v a S = solve(u^2 - v^2 == a^2, u + v == 1, a^2 - 2*a == 3)
S = struct with fields: a: [2x1 sym] u: [2x1 sym] v: [2x1 sym]
The solutions for
a reside in the “
Similar comments apply to the solutions for
v. The structure
S can now be manipulated by the field and index to access a particular portion of the solution. For example, to examine the second solution, you can use the following statement to extract the second component of each field.
s2 = [S.a(2),S.u(2),S.v(2)]
The following statement creates the solution matrix
M whose rows comprise the distinct solutions of the system.
M = [S.a,S.u,S.v]
soly for further use.
clear solx soly
Solve a Linear System of Equations
Linear systems of equations can also be solved using matrix division. For example, solve this system.
clear u v x y syms u v x y eqns = [x + 2*y == u, 4*x + 5*y == v]; S = solve(eqns); sol = [S.x;S.y]
[A,b] = equationsToMatrix(eqns,x,y); z = A\b
z produce the same solution, although the results are assigned to different variables.
Return the Full Solution of a System of Equations
solve does not automatically return all solutions of an equation. To return all solutions along with the parameters in the solution and the conditions on the solution, set the
ReturnConditions option to
Consider the following system of equations:
Visualize the system of equations using
fimplicit. To set the x-axis and y-axis values in terms of
pi, get the axes handles using
a. Create the symbolic array
S of the values
2*pi at intervals of
pi/2. To set the ticks to
S, use the
YTick properties of
a. To set the labels for the x-and y-axes, convert
S to character vectors. Use
arrayfun to apply
char to every element of
S to return
T. Set the
YTickLabel properties of
syms x y eqn1 = sin(x)+cos(y) == 4/5; eqn2 = sin(x)*cos(y) == 1/10; a = axes; fimplicit(eqn1,[-2*pi 2*pi],'b'); hold on grid on fimplicit(eqn2,[-2*pi 2*pi],'m'); L = sym(-2*pi:pi/2:2*pi); a.XTick = double(L); a.YTick = double(L); M = arrayfun(@char, L, 'UniformOutput', false); a.XTickLabel = M; a.YTickLabel = M; title('Plot of System of Equations') legend('sin(x)+cos(y) == 4/5','sin(x)*cos(y) == 1/10',... 'Location','best','AutoUpdate','off')
The solutions lie at the intersection of the two plots. This shows the system has repeated, periodic solutions. To solve this system of equations for the full solution set, use
solve and set the
ReturnConditions option to
S = solve(eqn1,eqn2,'ReturnConditions',true)
S = struct with fields: x: [2x1 sym] y: [2x1 sym] parameters: [z z1] conditions: [2x1 sym]
solve returns a structure
S with the fields
S.x for the solution to
S.y for the solution to
S.parameters for the parameters in the solution, and
S.conditions for the conditions on the solution. Elements of the same index in
S.conditions form a solution. Thus,
S.conditions(1) form one solution to the system of equations. The parameters in
S.parameters can appear in all solutions.
S to return the solutions, parameters, and conditions.
Solve a System of Equations Under Conditions
To solve the system of equations under conditions, specify the conditions in the input to
Solve the system of equations considered above for
y in the interval
2*pi. Overlay the solutions on the plot using
Srange = solve(eqn1, eqn2, -2*pi < x, x < 2*pi, -2*pi < y, y < 2*pi, 'ReturnConditions', true); scatter(Srange.x,Srange.y,'k')
Work with Solutions, Parameters, and Conditions Returned by
You can use the solutions, parameters, and conditions returned by
solve to find solutions within an interval or under additional conditions. This section has the same goal as the previous section, to solve the system of equations within a search range, but with a different approach. Instead of placing conditions directly, it shows how to work with the parameters and conditions returned by
For the full solution
S of the system of equations, find values of
y in the interval
2*pi by solving the solutions
S.y for the parameters
S.parameters within that interval under the condition
Before solving for
y in the interval, assume the conditions in
assume so that the solutions returned satisfy the condition. Assume the conditions for the first solution.
Find the parameters in
paramx = intersect(symvar(S.x),S.parameters)
paramy = intersect(symvar(S.y),S.parameters)
Solve the first solution of
x for the parameter
solparamx(1,:) = solve(S.x(1) > -2*pi, S.x(1) < 2*pi, paramx)
Similarly, solve the first solution of
solparamy(1,:) = solve(S.y(1) > -2*pi, S.y(1) < 2*pi, paramy)
Clear the assumptions set by
asumptions to check that the assumptions are cleared.
ans = Empty sym: 1-by-0
Assume the conditions for the second solution.
Solve the second solution to
y for the parameters
solparamx(2,:) = solve(S.x(2) > -2*pi, S.x(2) < 2*pi, paramx)
solparamy(2,:) = solve(S.y(2) > -2*pi, S.y(2) < 2*pi, paramy)
The first rows of
paramy form the first solution to the system of equations, and the second rows form the second solution.
To find the values of
y for these values of
subs to substitute for
solx(1,:) = subs(S.x(1), paramx, solparamx(1,:)); solx(2,:) = subs(S.x(2), paramx, solparamx(2,:))
soly(1,:) = subs(S.y(1), paramy, solparamy(1,:)); soly(2,:) = subs(S.y(2), paramy, solparamy(2,:))
soly are the two sets of solutions to
x and to
y. The full sets of solutions to the system of equations are the two sets of points formed by all possible combinations of the values in
Plot these two sets of points using
scatter. Overlay them on the plot of the equations. As expected, the solutions appear at the intersection of the plots of the two equations.
for i = 1:length(solx(1,:)) for j = 1:length(soly(1,:)) scatter(solx(1,i), soly(1,j), 'k') scatter(solx(2,i), soly(2,j), 'k') end end
Convert Symbolic Results to Numeric Values
Symbolic calculations provide exact accuracy, while numeric calculations are approximations. Despite this loss of accuracy, you might need to convert symbolic results to numeric approximations for use in numeric calculations. For a high-accuracy conversion, use variable-precision arithmetic provided by the
vpa function. For standard accuracy and better performance, convert to double precision using
vpa to convert the symbolic solutions
soly to numeric form.
Simplify Complicated Results and Improve Performance
If results look complicated,
solve is stuck, or if you want to improve performance, see, Troubleshoot Equation Solutions from solve Function.