MATLAB: Fzero Solving for the same value

fzero

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!

Best Answer

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.