Documentation Center |
Equations and systems solver
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.
[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.
[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.
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)
The result is:
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:
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)
The solver returns a symbolic array of solutions for each independent variable:
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
Solve the following equation:
syms x solve(sin(x) == x^2 - 1)
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:
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 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:
syms x solve(sin(1/sqrt(x)) == 0, x) solve(sin(1/x) == 0, x)
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:
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:
syms x solve(x^5 == 3125, x)
This equation has five solutions:
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/4If 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:
syms x solve(sin(x) + cos(2*x) == 1, x)
Instead of returning an infinite set of periodic solutions, the solver picks these three solutions that it considers to be most practical:
ans =
0
pi/6
(5*pi)/6To 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
1/2 1/2 1/2 1/2 1/2 1/2 3 1/2 1/2 1/2
3 (- 3 3 #4 #5 - 4 3 a #4 - 4 3 6 (3 (27 - 16 a ) + 9) )
#1 == ----------------------------------------------------------------------------------
/ 1/2 3 1/2 \1/6
1/4 | 2 3 (27 - 16 a ) |
6 (12 a + 9 #5) | ---------------------- + 2 |
\ 9 /
1/2 1/2 1/2 1/2 3 1/2 1/2 1/2 1/2 1/2
3 (4 3 6 (3 (27 - 16 a ) + 9) - 4 3 a #4 - 3 3 #4 #5)
#2 == --------------------------------------------------------------------------------
/ 1/2 3 1/2 \1/6
1/4 | 2 3 (27 - 16 a ) |
6 (12 a + 9 #5) | ---------------------- + 2 |
\ 9 /
1/2
3 #4
#3 == -----------------------------------
/ 1/2 3 1/2 \1/6
| 2 3 (27 - 16 a ) |
6 | ---------------------- + 2 |
\ 9 /
1/2
#4 == (4 a + 3 #5)
/ 1/2 3 1/2 \2/3
| 2 3 (27 - 16 a ) |
#5 == | ---------------------- + 2 |
\ 9 /