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

1 件のコメント

Walter Roberson
Walter Roberson 2012 年 5 月 9 日
Please reformat your question.

サインインしてコメントする。

 採用された回答

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