Problem: write a function with several functions inside.

조회 수: 1 (최근 30일)
Francesco
Francesco 2014년 3월 31일
답변: Salaheddin Hosseinzadeh 2014년 3월 31일
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.

답변 (2개)

Salaheddin Hosseinzadeh
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.
  댓글 수: 1
Francesco
Francesco 2014년 3월 31일
first of all thanks for the reply and sorry for the headache, but I wanted to make sure it was clear my purpose :). I'd rather keep that kind of writing, because I have other functions much longer and more complex built that way. According to you, if I write the function in that way, the main one considers those written below?
Thank you again

댓글을 달려면 로그인하십시오.


Salaheddin Hosseinzadeh
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

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by