Equations and systems solver

**Character vector inputs have been removed. Instead, use syms to declare variables and replace inputs such as solve('2*x ==
1','x') with solve(2*x == 1,x).**

`S = solve(eqn,var)`

`S = solve(eqn,var,Name,Value)`

`Y = solve(eqns,vars)`

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

`[y1,...,yN] = solve(eqns,vars)`

`[y1,...,yN] = solve(eqns,vars,Name,Value)`

```
[y1,...,yN,parameters,conditions]
= solve(eqns,vars,'ReturnConditions',true)
```

uses
additional options specified by one or more `S`

= solve(`eqn`

,`var`

,`Name,Value`

)`Name,Value`

pair
arguments.

solves
the system of equations `Y`

= solve(`eqns`

,`vars`

)`eqns`

for the variables `vars`

and
returns a structure that contains the solutions. If you do not specify `vars`

, `solve`

uses `symvar`

to find the variables to solve
for. In this case, the number of variables that `symvar`

finds
is equal to the number of equations `eqns`

.

uses
additional options specified by one or more `Y`

= solve(`eqns`

,`vars`

,`Name,Value`

)`Name,Value`

pair
arguments.

`[`

solves
the system of equations `y1,...,yN`

] = solve(`eqns`

,`vars`

)`eqns`

for the variables `vars`

.
The solutions are assigned to the variables `y1,...,yN`

.
If you do not specify the variables, `solve`

uses `symvar`

to
find the variables to solve for. In this case, the number of variables
that `symvar`

finds is equal to the number of output
arguments `N`

.

`[`

uses
additional options specified by one or more `y1,...,yN`

] = solve(`eqns`

,`vars`

,`Name,Value`

)`Name,Value`

pair
arguments.

`[`

returns
the additional arguments `y1,...,yN`

,`parameters`

,`conditions`

]
= solve(`eqns`

,`vars`

,'`ReturnConditions`

',true)`parameters`

and `conditions`

that
specify the parameters in the solution and the conditions on the solution.

Use the `==`

operator to specify
the equation `sin(x) == 1`

and solve it.

syms x eqn = sin(x) == 1; solx = solve(eqn,x)

solx = pi/2

Find the complete solution of the same equation by specifying
the `ReturnConditions`

option as `true`

.
Specify output variables for the solution, the parameters in the solution,
and the conditions on the solution.

[solx, params, conds] = solve(eqn, x, 'ReturnConditions', true)

solx = pi/2 + 2*pi*k params = k conds = in(k, 'integer')

The solution `pi/2 + 2*pi*k`

contains the parameter `k`

which
is valid under the condition `in(k, 'integer')`

.
This condition means the parameter `k`

must be an
integer.

If `solve`

returns an empty object, then
no solutions exist. If `solve`

returns an empty
object with a warning, solutions might exist but `solve`

did
not find any solutions.

eqns = [3*x+2, 3*x+1]; solve(eqns, x)

ans = Empty sym: 0-by-1

Return the complete solution of an equation
with parameters and conditions of the solution by specifying `ReturnConditions`

as `true`

.

Solve the equation sin(*x*)
= 0. Provide two additional output variables
for output arguments `parameters`

and `conditions`

.

syms x eqn = sin(x) == 0; [solx, param, cond] = solve(eqn, x, 'ReturnConditions', true)

solx = pi*k param = k cond = in(k, 'integer')

The solution `pi*k`

contains the parameter `k`

and
is valid under the condition `in(k,'integer')`

. This
condition means the parameter `k`

must be an integer. `k`

does
not exist in the MATLAB^{®} workspace and must be accessed using `param`

.

Find a valid value of `k`

for ```
0 <
x < 2*pi
```

by assuming the condition, `cond`

,
and using `solve`

to solve these conditions for `k`

.
Substitute the value of `k`

found into the solution
for `x`

.

assume(cond) interval = [solx > 0, solx < 2*pi]; solk = solve(interval, param) valx = subs(solx, param, solk)

solk = 1 valx = pi

A valid value of `k`

for ```
0 < x
< 2*pi
```

is `1`

. This produces the value ```
x
= pi
```

.

Alternatively, find a solution for `x`

by choosing
a value of `k`

. Check if the value chosen satisfies
the condition on `k`

using `isAlways`

.

Check if `k = 4`

satisfies the condition on `k`

.

condk4 = subs(cond, param, 4); isAlways(condk4)

ans = logical 1

`isAlways`

returns logical `1`

(`true`

),
meaning `4`

is a valid value for `k`

.
Substitute `k`

with `4`

to obtain
a solution for `x`

. Use `vpa`

to
obtain a numeric approximation.

valx = subs(solx, param, 4) vpa(valx)

valx = 4*pi ans = 12.566370614359172953850573533118

Avoid ambiguities when solving equations with
symbolic parameters by specifying the variable for which you want
to solve an equation. If you do not specify the variable, `solve`

chooses
a variable using `symvar`

. First, solve the quadratic
equation without specifying a variable. `solve`

chooses *x* to
return the familiar solution. Then solve the quadratic equation for `a`

to
return the solution for `a`

.

syms a b c x eqn = a*x^2 + b*x + c == 0; sol = solve(eqn) sola = solve(eqn, a)

sol = -(b + (b^2 - 4*a*c)^(1/2))/(2*a) -(b - (b^2 - 4*a*c)^(1/2))/(2*a) sola = -(c + b*x)/x^2

When solving for more than one variable, the order in which you specify the variables defines the order in which the solver returns the solutions.

Solve this system of equations and assign the solutions to variables `solv`

and `solu`

by
specifying the variables explicitly. The solver returns an array of
solutions for each variable.

syms u v eqns = [2*u^2 + v^2 == 0, u - v == 1]; vars = [v u]; [solv, solu] = solve(eqns, vars)

solv = - (2^(1/2)*1i)/3 - 2/3 (2^(1/2)*1i)/3 - 2/3 solu = 1/3 - (2^(1/2)*1i)/3 (2^(1/2)*1i)/3 + 1/3

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

solutions = [solv solu]

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

A solution of the system is ```
v = - (2^(1/2)*1i)/3 -
2/3
```

, and `u = 1/3 - (2^(1/2)*1i)/3`

.

When solving for multiple variables, it can
be more convenient to store the outputs in a structure array than
in separate variables. The `solve`

function returns
a structure when you specify a single output argument and multiple
outputs exist.

Solve a system of equations to return the solutions in a structure array.

syms u v eqns = [2*u + v == 0, u - v == 1]; S = solve(eqns, [u v])

S = struct with fields: u: [1×1 sym] v: [1×1 sym]

Access the solutions by addressing the elements of the structure.

S.u S.v

ans = 1/3 ans = -2/3

Using a structure array allows you to conveniently substitute
solutions into expressions. The `subs`

function
substitutes the correct values irrespective of which variables you
substitute.

Substitute solutions into expressions using the structure `S`

.

expr1 = u^2; subs(expr1, S) expr2 = 3*v+u; subs(expr2, S)

ans = 1/9 ans = -5/3

Return the complete solution of a system of
equations with parameters and conditions of the solution by specifying `ReturnConditions`

as `true`

.

syms x y eqns = [sin(x)^2 == cos(y), 2*x == y]; S = solve(eqns, [x y], 'ReturnConditions', true); S.x S.y S.conditions S.parameters

ans = pi*k - asin(3^(1/2)/3) asin(3^(1/2)/3) + pi*k ans = 2*pi*k - 2*asin(3^(1/2)/3) 2*asin(3^(1/2)/3) + 2*pi*k ans = in(k, 'integer') in(k, 'integer') ans = k

A solution is formed by the elements of the same index in `S.x`

, `S.y`

,
and `S.conditions`

. Any element of `S.parameters`

can
appear in any solution. For example, a solution is ```
x = pi*k
- asin(3^(1/2)/3)
```

, and `y = 2*pi*k - 2*asin(3^(1/2)/3)`

,
with the parameter `k`

under the condition ```
in(k,
'integer')
```

. This condition means `k`

must
be an integer for the solution to be valid. `k`

does
not exist in the MATLAB workspace and must be accessed with `S.parameters`

.

For the first solution, find a valid value of `k`

for ```
0
< x < pi
```

by assuming the condition `S.conditions(1)`

and
using `solve`

to solve these conditions for `k`

.
Substitute the value of `k`

found into the solution
for `x`

.

assume(S.conditions(1)) interval = [S.x(1)>0, S.x(1)<pi]; solk = solve(interval, S.parameters) solx = subs(S.x(1), S.parameters, solk)

solk = 1 solx = pi - asin(3^(1/2)/3)

A valid value of `k`

for ```
0 < x
< pi
```

is `1`

. This produces the value ```
x
= pi - asin(3^(1/2)/3)
```

.

Alternatively, find a solution for `x`

by choosing
a value of `k`

. Check if the value chosen satisfies
the condition on `k`

using `isAlways`

.

Check if `k = 4`

satisfies the condition on `k`

.

condk4 = subs(S.conditions(1), S.parameters, 4); isAlways(condk4)

ans = logical 1

`isAlways`

returns logical `1`

(`true`

)
meaning `4`

is a valid value for `k`

.
Substitute `k`

with `4`

to obtain
a solution for `x`

. Use `vpa`

to
obtain a numeric approximation.

valx = subs(S.x(1), S.parameters, 4) vpa(valx)

valx = 4*pi - asin(3^(1/2)/3) ans = 11.950890905688785612783108943994

When `solve`

cannot symbolically solve an equation, it
tries to find a numeric solution using `vpasolve`

. The
`vpasolve`

function returns the first solution found.

Try solving the following equation. `solve`

returns a numeric
solution because it cannot find a symbolic solution.

syms x eqn = sin(x) == x^2 - 1; solve(eqn, x)

Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve. > In solve (line 304) ans = -0.63673265080528201088799090383828

Plot the left and the right sides of the equation. Observe that the equation also has a positive solution.

fplot([lhs(eqn) rhs(eqn)], [-2 2])

Find this solution by directly calling the numeric solver `vpasolve`

and
specifying the interval.

vpasolve(eqn, x, [0 2])

ans = 1.4096240040025962492355939705895

`solve`

can solve inequalities
to find a solution that satisfies the inequalities.

Solve the following inequalities. Set `ReturnConditions`

to `true`

to
return any parameters in the solution and conditions on the solution.

$$\begin{array}{l}x>0\\ y>0\\ {x}^{2}+{y}^{2}+xy<1\end{array}$$

syms x y cond1 = x^2 + y^2 + x*y < 1; cond2 = x > 0; cond3 = y > 0; conds = [cond1 cond2 cond3]; sol = solve(conds, [x y], 'ReturnConditions', true); sol.x sol.y sol.parameters sol.conditions

ans = (- 3*v^2 + u)^(1/2)/2 - v/2 ans = v ans = [ u, v] ans = 4*v^2 < u & u < 4 & 0 < v

The parameters `u`

and `v`

do
not exist in the MATLAB workspace and must be accessed using `sol.parameters`

.

Check if the values `u = 7/2`

and ```
v
= 1/2
```

satisfy the condition using `subs`

and `isAlways`

.

condWithValues = subs(sol.conditions, sol.parameters, [7/2,1/2]); isAlways(condWithValues)

ans = logical 1

`isAlways`

returns logical `1`

(`true`

)
indicating that these values satisfy the condition. Substitute these
parameter values into `sol.x`

and `sol.y`

to
find a solution for `x`

and `y`

.

xSol = subs(sol.x, sol.parameters, [7/2,1/2]) ySol = subs(sol.y, sol.parameters, [7/2,1/2])

xSol = 11^(1/2)/4 - 1/4 ySol = 1/2

Convert the solution into numeric form by using `vpa`

.

vpa(xSol) vpa(ySol)

ans = 0.57915619758884996227873318416767 ans = 0.5

Solve this equation. It has five solutions.

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

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

Return only real solutions by setting argument `Real`

to `true`

.
The only real solution of this equation is `5`

.

solve(eqn, 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 eqn = sin(x) + cos(2*x) == 1; solve(eqn, x)

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

Pick only one solution using `PrincipalValue`

.

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

ans = 0

Try to solve this equation. By default, `solve`

does
not apply simplifications that are not always mathematically correct.
As a result, `solve`

cannot solve this equation
symbolically.

syms x eqn = exp(log(x)*log(3*x)) == 4; solve(eqn, x)

Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve. > In solve (line 304) ans = - 14.009379055223370038369334703094 - 2.9255310052111119036668717988769i

Set `IgnoreAnalyticConstraints`

to `true`

to
apply simplifications that might allow `solve`

to
find a result. For details, see Algorithms.

S = solve(eqn, x, 'IgnoreAnalyticConstraints', true)

S = (3^(1/2)*exp(-(log(256) + log(3)^2)^(1/2)/2))/3 (3^(1/2)*exp((log(256) + log(3)^2)^(1/2)/2))/3

`solve`

applies simplifications that allow
it to find a solution. The simplifications applied do not always hold.
Thus, the solutions in this mode might not be correct or complete,
and need verification.

The `sym`

and `syms`

functions
let you set assumptions for symbolic variables.

Assume that the variable *x* can have only
positive values.

syms x positive

When you solve an equation or a system of equations for a variable
under assumptions, the solver only returns solutions consistent with
the assumptions. Solve this equation for `x`

.

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

ans = 1

Allow solutions that do not satisfy the assumptions by setting `IgnoreProperties`

to `true`

.

solve(eqn, x, 'IgnoreProperties', true)

ans = -6 1

For further computations, clear the assumption that you set on the variable
*x* by recreating it using `syms`

.

syms x

`root`

When solving polynomials, `solve`

might
return solutions containing `root`

. To numerically
approximate these solutions, use `vpa`

. Consider
the following equation and solution.

syms x eqn = x^4 + x^3 + 1 == 0; s = solve(eqn, x)

s = root(z^4 + z^3 + 1, z, 1) root(z^4 + z^3 + 1, z, 2) root(z^4 + z^3 + 1, z, 3) root(z^4 + z^3 + 1, z, 4)

Because there are no parameters in this solution, use `vpa`

to
approximate it numerically.

vpa(s)

ans = 0.5189127943851558447865795886366 - 0.666609844932018579153758800733i 0.5189127943851558447865795886366 + 0.666609844932018579153758800733i - 1.0189127943851558447865795886366 - 0.60256541999859902604398442197193i - 1.0189127943851558447865795886366 + 0.60256541999859902604398442197193i

When you solve a higher order polynomial equation, the solver might use `root`

to return the results. Solve an equation of order 3.

syms x a eqn = x^3 + x^2 + a == 0; solve(eqn, x)

ans =$$\left(\begin{array}{c}\mathrm{root}\left({z}^{3}+{z}^{2}+a,z,1\right)\\ \mathrm{root}\left({z}^{3}+{z}^{2}+a,z,2\right)\\ \mathrm{root}\left({z}^{3}+{z}^{2}+a,z,3\right)\end{array}\right)$$

Try to get an explicit solution for such equations by calling the solver with `MaxDegree`

. The option specifies the maximum degree of polynomials for which the solver tries to return explicit solutions. The default value is `2`

. Increasing this value, you can get explicit solutions for higher order polynomials.

Solve the same equations for explicit solutions by increasing the value of `MaxDegree`

to `3`

.

`S = solve(eqn, x, 'MaxDegree', 3)`

S =$$\begin{array}{l}\left(\begin{array}{c}\frac{1}{9\hspace{0.17em}{\sigma}_{1}}+{\sigma}_{1}-\frac{1}{3}\\ -\frac{1}{18\hspace{0.17em}{\sigma}_{1}}-\frac{{\sigma}_{1}}{2}-\frac{1}{3}-\frac{\sqrt{3}\hspace{0.17em}\left(\frac{1}{9\hspace{0.17em}{\sigma}_{1}}-{\sigma}_{1}\right)\hspace{0.17em}i}{2}\\ -\frac{1}{18\hspace{0.17em}{\sigma}_{1}}-\frac{{\sigma}_{1}}{2}-\frac{1}{3}+\frac{\sqrt{3}\hspace{0.17em}\left(\frac{1}{9\hspace{0.17em}{\sigma}_{1}}-{\sigma}_{1}\right)\hspace{0.17em}i}{2}\end{array}\right)\\ \\ \mathrm{where}\\ \\ \mathrm{}{\sigma}_{1}={\left(\sqrt{{\left(\frac{a}{2}+\frac{1}{27}\right)}^{2}-\frac{1}{729}}-\frac{a}{2}-\frac{1}{27}\right)}^{1/3}\end{array}$$

If

`solve`

cannot find a solution and`ReturnConditions`

is`false`

, the`solve`

function internally calls the numeric solver`vpasolve`

that tries to find a numeric solution. If`solve`

cannot find a solution and`ReturnConditions`

is`true`

,`solve`

returns an empty solution with a warning. If no solutions exist,`solve`

returns an empty solution without a warning. For polynomial equations and systems without symbolic parameters, the numeric solver returns all solutions. For nonpolynomial equations and systems without symbolic parameters, the numeric solver returns only one solution (if a solution exists).If the solution contains parameters and

`ReturnConditions`

is`true`

,`solve`

returns the parameters in the solution and the conditions under which the solutions are true. If`ReturnConditions`

is`false`

, the`solve`

function either chooses values of the parameters and returns the corresponding results, or returns parameterized solutions without choosing particular values. In the latter case,`solve`

also issues a warning indicating the values of parameters in the returned solutions.If a parameter does not appear in any condition, it means the parameter can take any complex value.

The output of

`solve`

can contain parameters from the input equations in addition to parameters introduced by`solve`

.Parameters introduced by

`solve`

do not appear in the MATLAB workspace. They must be accessed using the output argument that contains them. Alternatively, to use the parameters in the MATLAB workspace use`syms`

to initialize the parameter. For example, if the parameter is`k`

, use`syms k`

.The variable names

`parameters`

and`conditions`

are not allowed as inputs to`solve`

.The syntax

`S = solve(eqn,var,'ReturnConditions',true)`

returns`S`

as a structure instead of a symbolic array.To solve differential equations, use the

`dsolve`

function.When solving a system of equations, always assign the result to output arguments. Output arguments let you access the values of the solutions of a system.

`MaxDegree`

only accepts positive integers smaller than 5 because, in general, there are no explicit expressions for the roots of polynomials of degrees higher than 4.The output variables

`y1,...,yN`

do not specify the variables for which`solve`

solves equations or systems. If`y1,...,yN`

are the variables that appear in`eqns`

, then there is no guarantee that`solve(eqns)`

will assign the solutions to`y1,...,yN`

using the correct order. Thus, when you run`[b,a] = solve(eqns)`

, you might get the solutions for`a`

assigned to`b`

and vice versa.To ensure the order of the returned solutions, specify the variables

`vars`

. For example, the call`[b,a] = solve(eqns,b,a)`

assigns the solutions for`a`

to`a`

and the solutions for`b`

to`b`

.

When you use `IgnoreAnalyticConstraints`

, the
solver applies these rules to the expressions on both sides of an
equation.

log(

*a*) + log(*b*) = log(*a*·*b*) for all values of*a*and*b*. In particular, the following equality is valid for all values of*a*,*b*, and*c*:(

*a*·*b*)^{c}=*a*^{c}·*b*^{c}.log(

*a*^{b}) =*b*·log(*a*) for all values of*a*and*b*. In particular, the following equality is valid for all values of*a*,*b*, and*c*:(

*a*^{b})^{c}=*a*^{b·c}.If

*f*and*g*are standard mathematical functions and*f*(*g*(*x*)) =*x*for all small positive numbers,*f*(*g*(*x*)) =*x*is assumed to be valid for all complex values*x*. In particular:log(

*e*^{x}) =*x*asin(sin(

*x*)) =*x*, acos(cos(*x*)) =*x*, atan(tan(*x*)) =*x*asinh(sinh(

*x*)) =*x*, acosh(cosh(*x*)) =*x*, atanh(tanh(*x*)) =*x*W

_{k}(*x*·*e*^{x}) =*x*for all values of*k*

The solver can multiply both sides of an equation by any expression except

`0`

.The solutions of polynomial equations must be complete.