Troubleshoot Equation Solutions from solve
Function
If solve
returns solutions that look complicated,
or if solve
cannot handle an input, there are
many options. These options simplify the solution space for solve
.
These options also help solve
when the input
is complicated, and might allow solve
to return
a solution where it was previously stuck.
Return Only Real Solutions
Solve the equation x^5 - 1 == 0
. This equation
has five solutions.
syms x solve(x^5 - 1 == 0, x)
ans = 1 - (2^(1/2)*(5 - 5^(1/2))^(1/2)*1i)/4 - 5^(1/2)/4 - 1/4 (2^(1/2)*(5 - 5^(1/2))^(1/2)*1i)/4 - 5^(1/2)/4 - 1/4 5^(1/2)/4 - (2^(1/2)*(5^(1/2) + 5)^(1/2)*1i)/4 - 1/4 5^(1/2)/4 + (2^(1/2)*(5^(1/2) + 5)^(1/2)*1i)/4 - 1/4
If you only need real solutions, specify the Real
option
as true
. The solve
function
returns the one real solution.
solve(x^5 - 1, x, 'Real', true)
ans = 1
Apply Simplification Rules
Solve the following equation. The solve
function
returns a complicated solution.
syms x solve(x^(5/2) + 1/x^(5/2) == 1, x)
ans = 1/(1/2 - (3^(1/2)*1i)/2)^(2/5) 1/((3^(1/2)*1i)/2 + 1/2)^(2/5) -(5^(1/2)/4 - (2^(1/2)*(5 - 5^(1/2))^(1/2)*1i)/4 + 1/4)/(1/2 - (3^(1/2)*1i)/2)^(2/5) -((2^(1/2)*(5 - 5^(1/2))^(1/2)*1i)/4 + 5^(1/2)/4 + 1/4)/(1/2 - (3^(1/2)*1i)/2)^(2/5) -(5^(1/2)/4 - (2^(1/2)*(5 - 5^(1/2))^(1/2)*1i)/4 + 1/4)/(1/2 + (3^(1/2)*1i)/2)^(2/5) -((2^(1/2)*(5 - 5^(1/2))^(1/2)*1i)/4 + 5^(1/2)/4 + 1/4)/(1/2 + (3^(1/2)*1i)/2)^(2/5)
To apply simplification rules when solving equations, specify
the IgnoreAnalyticConstraints
option as true
.
The applied simplification rules are not generally correct mathematically
but might produce useful solutions, especially in physics and engineering.
With this option, the solver does not guarantee the correctness and
completeness of the result.
solve(x^(5/2) + 1/x^(5/2) == 1, x, 'IgnoreAnalyticConstraints', true)
ans = 1/(1/2 - (3^(1/2)*1i)/2)^(2/5) 1/((3^(1/2)*1i)/2 + 1/2)^(2/5)
This solution is simpler and more usable.
Use Assumptions to Narrow Results
For solutions to specific cases, set assumptions to return appropriate
solutions. Solve the following equation. The solve
function
returns seven solutions.
syms x solve(x^7 + 2*x^6 - 59*x^5 - 106*x^4 + 478*x^3 + 284*x^2 - 1400*x + 800, x)
ans = 1 - 5^(1/2) - 1 - 17^(1/2)/2 - 1/2 17^(1/2)/2 - 1/2 -5*2^(1/2) 5*2^(1/2) 5^(1/2) - 1
Assume x
is a positive number and solve the
equation again. The solve
function only returns
the four positive solutions.
assume(x > 0) solve(x^7 + 2*x^6 - 59*x^5 - 106*x^4 + 478*x^3 + 284*x^2 - 1400*x + 800, x)
ans = 1 17^(1/2)/2 - 1/2 5*2^(1/2) 5^(1/2) - 1
Place the additional assumption that x
is
an integer using in(x,'integer')
. Place additional
assumptions on variables using assumeAlso
.
assumeAlso(in(x,'integer')) solve(x^7 + 2*x^6 - 59*x^5 - 106*x^4 + 478*x^3 + 284*x^2 - 1400*x + 800, x)
ans = 1
solve
returns the only positive, integer
solution to x
.
Clear the assumptions on x
for further computations by recreating it using
syms
.
syms x
Alternatively, to make several assumptions, use the &
operator.
Make the following assumptions, and solve the following equations.
syms a b c f g h y assume(f == c & a == h & a~= 0) S = solve([a*x + b*y == c, h*x - g*y == f], [x, y], 'ReturnConditions', true); S.x S.y S.conditions
ans = f/h ans = 0 ans = b + g ~= 0
Under the specified assumptions, the solution is x
= f/h
and y = 0
under the condition b
+ g ~= 0
.
Clear the assumptions on the variables for further computations by recreating them using
syms
.
syms a c f h
Simplify Solutions
The solve
function does not call simplification
functions for the final results. To simplify the solutions, call simplify
.
Solve the following equation. Convert the numbers to symbolic
numbers using sym
to return a symbolic result.
syms x S = solve((sin(x) - 2*cos(x))/(sin(x) + 2*cos(x)) == 1/2, x)
S = -log(-(- 140/37 + 48i/37)^(1/2)/2)*1i -log((- 140/37 + 48i/37)^(1/2)/2)*1i
Call simplify
to simplify solution S
.
simplify(S)
ans = -log(37^(1/2)*(- 1/37 - 6i/37))*1i log(2)*1i - (log(- 140/37 + 48i/37)*1i)/2
Call simplify
with more steps to simplify
the result even further.
simplify(S, 'Steps', 50)
ans = atan(6) - pi atan(6)
Tips
To represent a number exactly, use
sym
to convert the number to a floating-point object. For example, usesym(13)/5
instead of13/5
. This represents13/5
exactly instead of converting13/5
to a floating-point number. For a large number, place the number in quotes. Comparesym(13)/5
,sym(133333333333333333333)/5
, andsym('133333333333333333333')/5
.sym(13)/5 sym(133333333333333333333)/5 sym('133333333333333333333')/5
ans = 13/5 ans = 133333333333333327872/5 ans = 133333333333333333333/5
Placing the number in quotes and using
sym
provides the highest accuracy.If possible, simplify the system of equations manually before using
solve
. Try to reduce the number of equations, parameters, and variables.