Why do I get more real solutions then complex solutions for a equation?

1 view (last 30 days)
I tried to solve an equation using the MATLAB symbolic toolbox:
syms x;
solve(abs(x+1)+abs(5*x-2)==6,x);
The returned result is
ans=-0.75;
If I only allow real solutions by typing
syms x real
solve(abs(x+1)+abs(5*x-2)==6,x)
I get two real solutions
ans =
7/6
-3/4
Why is MATLAB unable to find the second solution (7/6) when solving the equation for complex x?
  4 Comments
Adam
Adam on 26 Sep 2014
Yeah, I thought it might be, it just looked as though it would have been a copy and pasted result!

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 26 Sep 2014
Edited: John D'Errico on 26 Sep 2014
I would point out it is important to read what the toolbox tells you.
syms x;
solve(abs(x+1)+abs(5*x-2)==6,x)
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
> In solve at 306
ans =
-0.75
So it had problems solving the problem, so it used a numerical solver instead! READ WHAT IT TELLS YOU! It did not find a symbolic solution, as if X is allowed to be complex, there would be insufficient information to solve the problem.
Think of it like this, if x is complex, it is really a two variable problem, such that
x = x_r + i*x_i
So a real and imaginary part, where both of those variables would be take on real values only. But you have only one equation, and the output of the function you have posed is always real, since it has those abs functions in it. So essentially, you have one equation, with two unknowns. My guess is there may be an infinite number of solutions if you pose this problem in the complex plane.
Lets see how this works if we expand the problem as the symbolic toolbox might implicitly do internally. By the way, the square roots below will always be the principle square root, so the positive branch.
real(sqrt((x_r + 1)^2 + x_i^2) + sqrt((5*x_r - 2)^2 + (5*x_i)^2))) == 6
imag(sqrt((x_r + 1)^2 + x_i^2) + sqrt((5*x_r - 2)^2 + (5*x_i)^2))) == 0
See that the complex equality is implicitly two equality statements, but the second one, where we take the imaginary part is ALWAYS trivially zero, so it imparts no information about the solution. So we are left with this single equation in two real unknowns, (x_r,x_i).
sqrt((x_r + 1)^2 + x_i^2) + sqrt((5*x_r - 2)^2 + (5*x_i)^2)) == 6
The solution in general to such a problem is usually a curved path in the (x_r,x_i) plane, thus, the complex plane. This is why I suggest there are infinitely many solutions.
We can see the solution locus easily enough, letting contour do the hard work for us.
[x_r,x_i] = meshgrid(-3:.01:3);
contour(x_r,x_i,sqrt((x_r + 1).^2 + x_i.^2) + sqrt((5*x_r - 2).^2 + 5*x_i).^2),[6 6])
grid on
axis square
axis equal
As you can see, there are infinitely many solutions if we look in the complex plane. The two real solutions (x_r, such that x_i==0) appear to be -0.75, and 1.1666667, which matches what solve told you for real solutions.
The problem is indeed much simpler when x is purely real of course, since then it reduces to
abs(x_r + 1) + abs(5*x_r - 2) == 6
We can see the solutions to this graphically. I'll add a reference line at y==6 to indicate the solutions where our function crosses that line.
ezplot(@(x_r) abs(x_r + 1) + abs(5*x_r - 2))
grid on
hold on
plot([-6 6],[6 6],'r-')
The two points of intersection correspond to the real solutions returned by solve.
Of course, solve did return a -0.75 solution, which is indeed one of the valid solutions that solve returned when x was constrained to be purely real. This was generated by a numerical solver, and numerical solvers cannot be expected to return all the solutions to a problem.
  3 Comments
Sean de Wolski
Sean de Wolski on 26 Sep 2014
Do you have warnings turned off? It does for me.
syms x
solve(abs(x+1)+abs(5*x-2)==6,x)
Warning: Cannot solve symbolically. Returning a numeric
approximation instead.
> In solve at 306
Maybe:
doc warning
John D'Errico
John D'Errico on 26 Sep 2014
Edited: John D'Errico on 26 Sep 2014
My guess is warnings must be turned off. (If I turn them off, I get the same non-informatory result as you did.) They can get that way, though I'm not sure why that happened for you. Try this:
warning('on')
The retry the solve call and see if a warning happens.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!