Solving absolute value inequality

10 views (last 30 days)
Andrew Wiles
Andrew Wiles on 16 Sep 2017
Commented: Walter Roberson on 3 Jan 2018
syms z;
solve(abs(1+z)<=1,z)
gives
ans =
-3/2
But I should've got z=[-2,0]. How to get correct answer?

Answers (1)

Walter Roberson
Walter Roberson on 17 Sep 2017
Edited: Walter Roberson on 17 Sep 2017
You encountered a "feature" of solve. I recommend that you write a bug report about it and reference case 02336440. It becomes really obvious when you do something like
>> solve(z>7)
ans =
8
The "feature" is that now when an interval is the solution, MATLAB returns a representative solution. I worked out in detail which representative solution will be chosen; see below.
The official Mathworks response is that you should be using 'returnconditions':
>> syms z; sol = solve(abs(1+z)<=1,z, 'returnconditions',true)
sol =
struct with fields:
z: [1×1 sym]
parameters: [1×2 sym]
conditions: [1×1 sym]
>> sol.z
ans =
x*exp(y*1i) - 1
>> sol.parameters
ans =
[ x, y]
>> sol.conditions
ans =
y < 2*pi & 0 <= x & x <= 1 & 0 <= y
Another way of writing that condition would be x in [0, 1], y in [0, 2*pi), z is x*exp(y*1i) - 1
If you test out this solution you will see that most of the generated z are complex. This is because you did not restrict yourself to real values.
>> syms z real; sol = solve(abs(1+z)<=1,z, 'returnconditions',true)
sol =
struct with fields:
z: [1×1 sym]
parameters: [1×1 sym]
conditions: [1×1 sym]
>> sol.z, sol.parameters,sol.conditions
ans =
x
ans =
x
ans =
x <= 0 & -2 <= x
The symbolic engine calculates the values perfectly well and understandably; the mess comes when the interface between MATLAB and the symbolic engine munge the results into a different format.
As I reported in the case:
Experimentally the rule appears to be break the solutions up into disjoint intervals. For each of the disjoint intervals expressible through a simple pair of bounds
if both bounds of the interval are the same then
if that bound is infinite
emit empty rather than the infinite bound
else
emit that bound
end
elseif the bounds are -inf and +inf
emit 0
elseif one of the bounds is infinite
emit the other bound plus 1 * sign() of the infinity
elseif 0 is in the interval
emit 0
elseif pi is in the interval
emit pi
else emit the average of the bounds
end
return the union of all of the emitted values
When there are intervals that are not disjoint, the outputs are similar to the above, but instead of the plain average of two bounds, the min() and max() of the bounds that enclose the interval (whether open or closed) are calculated and those are averaged.
  1 Comment
Walter Roberson
Walter Roberson on 3 Jan 2018
In a later version I also see exp(1) showing up as an answer. I do not know whether I missed it before (possible) or if it was added since then (possible)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!