I have an equation that theoretically has to only have one positive solution and the rest must be negative. So I used matlab solve function but two of the solutions are positive instead of one. Then as expected, by testing the solutions one of them does not satisfy the equation which means it is not the proper results. Here is the code:
n=4
c = 10.3343;
theta = [51.7924 ; 4.3564 ; 0.0103 ; 1.3505];
syms x
eqn = sum(sqrt(x.^2 + theta)) - n.*x == c;
sol = solve(eqn,x);
% find pi
xdouble = double(sol);
xdouble(xdouble<0) = [];
cnew1 = sum(sqrt(xdouble(1).^2 + theta)) - n.*xdouble(1)
cnew2 = sum(sqrt(xdouble(2).^2 + theta)) - n.*xdouble(2)
The results are
cnew1 =
10.5373
cnew2 =
10.3343
which clearly second one is the proper answer, but I am confused why matlab generates the first incorrect solution? Any help much appreciated.
It could happens that solve
produce spurious solutions (a.k.a incorrect solutions). Check the wikipedia article about spurious solutions.
Why ?
Because solve
try to solve your problem analytically. During this process matlab can transform your initial equation to something easier to solve.
For example if we have the following equation:
x+1=0
It is obvious that the solution is x = -1
.
But matlab could do something like:
x = -1
x^2 = (-1)^2
x^2 = 1
And now we have two solutions: -1 and 1. 1 is a spurious solution.
So you have 2 choices to avoid this problem:
solve
are correct.vpasolve
, vpasolve
use numerical analysis. So, strictly speaking,vpasolve
do not solve your equation but instead determine by iteration the possible solutions.So:
sol = vpasolve(eqn,x);
will do the trick