I have more non-linear equations than unknowns. How can I solve it in matlab? One method I know is non-linear least squares, how can I implement it in Matlab or you just suggest me anything you deem more proper.

x1=475060;y1=1096300;z1=4670;x2=481500;y2=1094900;z2=4694;x3=482230;y3=1088430;z3=4831;x4=478050;y4=1087810;z4=4775;
% ((x-x1)^2)+((y-y1)^2)+((z-z1)^2)=5942.607^2 % ((x-x2)^2)+((y-y2)^2)+((z-z2)^2)=2426.635^2 % ((x-x3)^2)+((y-y3)^2)+((z-z3)^2)=5094.254^2 % ((x-x4)^2)+((y-y4)^2)+((z-z4)^2)=5549.874^2
%4 equations
%x y z unknowns

 Accepted Answer

Aside from FSOLVE, you can try LSQNONLIN which is another least squares solver. However, I obtain nearly the same results (x,y,z) for your problem with both FSOLVE and LSQNONLIN.
F=@(x,y,z)[((x-x1)^2)+((y-y1)^2)+((z-z1)^2)-5942.607^2
((x-x2)^2)+((y-y2)^2)+((z-z2)^2)-2426.635^2;
((x-x3)^2)+((y-y3)^2)+((z-z3)^2)-5094.254^2;
((x-x4)^2)+((y-y4)^2)+((z-z4)^2)-5549.874^2
]
fun=@(u) F(u(1),u(2),u(3));
x0=mean([x1 y1 z1;x2 y2 z2; x3 y3 z3; x4 y4 z4])
[u1,fval1]=fsolve(fun,x0),
[u2,~,fval2]=lsqnonlin(fun,x0)
>> norm(u1-u2)
ans =
9.7868e-04

4 Comments

it gives me this warning;
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
No solution found.
fsolve stopped because the relative size of the current step is less than the default value of the step size tolerance, but the vector of function values is not near zero as measured by the default value of the function tolerance.
Sermet, I think you need once and for all to give up the notion that you are "solving" these four equations by adjusting only three variables. In general you cannot solve these equations in the sense that all four equations will be satisfied or even nearly satisfied. Your best solution least-squares-wise may be very far from a true solution. If you attempt to use 'fsolve' which is dedicated to finding actual solutions, you can probably expect to obtain, at the very least, some warning messages, as you apparently have. I think you should follow the second part of Matt's advice and use 'lsqnonlin' which only tries to minimize the squared-error and is content even with a large least squares error as its answer.
@Sermet,
FSOLVE is telling you that an exact solution could not be found. However, because you had more equations than unknowns, it did default to a least squares algorithm. So, there was no real point to using lsqnonlin, except in my example to prove to you that they produce nearly the same thing.
As the three of us have discussed before, an exact solution is generally not available. However, for the particular data you've given, the least squares solution's residuals fairly small, compared to the radii of the spheres, in all 4 equations,
>> [xyz,residuals]=fsolve(fun,x0)
xyz =
1.0e+06 *
0.4800 1.0930 0.0045
residuals =
6.5293
-10.6212
12.1881
-10.1086
So, you could say that all 4 spheres "almost" intersect in this specific case.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 2 Dec 2013

Edited:

on 3 Dec 2013

Community Treasure Hunt

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

Start Hunting!