Why does the equation solver return only one root when using "abs"?
9 views (last 30 days)
Show older comments
MathWorks Support Team
on 16 Feb 2021
Answered:
MathWorks Support Team
on 26 Feb 2021
I am using the Symbolic Math Toolbox.
When my equation has an absolute value the solver only returns one of the two roots.
这种行为预期吗?
Here is a reproduction example:
>> syms x
>> solve(abs(x) == 1)% ans = 1
>> solve(sqrt(x^2) == 1)% ans = [-1; 1]
Accepted Answer
MathWorks Support Team
on 16 Feb 2021
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[
Meaning 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',真正的)
In case you only want real solutions, you can get the expected result by making an assumption when declaring your variable:
>> syms xreal
>> solve(abs(x) == 1)% ans = [-1; 1]
>> solve(sqrt(x^2) == 1)% ans = [-1; 1]
您可以在以下文档页面上找到有关声明符号变量的更多信息:
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
开始狩猎!