solve non linear equation

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!!!

 Accepted Answer

Walter Roberson
Walter Roberson on 9 May 2012

0 votes

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.

1 Comment

Thank you so much for your fast answer, highly appreciate :)

Sign in to comment.

More Answers (1)

The Anh
The Anh on 13 May 2012

0 votes

In case I work with a loop and a, b and c are variables and their values change in every loop. How can function "myfun2" have access to those variables?
How can I define mufun2() as a nested function with access to "a", "b" and "c" as share variables?

Tags

Community Treasure Hunt

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

Start Hunting!