Problem: write a function with several functions inside.
1 次查看(过去 30 天)
显示 更早的评论
Hi everyone, i've some problem with this function:
if true
function call = callBSMcfPROVA (S0,K,T,r,q,v0)
vP1 = 0.5 + 1/ pi * quad (@P1,0,200,[],[],S0,K,T,r,q,v0);
vP2 = 0.5 + 1/ pi * quad (@P2,0,200,[],[],S0,K,T,r,q,v0);
call = exp (-q * T ) * S0 * vP1 - exp (-r * T ) * K * vP2 ;
end
function p = P1(om,S0,K,T,r,q,v0)
p = real ( exp (-1i* log (K)*om) .* cfBSM (om -1i,S0,T ,r,q,v0) ./ ...
(1i * om * S0 * exp ((r-q) * T )));
end
function p = P2(om,S0,K,T,r,q,v0)
p = real ( exp (-1i* log (K)*om) .* cfBSM (om ,S0,T ,r,q,v0) ./ ...
(1i * om));
end
function cf = cfBSM (om,S0,T,r,q,v0)
cf = exp (1i * om * log (S0) + 1i * T * (r - q) * om - 0.5 * T * ...
v0 * (1i * om + om .^ 2));
end % code
end
I saved all of that into an m-file named callBSMcfPROVA.m . my intention was to make sure that the first function depends on those written below. Let me explain: I want this function to calculate the value of one written last, take this result and put into to calculate P1 and P2 (the second and the third), so that these results are inserted into the main one to calculate "call". My question is: written in this way, I get the result I want? I'm not sure of that because i get some strange results.
I hope someone could help me! thank you very much.
0 个评论
回答(2 个)
Salaheddin Hosseinzadeh
2014-3-31
First I suggest you to write your functions in a different form, which you may find it way easier to work with.
Anonynous function, I think by doing that you would be able to find your way around.
Salaheddin Hosseinzadeh
2014-3-31
You can definitely use functions inside the functions.
I just did it couple of weeks ago, and I defined the functions as anonymous functions, later I used them to find theor zero and ...
And U don't need to save each funtion as a file!
Take a look at them that might help, it's basicly (Px o Beta_x o Vx)(t) or Px(Beta_x(Vx(t)))
Vx=@(t) A*sin(2*pi*fc*t);
Vy=@(t) A*cos(2*pi*fc*t);
%%Deviation angle inrespect to X & Y axis as a function of applied voltage and
% intial deviation
Beta_x=@(t) Vx(t)*Mu + Theta_x;
Beta_y=@(t) Vy(t)*Mu + Theta_y;
Px=@(t) G*tan(Beta_x(t));
Py=@(t) G*tan(Beta_y(t));
i=0;
p0_cal=linspace(0+1/Fs,t(end),4*t(end)/(1/fc));
for j=1:length(p0_cal)
i=i+1;
tempx=@(t) Px(t)-G*tan(Theta_x);
[t0x(i),valx(i)]=fzero(tempx,p0_cal(j));
tempy=@(t) Py(t)-G*tan(Theta_y);
[t0y(i),valy(i)]=fzero(tempy,p0_cal(j));
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!