Using the fzero function
5 views (last 30 days)
Show older comments
I am using the fzero function to solve my equation : exp(-r*(Ts-t))*P*(Kt*Nd1-Ko*Nd2)-S . I have successfully used fzero to solve for a single case but I must vary the variables Ko and S. I have a vector of values of Ko that correspond to S.
This is my entire code if it can be of help:
function [m] = mzero(x)
S=0.0098;Ko=75;
r=0.1;Ts=1;t=0;P=11.37199463;Kt=44.74239971;
d1=(log(Kt/Ko)+((x^2)/2)*(Ts-t))/(x*sqrt(Ts-t));
d2=d1-x*sqrt(Ts-t);
Nd1=normcdf(d1);
Nd2=normcdf(d2);
m= exp(-r*(Ts-t))*P*(Kt*Nd1-Ko*Nd2)-S;
end
After I save that function I type in the command :
>>x0=[-1 1];x=fzero(@mzero,x0)
x =
0.1544
My issue is how to call in values to S and Ko that I have as vectors both of size (65,1) and save the values of x that I find using fzero to the corresponding S and Ko.
0 Comments
Answers (2)
Sean de Wolski
on 23 Sep 2013
Edited: Sean de Wolski
on 23 Sep 2013
Change the anonymous function to something like the following:
Ko = 92;
S = .11;
x = fzero(@(xx)mzero(xx,S,Ko),x0);
Then change the function signature to
function m = mzero(x,S,Ko)
What is happening here is that S and Ko are snapshotted when you call fzero. Thus when you change them above, that will be reflected in the creation of the anonymous function and they'll be passed into mzero.
2 Comments
Sean de Wolski
on 24 Sep 2013
So do the above in a for-loop
for ii = 1:10
x(ii) = fzero(@(xx),S(ii),Ko(ii),x0)
end
Now on each iteration the snapshot will be taken again with the current value of S and Ko
Azzi Abdelmalek
on 23 Sep 2013
Edited: Azzi Abdelmalek
on 23 Sep 2013
Declare Ko and S as global
function [m] = mzero(x)
global Ko S
r=0.1;
Ts=1;
t=0;
...
call your function
global Ko S
S=0.0098;
Ko=75;
x0=[-1 1];
x=fzero(@mzero,x0 )
0 Comments
See Also
Categories
Find more on Function Handles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!