using function handle in fsolve

2 views (last 30 days)
Hi all,
I have computed the function vector and its Jacobian, and use matlabFunction to create a .m file to store the function and Jacobian. Here is my code:
currdir = [pwd filesep];
filename = [currdir, 'objfun.m'];
matlabFunction(fvec, J, 'file', filename, 'vars', {x, phi});
phi=zeros(N,1);
for i=1:1:STEP
[x,fval,exitflag] = fsolve(@(x,phi)objfun,x0,options);
phi(1:N)=x(1:N);
end
where fvec and J have been computed analytically and objfun.m is created successfully. The only problem is I have a global vector phi in objfun.m that needs to update after each step i. I got the error:
Error using objfun (line 8)
Not enough input arguments.
Error in @(x,phi)objfun
I did not see any inconsistency of dimensions in x and phi. Could anybody give some suggestions? Thank you.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Dec 2013
@(x,phi)objfun means "this is an anonymous function with two dummy arguments, "x" and "phi". When this anonymous function is invoked, it should invoke "objfun" with no arguments."
Remember,
z = objfun
is the same thing as
z = objfun()
Perhaps what you want is instead,
@(x) objfun(x, phi)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!