Accelerating the pace of engineering and science

# solve

Equations and systems solver

solve does not accept string inputs containing multiple input arguments. In future releases, string inputs will be deprecated. In place of string inputs, first declare the variables using syms and pass them as a comma-separated list or vector.

## Syntax

• Y = solve(eqns,vars)
• Y = solve(eqns,vars,Name,Value) example
• [y1,...,yN] = solve(eqns,vars) example
• [y1,...,yN] = solve(eqns,vars,Name,Value)
• [y1,...,yN,parameters,conditions] = solve(eqns,vars,'ReturnConditions',true) example

## Description

example

S = solve(eqn,var) solves the equation eqn for the variable var. If you do not specify var, the variable to solve for is determined by symvar. For example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 for x.

example

S = solve(eqn,var,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

Y = solve(eqns,vars) solves the system of equations 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 symvar finds is equal to the number of equations eqns.

example

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

example

[y1,...,yN] = solve(eqns,vars) solves the system of equations 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 symvar finds is equal to the number of output arguments N.

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

example

[y1,...,yN,parameters,conditions] = solve(eqns,vars,'ReturnConditions',true) returns the additional arguments parameters and conditions that specify the parameters in the solution and the conditions on the solution.

## Examples

### Solve Univariate Equations

Use the == operator to specify the equation sin(x) == 1 and solve it.

```syms x
eqn = sin(x) == 1;
solve(eqn, x)```
```ans =
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 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, this means solutions might exist but solve did not find any solutions.

`solve(3*x+2, 3*x+1, x)`
```ans =
Empty sym: 0-by-1```

### Solve Multivariate Equations

To avoid ambiguities when solving equations with symbolic parameters, specify the variable for which you want to solve an equation. If you do not specify the variable for which you want to solve the equation, solve chooses a variable using symvar. Here, solve chooses the variable x.

```syms a b c x
sola = solve(a*x^2 + b*x + c == 0, a)
sol = solve(a*x^2 + b*x + c == 0)```
```sola =
-(c + b*x)/x^2
sol =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)```

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 b and a by specifying the variables explicitly.

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

a =
5/3```

### Work with Parameters and Conditions Returned by solve

To return the complete solution of an equation with parameters and conditions of the solution, specify ReturnConditions as true.

Solve the equation sin(x) = 0. The solve function returns a structure with three fields. The S.x field contains the solution, S.parameters contains the parameters in the solution, and S.conditions contains the conditions on the solution.

```syms x
S = solve(sin(x) == 0, x, 'ReturnConditions', true);
S.x
S.parameters
S.conditions```
```ans =
pi*k
ans =
k
ans =
in(k, 'integer')```

The solution pi*k contains the parameter k and is valid under the condition in(k, 'integer'). This means the parameter k must be an integer.

To find a numeric solution for x, substitute for k with a value using subs. Check if the value satisfies the condition on k using isAlways.

Check if k = 4 satisfies in(k, 'integer').

`isAlways(subs(S.conditions, S.parameters, 4))`
```ans =
1```

isAlways returns logical 1 (true) meaning 4 is a valid value for k. Substitute 4 for k to obtain a solution for x. Use vpa to obtain a numeric approximation.

```solx = subs(S.x, S.parameters, 4)
vpa(solx)```
```solx =
4*pi
ans =
12.566370614359172953850573533118```

To find a valid value of k for 0<x<2*pi, assume the condition S.conditions and use solve to solve these conditions for k. Substitute the value of k into the solution for x.

```assume(S.conditions)
solk = solve(S.x>0, S.x<2*pi, S.parameters)
solx = subs(S.x, S.parameters, solk)```
```solk =
1
solx =
pi```

A valid value of k for 0<x<2*pi is 1. This produces the solution x = pi.

### Solve a System of Equations Assigning Solutions to Variables

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
[sola, solu, solv] = solve(a*u^2 + v^2 == 0, u - v == 1, a^2 + 6 == 5*a, a, u, v)```
```sola =
2
2
3
3
solu =
1/3 - (2^(1/2)*i)/3
(2^(1/2)*i)/3 + 1/3
1/4 - (3^(1/2)*i)/4
(3^(1/2)*i)/4 + 1/4
solv =
- (2^(1/2)*i)/3 - 2/3
(2^(1/2)*i)/3 - 2/3
- (3^(1/2)*i)/4 - 3/4
(3^(1/2)*i)/4 - 3/4```

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

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

A solution of the system is a = 2, u = 1/3 - (2^(1/2)*i)/3, and v = - (2^(1/2)*i)/3 - 2/3.

### Return Complete Solution of System of Equations with Parameters and Conditions

To return the complete solution of a system of equations with parameters and conditions of the solution, specify ReturnConditions as true. Provide two additional output variables for output arguments parameters and conditions.

```syms x y
[solx, soly, params, conditions] = solve(sin(x) == cos(2*y), x^2 == y,...
[x, y], 'ReturnConditions', true)```
```solx =
1/4 - (16*pi*k - 4*pi + 1)^(1/2)/4
(16*pi*k - 4*pi + 1)^(1/2)/4 + 1/4
(4*pi + 16*pi*k + 1)^(1/2)/4 - 1/4
- (4*pi + 16*pi*k + 1)^(1/2)/4 - 1/4
soly =
((16*pi*k - 4*pi + 1)^(1/2)/4 - 1/4)^2
((16*pi*k - 4*pi + 1)^(1/2)/4 + 1/4)^2
((4*pi + 16*pi*k + 1)^(1/2)/4 - 1/4)^2
((4*pi + 16*pi*k + 1)^(1/2)/4 + 1/4)^2
params =
k
conditions =
in(k, 'integer')
in(k, 'integer')
in(k, 'integer')
in(k, 'integer')```

A solution is formed by the elements of the same index in solx, soly, and conditions. Any element of params can appear in any solution. For example, a solution is x = (4*pi + 16*pi*k + 1)^(1/2)/4 - 1/4, and y = ((4*pi + 16*pi*k + 1)^(1/2)/4 - 1/4)^2, with the parameter k under the condition in(k, 'integer'). This condition means k must be an integer for the solution to be valid.

### Return Numeric Solutions

Try solving the following equation. The symbolic solver cannot find an exact symbolic solution for this equation, and therefore issues a warning before calling 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 it finds.

```syms x
solve(sin(x) == x^2 - 1, x)```
```Warning: Cannot solve symbolically. Returning a numeric
> In solve at 301
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.

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

You can find this solution by calling the numeric solver vpasolve directly and specifying the interval where this solution can be found.

`vpasolve(sin(x) == x^2 - 1, x, [0 2])`
```ans =
1.4096240040025962492355939705895```

### Solve Inequalities

solve can solve inequalities to find a solution that satisfies the conditions. Solve the following conditions. 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
S = solve(x^2+y^2+x*y<1, x>0, y>0, [x, y], 'ReturnConditions', true);
solx = S.x
soly = S.y
params = S.parameters
conditions = S.conditions```
```solx =
(- 3*v^2 + u)^(1/2)/2 - v/2
soly =
v
params =
[ u, v]
conditions =
4*v^2 < u & u < 4 & 0 < v```

Check if the values u = 7/2 and v = 1/2 satisfy the condition using subs and isAlways.

`isAlways(subs(S.conditions, S.parameters, [7/2,1/2]))`
```ans =
1```

isAlways returns logical 1 (true) indicating that these values satisfy the condition. Substitute these parameter values into S.x and S.y to find a solution for x and y.

```solx = subs(S.x, S.parameters, [7/2,1/2])
soly = subs(S.y, S.parameters, [7/2,1/2])```
```solx =
11^(1/2)/4 - 1/4
soly =
1/2```

To convert the solution into numeric form, use vpa.

```vpa(solx)
vpa(soly)```
```ans =
0.57915619758884996227873318416767
ans =
0.5```

### Return Real Solutions

Solve this equation. It has five solutions.

```syms x
solve(x^5 == 3125, x)```
```ans =
5
- (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
(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
```

If you need a solution in real numbers, set argument Real to true. The only real solution of this equation is 5.

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

### Return One Solution

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```

### Apply Simplification Rules That Shorten the Result

By default, solve does not apply simplifications that are not always mathematically correct. As a result, solve cannot solve this equation symbolically.

```syms x
solve(exp(log(x)*log(3*x))==4, x)```
```Warning: Cannot solve symbolically. Returning a numeric
> In solve at 301
ans =
- 14.009379055223370038369334703094 - 2.9255310052111119036668717988769*i
```

Set IgnoreAnalyticConstraints to true to apply simplifications that might allow solve to find a result. For details, see Algorithms.

`S = solve(exp(log(x)*log(3*x))==4, 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 should be verified.

### Ignore Assumptions on Variables

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 only returns solutions consistent with the assumptions.

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

To allow solutions that do not satisfy the assumptions, set IgnoreProperties to true.

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

For further computations, clear the assumption that you set on the variable x.

`syms x clear`

### Numerically Approximating Symbolic Solutions That Contain RootOf

When solving polynomials, solve might return solutions containing RootOf. To numerically approximate these solutions, use vpa. Consider the following equation and solution.

```syms x
s = solve(x^4 + x^3 + 1 == 0, x)```
```s =
RootOf(z^4 + z^3 + 1, z)[1]
RootOf(z^4 + z^3 + 1, z)[2]
RootOf(z^4 + z^3 + 1, z)[3]
RootOf(z^4 + z^3 + 1, z)[4]```

Because there are no parameters in this solution, you can use vpa to approximate it numerically.

`vpa(s)`
```ans =
- 1.0189127943851558447865795886366 + 0.60256541999859902604398442197193*i
- 1.0189127943851558447865795886366 - 0.60256541999859902604398442197193*i
0.5189127943851558447865795886366 + 0.666609844932018579153758800733*i
0.5189127943851558447865795886366 - 0.666609844932018579153758800733*i```

### Solve Polynomial Equations of High Degree

When you solve a higher-order polynomial equation, the solver might use RootOf to return the results.

```syms x a
solve(x^4 + x^3 + a == 0, x)```
```ans =
RootOf(z^4 + z^3 + a, z)[1]
RootOf(z^4 + z^3 + a, z)[2]
RootOf(z^4 + z^3 + a, z)[3]
RootOf(z^4 + z^3 + a, z)[4]```

To get an explicit solution for such equations, try 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 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 + x^3 + a == 0, x, 'MaxDegree', 4);
pretty(s)```
```    /             1 \
| - #3 - #2 - - |
|             4 |
|               |
|            1  |
|  #2 - #3 - -  |
|            4  |
|               |
|            1  |
|  #3 - #1 - -  |
|            4  |
|               |
|            1  |
|  #3 + #1 - -  |
\            4  /

where

/   1/3                                                   \
| #7    #6 9   9 #6     2/3        /      3  \            |
sqrt| ---------- - ---- - #7    #6 9 - | a - --- | #6 12 - #5 |
\      2        64                 \     256 /            /
#1 == ---------------------------------------------------------------
#4

/   1/3                                                   \
| #7    #6 9   9 #6     2/3        /      3  \            |
sqrt| ---------- - ---- - #7    #6 9 - | a - --- | #6 12 + #5 |
\      2        64                 \     256 /            /
#2 == ---------------------------------------------------------------
#4

#6
#3 == -------
1/6
6 #7

/            1/3           \1/4
1/6 |        9 #7          2/3 |
#4 == 6 #7    | 12 a + ------- + 9 #7    |
\           4              /

sqrt(3) sqrt(6) sqrt(9 a + #8) 3
#5 == --------------------------------
8

/            1/3           \
|        9 #7          2/3 |
#6 == sqrt| 12 a + ------- + 9 #7    |
\           4              /

a   #8
#7 == - + --
2   18

/ 135 a      /      3  \2       /      3  \3    189  \
#8 == sqrt(3) sqrt| ----- + 18 | a - --- |  - 256 | a - --- |  - ----- |
\  256       \     256 /        \     256 /    65536 /```

## Input Arguments

expand all

### eqn — Equation to solvesymbolic expression | symbolic equation

Equation to solve, specified as a symbolic expression or symbolic equation. Symbolic equations are defined by the relation operator ==. If eqn is a symbolic expression (without the right side), the solver assumes that the right side is 0, and solves the equation eqn == 0.

### var — Variable for which you solve equationsymbolic variable

Variable for which you solve an equation, specified as a symbolic variable. By default, solve uses the variable determined by symvar.

### eqns — System of equationssymbolic expressions | symbolic equations

System of equations, specified as symbolic expressions or symbolic equations. If any elements of eqns are symbolic expressions (without the right side), solve equates the element to 0.

### vars — Variables for which you solve an equation or system of equationssymbolic variables

Variables for which you solve an equation or system of equations, specified as symbolic variables. By default, solve uses the variables determined by symvar.

The order in which you specify these variables defines the order in which the solver returns the solutions.

### Name-Value Pair Arguments

Example: 'Real',true specifies that the solver returns real solutions.

### 'ReturnConditions' — Flag for returning parameters in solution and conditions on solutionfalse (default) | true

Flag for returning parameters in solution and conditions under which the solution is true, specified as the comma-separated pair consisting of 'ReturnConditions' and one of these values.

 false Do not return parameterized solutions. Do not return the conditions under which the solution holds. The solve function replaces parameters with appropriate values. true Return the parameters in the solution and the conditions under which the solution holds. For a call with a single output variable, solve returns a structure with the fields parameters and conditions. For multiple output variables, solve assigns the parameters and conditions to the last two output variables. This means the number of output variables must be equal to the number of variables to solve for plus two.

Example: [v1, v2, params, conditions] = solve(sin(x) +y == 0,y^2 == 3,'ReturnConditions',true) returns the parameters in params and conditions in conditions.

### 'IgnoreAnalyticConstraints' — Simplification rules applied to expressions and equationsfalse (default) | true

Simplification rules applied to expressions and equations, specified as the comma-separated pair consisting of 'IgnoreAnalyticConstraints' and one of these values.

 false Use strict simplification rules. true Apply purely algebraic simplifications to expressions and equations. Setting IgnoreAnalyticConstraints to true can give you simple solutions for the equations for which the direct use of the solver returns complicated results. In some cases, it also enables solve to solve equations and systems that cannot be solved otherwise. Note that setting IgnoreAnalyticConstraints to true can lead to wrong or incomplete results.

### 'IgnoreProperties' — Flag for returning solutions inconsistent with properties of variablesfalse (default) | true

Flag for returning solutions inconsistent with properties of variables, specified as the comma-separated pair consisting of 'IgnoreProperties' and one of these values.

 false Do not exclude solutions inconsistent with the properties of variables. true Exclude solutions inconsistent with the properties of variables.

### 'MaxDegree' — Maximum degree of polynomial equations for which solver uses explicit formulas3 (default) | positive integer smaller than 5

Maximum degree of polynomial equations for which solver uses explicit formulas, specified as a positive integer smaller than 5. The solver does not use explicit formulas that involve radicals when solving polynomial equations of a degree larger than the specified value.

### 'PrincipalValue' — Flag for returning one solutionfalse (default) | true

Flag for returning one solution, specified as the comma-separated pair consisting of 'PrincipalValue' and one of these values.

 false Return all solutions. true Return only one solution. If an equation or a system of equations does not have a solution, the solver returns an empty symbolic object.

### 'Real' — Flag for returning only real solutionsfalse (default) | true

Flag for returning only real solutions, specified as the comma-separated pair consisting of 'Real' and one of these values.

 false Return all solutions. true Return only those solutions for which every subexpression of the original equation represents a real number. Also, assume that all symbolic parameters of an equation represent real numbers.

## Output Arguments

expand all

### S — Solutions of equationsymbolic array

Solutions of equation, returned as a symbolic array. The size of a symbolic array corresponds to the number of the solutions.

### Y — Solutions of system of equationsstructure

Solutions of system of equations, returned as a structure. The number of fields in the structure correspond to the number of independent variables in a system. If ReturnConditions is set to true, the solve function returns two additional fields that contain the parameters in the solution, and the conditions under which the solution is true.

### y1,...,yN — Solutions of system of equationssymbolic variables

Solutions of system of equations, returned as symbolic variables. The number of output variables or symbolic arrays must be equal to the number of independent variables in a system. If you explicitly specify independent variables vars, then the solver uses the same order to return the solutions. If you do not specify vars, the toolbox sorts independent variables alphabetically, and then assigns the solutions for these variables to the output variables.

### parameters — Parameters in solutionvector of symbolic variables

Parameters in solution, returned as a vector of symbolic variables. This output argument is only returned if ReturnConditions is true. If a single output argument is provided, parameters is returned as a field of a structure. If multiple output arguments are provided, parameters is returned as the second-to-last output argument.

Example: [solx, params, conditions] = solve(sin(x) == 0, 'ReturnConditions', true) returns the parameter k in the argument params.

### conditions — Conditions under which solutions are validvector of symbolic expressions

Conditions under which solutions are valid, returned as a vector of symbolic expressions. This output argument is only returned if ReturnConditions is true. If a single output argument is provided, conditions is returned as a field of a structure. If multiple output arguments are provided, conditions is returned as the last output argument.

Example: [solx, params, conditions] = solve(sin(x) == 0, 'ReturnConditions', true) returns the condition in(k, 'integer') in conditions. The solution in solx is valid only under this condition.

expand all

### Tips

• 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.

• 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, that does not 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.

### Algorithms

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 = ac·bc.

• log(ab) = 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:

(ab)c = ab·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(ex) = 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

• Wk(x·ex) = 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.