Quantcast

Documentation Center

  • Trial Software
  • Product Updates

solve

Equations and systems solver

Syntax

  • Y = solve(eqns) example
  • Y = solve(eqns,vars,Name,Value)
  • [y1,...,yN] = solve(eqns) example
  • [y1,...,yN] = solve(eqns,vars,Name,Value) example

Description

example

S = solve(eqn) solves the equation eqn for the default variable determined by symvar. You can specify the independent variable. For example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 with respect to the variable x.

example

S = solve(eqn,var,Name,Value) uses additional options specified by one or more Name,Value pair arguments. If you do not specify var, the solver uses the default variable determined by symvar.

example

Y = solve(eqns) solves the system of equations eqns for the variables determined by symvar and returns a structure array that contains the solutions. The number of fields in the structure array corresponds to the number of independent variables in a system.

Y = solve(eqns,vars,Name,Value) uses additional options specified by one or more Name,Value pair arguments. If you do not specify vars, the solver uses the default variables determined by symvar.

example

[y1,...,yN] = solve(eqns) solves the system of equations eqns for the variables determined by symvar and assigns the solutions to the variables y1,...,yN.

example

[y1,...,yN] = solve(eqns,vars,Name,Value) uses additional options specified by one or more Name,Value pair arguments. If you specify the variables vars, solve returns the results in the same order in which you specify vars. If you do not specify vars, the solver uses the default variables determined by symvar.

Examples

Solve Univariate Equations

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)

Solve Multivariate Equations

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)

Solve a System of Equations Returning Solutions as a Structure Array

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]

Solve a System of Equations Assigning the 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
[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]

Specify the Order of Returned Solutions

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

Return Numeric Solutions

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

Return Parameterized Solutions

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

Return Real Solutions

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

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

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)

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

Specify Maximal Degree of Polynomials for Which the Solver Uses Explicit Formulas

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                 /

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 an 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 of eqns are symbolic expressions (without the right side), the solver assumes that the right sides of those equations are 0s.

vars — Variables for which you solve an equation or a 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.

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

falseUse strict simplification rules.
trueApply 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 the properties of variablesfalse (default) | true

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

falseDo not exclude solutions inconsistent with the properties of variables.
trueExclude solutions inconsistent with the properties of variables.

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

Maximal degree of polynomial equations for which the 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 only one solutionfalse (default) | true

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

falseReturn all solutions.
trueReturn 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.

falseReturn all solutions.
trueReturn 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 an equationsymbolic array

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

Y — Solutions of a system of equationsstructure array

Solutions of a system of equations, returned as a structure array. The number of fields in the structure array corresponds to the number of independent variables in a system.

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

Solutions of a 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.

More About

expand all

Tips

  • If the symbolic solver cannot find a solution of an equation or a system of equations, the toolbox internally calls the numeric solver that tries to find a numeric approximation. For polynomial equations and systems without symbolic parameters, the numeric solver returns all solutions. For nonpolynomial equations and systems without symbolic parameters, the solver returns only one solution (if a solution exists).

  • If the solution of an equation or a system of equations contains parameters, the solver can choose one or more values of the parameters and return the results corresponding to these values. For some equations and systems, the solver returns parameterized solutions without choosing particular values. In this case, the solver also issues a warning indicating the values of parameters in the returned solutions.

  • 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, for the call [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 assigned to a and the solutions for b assigned 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.

See Also

| | |

Related Examples

Was this topic helpful?