Borrar filtros
Borrar filtros

Using the fzero function

4 visualizaciones (últimos 30 días)
L
L el 23 de Sept. de 2013
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.

Respuestas (2)

Sean de Wolski
Sean de Wolski el 23 de Sept. de 2013
Editada: Sean de Wolski el 23 de Sept. de 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 comentarios
L
L el 23 de Sept. de 2013
Yes I believe the "snapshot" is the issue. I need the program to read in S(1,1) and Ko(1,1) then execute function to find corresponding x. Then go back and read in for S(2,1) and Ko(2,1) then find the corresponding x..etc. All while saving the x values as they are found because I must plot all the x values I obtain for all 65 S and Ko. I was thinking a loop would help but I am unsure. I appreciate your time and help and forgive me if this is trivial.
Sean de Wolski
Sean de Wolski el 24 de Sept. de 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

Iniciar sesión para comentar.


Azzi Abdelmalek
Azzi Abdelmalek el 23 de Sept. de 2013
Editada: Azzi Abdelmalek el 23 de Sept. de 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 )

Categorías

Más información sobre Function Handles en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by