## Documentation Center |

Equations and systems solver

`Y = solve(eqns)`example`Y = solve(eqns,vars,Name,Value)`

` Y = solve(eqns)` solves
the system of equations

` Y = solve(eqns,vars,Name,Value)` uses
additional options specified by one or more

`[ y1,...,yN] = solve(eqns)` solves
the system of equations

`[ y1,...,yN] = solve(eqns,vars,Name,Value)` uses
additional options specified by one or more

If the right side of an equation is 0, you
can specify either a symbolic expression without the left side or
an equation with the `==` operator:

syms x solve(x^2 - 1) solve(x^2 + 4*x + 1 == 0)

ans = 1 -1 ans = 3^(1/2) - 2 - 3^(1/2) - 2

If the right side of an equation is not 0, specify the equation
using `==`:

syms x solve(x^4 + 1 == 2*x^2 - 1)

ans = (1 + i)^(1/2) (1 - i)^(1/2) -(1 + i)^(1/2) -(1 - i)^(1/2)

To avoid ambiguities when solving equations with symbolic parameters, specify the variable for which you want to solve an equation:

syms a b c x solve(a*x^2 + b*x + c == 0, a) solve(a*x^2 + b*x + c == 0, b)

ans = -(c + b*x)/x^2 ans = -(a*x^2 + c)/x

If you do not specify the variable for which you want to solve
the equation, the toolbox chooses a variable by using the `symvar` function.
Here, the solver chooses the variable *x*:

syms a b c x solve(a*x^2 + b*x + c == 0)

ans = -(b + (b^2 - 4*a*c)^(1/2))/(2*a) -(b - (b^2 - 4*a*c)^(1/2))/(2*a)

When solving a system of equations, use one output argument to return the solutions in the form of a structure array:

syms x y S = solve(x + y == 1, x - 11*y == 5)

S = x: [1x1 sym] y: [1x1 sym]

To display the solutions, access the elements of the structure
array `S`:

S = [S.x S.y]

S = [ 4/3, -1/3]

When solving a system of equations, use multiple output arguments to assign the solutions directly to output variables. The solver returns a symbolic array of solutions for each independent variable.

syms a u v [solutions_a, solutions_u, solutions_v] =... solve(a*u^2 + v^2 == 0, u - v == 1, a^2 + 6 == 5*a)

solutions_a = 3 2 2 3 solutions_u = (3^(1/2)*i)/4 + 1/4 (2^(1/2)*i)/3 + 1/3 1/3 - (2^(1/2)*i)/3 1/4 - (3^(1/2)*i)/4 solutions_v = (3^(1/2)*i)/4 - 3/4 (2^(1/2)*i)/3 - 2/3 - (2^(1/2)*i)/3 - 2/3 - (3^(1/2)*i)/4 - 3/4

Entries with the same index form the solutions of a system:

solutions = [solutions_a, solutions_u, solutions_v]

solutions = [ 3, (3^(1/2)*i)/4 + 1/4, (3^(1/2)*i)/4 - 3/4] [ 2, (2^(1/2)*i)/3 + 1/3, (2^(1/2)*i)/3 - 2/3] [ 2, 1/3 - (2^(1/2)*i)/3, - (2^(1/2)*i)/3 - 2/3] [ 3, 1/4 - (3^(1/2)*i)/4, - (3^(1/2)*i)/4 - 3/4]

Solve this system of equations and assign the
solutions to variables `b` and `a`.
To ensure the correct order of the returned solutions, specify the
variables explicitly. The order in which you specify the variables
defines the order in which the solver returns the solutions.

syms a b [b, a] = solve(a + b == 1, 2*a - b == 4, b, a)

b = -2/3 a = 5/3

Try solving the following equation. The symbolic solver cannot find an exact symbolic solution for this equation, and therefore, it calls the numeric solver. Because the equation is not polynomial, an attempt to find all possible solutions can take a long time. The numeric solver does not try to find all numeric solutions for this equation. Instead, it returns only the first solution that it finds:

syms x solve(sin(x) == x^2 - 1)

ans = -0.63673265080528201088799090383828

Plotting the left and the right sides of the equation in one graph shows that the equation also has a positive solution:

Plotting the left and the right sides of the equation in one graph shows that the equation also has a positive solution:

ezplot(sin(x), -2, 2) hold on ezplot(x^2 - 1, -2, 2) hold off

You can find this solution by calling the MuPAD^{®} numeric
solver directly and specifying the interval where this solution can
be found. To call MuPAD commands from the MATLAB^{®} Command
Window, use the `evalin` or `feval` function:

evalin(symengine, 'numeric::solve(sin(x) = x^2 - 1, x = 0..2)')

ans = 1.4096240040025962492355939705895

Solve these trigonometric equations. For the first equation, the solver returns the solution with one parameter and issues a warning indicating the values of the parameter. For the second equation, the solver chooses one value of the parameter and returns the solution corresponding to this value:

syms x solve(sin(1/sqrt(x)) == 0, x) solve(sin(1/x) == 0, x)

Warning: The solutions are parametrized by the symbols: k = Z_ intersect Dom::Interval([0], infinity) ans = 1/(pi^2*k^2) ans = 1/pi

Solve this equation. It has five solutions:

syms x solve(x^5 == 3125, x)

ans = 5 (5*5^(1/2))/4 + (2^(1/2)*(5^(1/2) + 5)^(1/2)*5*i)/4 - 5/4 (5*5^(1/2))/4 - (2^(1/2)*(5^(1/2) + 5)^(1/2)*5*i)/4 - 5/4 (2^(1/2)*(5 - 5^(1/2))^(1/2)*5*i)/4 - (5*5^(1/2))/4 - 5/4 - (2^(1/2)*(5 - 5^(1/2))^(1/2)*5*i)/4 - (5*5^(1/2))/4 - 5/4

If you need a solution in real numbers, use `Real`.
The only real solution of this equation is 5:

solve(x^5 == 3125, x, 'Real', true)

ans = 5

Solve this equation. Instead of returning an infinite set of periodic solutions, the solver picks these three solutions that it considers to be most practical:

syms x solve(sin(x) + cos(2*x) == 1, x)

ans = 0 pi/6 (5*pi)/6

To pick only one solution, use `PrincipalValue`:

solve(sin(x) + cos(2*x) == 1, x, 'PrincipalValue', true)

ans = 0

Solve this equation. By default, the solver returns a complete, but rather long and complicated solution:

syms x solve(x^(7/2) + 1/x^(7/2) == 1, x)

ans = 1/((3^(1/2)*i)/2 + 1/2)^(2/7) 1/(1/2 - (3^(1/2)*i)/2)^(2/7) exp((pi*4*i)/7)/(3^(1/2)*(i/2) + 1/2)^(2/7) exp((pi*4*i)/7)/(3^(1/2)*(-i/2) + 1/2)^(2/7) -exp((pi*3*i)/7)/(3^(1/2)*(i/2) + 1/2)^(2/7) -exp((pi*3*i)/7)/(3^(1/2)*(-i/2) + 1/2)^(2/7)

To apply the simplification rules that shorten the result, use `IgnoreAnalyticConstraints`:

solve(x^(7/2) + 1/x^(7/2) == 1, x,... 'IgnoreAnalyticConstraints', true)

ans = 1/((3^(1/2)*i)/2 + 1/2)^(2/7) 1/(1/2 - (3^(1/2)*i)/2)^(2/7)

The `sym` and `syms` functions
let you set assumptions for symbolic variables. For example, declare
that the variable *x* can have only positive values:

syms x positive

When you solve an equation or a system of equations with respect to such a variable, the solver verifies the results against the assumptions and returns only the solutions consistent with the assumptions:

solve(x^2 + 5*x - 6 == 0, x)

ans = 1

To ignore the assumptions and return all solutions, use `IgnoreProperties`:

solve(x^2 + 5*x - 6 == 0, x, 'IgnoreProperties', true)

ans = 1 -6

For further computations, clear the assumption that you set
for the variable *x*:

syms x clear

When you solve a higher-order polynomial equation,
the solver sometimes uses `RootOf` to return the
results:

syms x a solve(x^4 + 2*x + a == 0, x)

ans = RootOf(z^4 + 2*z + a, z)

To get an explicit solution for such equations, try calling
the solver with `MaxDegree`. The option specifies
the maximal degree of polynomials for which the solver tries to return
explicit solutions. The default value is 3. Increasing this value,
you can get explicit solutions for higher-order polynomials. For example,
increase the value of `MaxDegree` to 4 and get explicit
solutions instead of `RootOf` for the fourth-order
polynomial:

s = solve(x^4 + 2*x + a == 0, x, 'MaxDegree', 4); pretty(s)

/ #2 - #3 \ | | | #3 + #1 | | | | #3 - #1 | | | \ - #3 - #2 / where sqrt(3) sqrt(- #4 - sqrt(3) a #7 4 - #6) #1 == ---------------------------------------- #5 sqrt(3) sqrt(#6 - sqrt(3) a #7 4 - #4) #2 == -------------------------------------- #5 sqrt(3) #7 #3 == ---------- 6 #8 #4 == 3 sqrt(3) #7 #9 / / 3 \2/3 \1/4 | | sqrt(3) sqrt(27 - 16 a ) 2 | | #5 == | 12 a + | -------------------------- + 2 | 9 | #8 6 \ \ 9 / / 3 #6 == 4 sqrt(3) sqrt(6) sqrt(sqrt(3) sqrt(27 - 16 a ) + 9) #7 == sqrt(4 a + 3 #9) / 3 \1/6 | sqrt(3) sqrt(27 - 16 a ) 2 | #8 == | -------------------------- + 2 | \ 9 / / 3 \2/3 | 2 sqrt(3) sqrt(27 - 16 a ) | #9 == | -------------------------- + 2 | \ 9 /

`dsolve` | `linsolve` | `symvar` | `vpasolve`

Was this topic helpful?