Fzero Solving for the same value

1 vue (au cours des 30 derniers jours)
Oscar de la Torre
Oscar de la Torre le 28 Mar 2019
Hi,
I'm trying to use fzero to find the implied asset value of a firm. My function to calculate the asset value is:
Function F = myKMV1(x,st,vol,D,r)
x = x^2 ;%used to prevent negative numbers
d1 = log(x/D)+r+0.5*(vol*vol)/vol ;
d2 = d1 - vol ;
F = st - x*normcdf(d1)+D*exp(-r)*normcdf(d2);
I have to iterate through a vector with different share prices and for each share price there should be a different implied asset value. However, fzero always gives the same result even though the share price is changing? The code running the function is as follows:
fun = @(x) myKMV1(x, st1, vol1,D11, r1)
volNew = 1;
vol1 = AssetVolatility; %this is equal to 0.0582
D11 = D1; %this is equal to 88500000
r1 = UkGilt; % this is equal to 0.0185
while abs(vol1 - volNew) > 0.00001
vol1 = VolNew ;
for n = 1:252
st1 = st(n);
x(n) = fzero(fun, 1000)
end
end
Thanks for any help in advance!
  2 commentaires
Walter Roberson
Walter Roberson le 28 Mar 2019
You did not indicate what problem you are observing?
To test we need values for st1, vol1, D11, r1
Oscar de la Torre
Oscar de la Torre le 28 Mar 2019
The problem I'm observing is that the value fzero is solving for is the same even though st1 changes. st1 is a vector of values of which the first five are: [10757700, 10757700, 108652700, 112955850], vol1 = 0.0582, D11 = 88500000, and r1 = 0.0185. So because only the values of st1 change the values calculated by fzero, i.e. x, should change but with the code above the answer provided by fzero is constant.
Hope this clarifies,
Thanks!

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Mar 2019
When you define
fun = @(x)myKMV1(x,st1,vol1,D11,r1)
then at the time that that statement is executed (defining the function handle for assignment to fun) then the a copy is taken of the values of st1, vol1, D11, r1, and those values are saved along with the function handle. No matter how you might change those variables after that, the recorded values are brought back into memory for use by the anonymous function.
You should move the definition of fun to between the assignment to st1 and the use of fun in the fzero call.
  1 commentaire
Oscar de la Torre
Oscar de la Torre le 29 Mar 2019
It works! Thank you so much. I really appreciate it I've spent days working on it without realising it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by