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

 채택된 답변

Walter Roberson
Walter Roberson 2012년 5월 9일

0 개 추천

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

The Anh
The Anh 2012년 5월 10일
Thank you so much for your fast answer, highly appreciate :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

The Anh
The Anh 2012년 5월 13일

0 개 추천

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?

태그

Community Treasure Hunt

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

Start Hunting!

Translated by