MATLAB: Passing arguments into fsolve without using globals

fsolve global "passing variables"

Hello,
How do I pass arguments, vectors, or constants into fsolve? A simple example:
REF=[x,y,z] <-- I want this to pass into 'myfun' during fsolve
[out,fval]=fsolve('myfun',y0)
...
function [Z]=myfun(y)
Z=[ REF(1)*y(1) + REF(2);
REF(3)*y(2) - REF(2)];
I am trying to avoid using globals.

Best Answer

1) Rewrite myfun to take two inputs:
function Z = myfun(y,REF)
...
2) Use an anonymous function handle to make a function of one variable, with the second input to myfun (REF) embedded into it:
REF = [x,y,z];
f = @(y) myfun(y,REF); % function of dummy variable y
[out,fval]=fsolve(f,y0)