S = solve(eqn) solves the equation eqn for the default variable determined by symvar. ... Y = solve(eqns) solves the system of equations eqns for the variables ...
solve Equations and systems solver Syntax S = solve(eqn) S = solve(eqn,var,Name,Value)
example
Y = solve(eqns) Y = solve(eqns,vars,Name,Value)
example
[y1,...,yN] = solve(eqns) [y1,...,yN] = solve(eqns,vars,Name,Value)
example
example
example
Description 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 w ith 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.
example
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.
[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 w hich you specify vars. If you do not specify vars, the solver uses the default variables determined by symvar.
example
Examples Solve Univariate Equations If the right side of an equation is 0, you can specify either a symbolic expression w ithout the left side or an equation w ith 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 w hen solving equations w ith symbolic parameters, specify the variable for w hich you w ant 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 w hich you w ant 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 w ith 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 w hich you specify the variables defines the order in w hich 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 follow ing 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 show s that the equation also has a positive solution: Plotting the left and the right sides of the equation in one graph show s 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 w here 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 w ith one parameter and issues a w arning 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 w ith respect to such a variable, the solver verifies the results against the assumptions and returns only the solutions consistent w ith 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 higherorder 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 w ith MaxDegree. The option specifies the maximal degree of polynomials for w hich the solver tries to return explicit solutions. The default value is 3. Increasing this value, you can get explicit solutions for higherorder polynomials. For example, increase the value of MaxDegree to 4 and get explicit solutions instead of RootOf for the fourthorder 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 solve
symbolic expression | symbolic equation var — Variable for which you solve an equation
symbolic variable eqns — System of equations
symbolic expressions | symbolic equations vars — Variables for which you solve an equation or a system of equations
symbolic variables NameValue Pair Arguments Exam ple: 'Real',true specifies that the solver returns real solutions.
'IgnoreAnalyticConstraints' — Simplification rules applied to expressions and equations false (default) | true 'IgnoreProperties' — Flag for returning solutions inconsistent with the properties of variables false (default) | true 'MaxDegree' — Maximal degree of polynomial equations for which the solver uses explicit formulas 3 (default) | positive integer smaller than 5 'PrincipalValue' — Flag for returning only one solution false (default) | true 'Real' — Flag for returning only real solutions false (default) | true
Output Arguments
expand all
S — Solutions of an equation
symbolic array Y — Solutions of a system of equations
structure array y1,...,yN — Solutions of a system of equations
symbolic variables
More About
expand all
Tips Algorithms
See Also dsolve | linsolve | symvar | vpasolve
Related Examples Solve an Algebraic Equation Solve a System of Algebraic Equations