MATLAB fsolve giving wrong solution
I tried to solve equations of 3 spheres using MATLAB's fsolve function, but it is giving the wrong solution. here is my MATLAB function
function point = sphereIntersection( x1, y1, z1, r1, x2, y2, z2, r2, x3, y3, z3, r3 )
system = @(X) [ (X(1)-x1).^2 + (X(2)-y1).^2 + (X(3)-z1).^2 - r1.^2; (X(1)-x2).^2 + (X(2)-y2).^2 + (X(3)-z2).^2 - r2.^2; (X(1)-x3).^2 + (X(2)-y3).^2 + (X(3)-z3).^2 - r3.^2; ];
guess = [0;0;0];
point = fsolve( system, guess );
endAnd I called the function like this
sphereIntersection(0, 0, 0, 5, 8, 0, 0, 5, 4, 4, 0, 5)Here 1st sphere is centered at origin, 2nd at (8,0,0) and third is at (4,4,0) and all have a radius of 5. The actual solution to the system is (4,0,+3) and (4,0,-3). but the fsolve function is returning (4,-1.35,0). Where the thing went wrong?
$\endgroup$1 Answer
$\begingroup$Usually such methods are very sensitive to initial guess, i.e. they may converge for any guess from some domain, but nothing is promised if your guess is outside of this domain.
The S below is your system.
>> fsolve( S, [0,0,0] )
No solution found.
fsolve stopped because the last step was ineffective. However, the vector of function values is not near zero, as measured by the default value of the function tolerance.
<stopping criteria details>
ans = 4.0000 -1.3500 0Note the message - the result you have obtained isn't valid and matlab yells full of it, just read the message. However if you take another initial guess you can obtain the solution easily.
>> fsolve( S, [1,1,1] )
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ans = 4.0000 -0.0000 3.0000
>> $\endgroup$