using function handle in fsolve

조회 수: 2 (최근 30일)
Marrie
Marrie 2013년 12월 11일
답변: Walter Roberson 2013년 12월 11일
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.

채택된 답변

Walter Roberson
Walter Roberson 2013년 12월 11일
@(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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by