Page 1 of 1

Varargin

Posted: November 19th, 2008, 7:59 pm
by Bazman2
Hi,I have the following function:function val = MyFalsePosition(f,a,b,prec,varargin)if nargin==4 sfa=sign(f(a)); sfb=sign(f(b));else sfa = sign(f(a,varargin{:})); sfb = sign(f(b,varargin{:}));enditer=0;if sfa == 0 val= a; elseif sfb == 0 val= b; elseif (sfa*sfb)>0 disp ('wrong starting values'); else m=b; mlast=a; d=abs((f(m,varargin{:})/(f(m,varargin{:})-f(mlast,varargin{:})))*(m-mlast)); while (d>prec) iter=iter+1; mlast=m; m=(a*f(b,varargin{:})- b*f(a,varargin{:}))/(f(b,varargin{:})-f(a,varargin{:})); if f(m,varargin{:})==0 d=0; else d=abs(f(m,varargin{:})/f(m,varargin{:})-f(mlast,varargin{:}))*(m-mlast); if sign(f(m,varargin{:})) ==sfa a=m; else b=m; end end disp(sprintf('iteration %5.0f',iter)); disp(sprintf('a %5.0f',a)); disp(sprintf('b) %5.0f',b)); disp(sprintf('error %5.0f',d)); end val= m;endendfunction val =BS_European_Call(S, K, sigma, r, T);%useage BS_European_Call(S,K,sigma,r,T)%European call option value in the Black-Scholes modeld1 = (log(S./K)+(r+sigma^2/2)*T)/(sigma*sqrt(T));d2 = (log(S./K)+(r-sigma^2/2)*T)/(sigma*sqrt(T));val=S.*mynormcdf(d1)-K*exp(-r*T)*mynormcdf(d2);I am confident that both function work individually.I want to call BS_European_call into MyFalsePosition to find the value it equals zero for by varying the input sigma in BS_european_Call.How should I type this in to the command window?I tried:MyFalsePosition(BS_European_Call,0,1,0.01,40,40,x,0.1,0.5)but that did not work?

Varargin

Posted: November 22nd, 2008, 12:26 pm
by aiQUANT
I dont understand what the hell you want to do. Do you want MyFalsePosition to process the output from BS_European_Call? And also in the call MyFalsePosition(BS_European_Call,0,1,0.01,40,40,x,0.1,0.5) matlab will think BS_European_Call is a workspace variable and not a function pointer. Use '@' sign to denote function pointer. But still i'm confused.