Why does the equation solver return only one root when using "abs"?

I am using the Symbolic Math Toolbox.
When my equation has an absolute value the solver only returns one of the two roots.
Is this behavior expected?
Here is a reproduction example:
>> syms x >> solve(abs(x) == 1) % ans = 1 >> solve(sqrt(x^2) == 1) % ans = [-1; 1]

 Accepted Answer

This is an expected behavior. Symbolic variables are created as complex by default. In your case, the equation is solved like this:
|z| == 1 => z = exp(j * y), y = [0, 2pi[
This means there are an infinite number of solutions along the unit circle. Only one solution is returned by default. To get all the solutions, you need to specify "ReturnConditions" as true:
>> solve(abs(x) == 1, 'ReturnConditions',true)
If you only want real solutions, you can get the expected result by making an assumption when declaring your variable:
>> syms x real >> solve(abs(x) == 1) % ans = [-1; 1] >> solve(sqrt(x^2) == 1) % ans = [-1; 1]
For more information about declaring symbolic variables, you can access the release-specific documentation by executing the following command in the MATLAB R2020b command window:
>> web(fullfile(docroot, 'symbolic/syms.html'))
Please follow the link below to search for the required information regarding the current release:

More Answers (0)

Categories

Products

Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!