Difficulty in calling values from main program to fsolve function
1 view (last 30 days)
Show older comments
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?????
0 Comments
Accepted Answer
Mischa Kim
on 11 Mar 2014
Edited: Mischa Kim
on 11 Mar 2014
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
More Answers (0)
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!