Difficulty in calling values from main program to fsolve function

my version is MATLAB 7.10.0(R2010a) My main program is aa=10; ab=12; ac=18; x0=[0;0]; result=fsolve(@myfun3,x0);
My function is myfun3 created as function F=myfun3(x) F=[2*x(1)^2+(x(2)*aa)+ab;5*x(2)^2+x(1)+ac]; end
the error shown is ??? Undefined function or variable 'aa'.
Error in ==> myfun3 at 2 F=[2*x(1)^2+(x(2)*aa)+ab;5*x(2)^2+x(1)+ac];
Error in ==> fsolve at 254 fuser = feval(funfcn{3},x,varargin{:});
Error in ==> yyyy at 5 result=fsolve(@myfun3,x0);
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
As i have sorted out the problem the function is not recognising the value of aa,ab and ac from the main problem.Why is that?????

 採用された回答

Mischa Kim
Mischa Kim 2014 年 3 月 11 日
編集済み: Mischa Kim 2014 年 3 月 11 日

0 投票

BITS, you need to pass the parameters ( aa, etc.) to the function:
function my_main()
aa = 10; ab = 12; ac = 18;
param = [aa,ab,ac]; % define parameter vector
x0 = [0;0];
f = @(x) myfun3(x,param); % define function (handle)
result = fsolve(f,x0);
disp(result)
end
function F = myfun3(x,param)
aa = param(1); % back out parameters from vector
ab = param(2);
ac = param(3);
F = [2*x(1)^2 + (x(2)*aa) + ab;...
5*x(2)^2 + x(1) + ac];
end

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

製品

質問済み:

2014 年 3 月 11 日

コメント済み:

2014 年 3 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by