MATLAB: Solve non linear equation

fsolve

I wrote a function to find value of x (with 3 unknown) by solving 3 equations described in F
a= [5 8 4 4];
b= [4 15 3 3];
c= [12 12 2 2];
x0=[0 0 0];
function F = myfun2(x)
F = [sqrt((a(1)-x(1))^2+(a(2)-x(2))^2+(a(3)-x(3))^2)-(a(4)+4);
sqrt((b(1)-x(1))^2+(b(2)-x(2))^2+(b(3)-x(3))^2)-(b(4)+4);
sqrt((c(1)-x(1))^2+(c(2)-x(2))^2+(c(3)-x(3))^2)-(c(4)+4)];
Then I got the error after running this command
x=fsolve(@myfun2,x0)
??? Undefined function or method 'a' for input arguments of type 'double'.
Error in ==> myfun2 at 2
F = [sqrt((a(1)-x(1))^2+(a(2)-x(2))^2+(a(3)-x(3))^2)-(a(4)+4);
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot
continue.
anyone have any idea that what problem I have? And how to solve it?
Thanks a lot!!!

Best Answer

The workspace of your function myfun2() does not have access to whichever workspace you defined "a" in.
Consider passing "a" and "b" in to the function myfun2(). Also consider defining myfun2() as a nested function with access to "a" and "b" as shared variables.