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 );
end
And 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?
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
Sbelow is yoursystem.Note 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.