solve non linear equation

2 views (last 30 days)
john birt
john birt on 22 Mar 2012
ok so I have a non linear function that I want to solve for 'x', one equation and one unknown variable 'x'.
I have a function Black_Imp_Vol.m of the variable 'x'.
function F = Black_Imp_Vol(x)
F = exp(-r*t)*(P*normcdf(((log(P/K)+t*x^2/2)/(x*sqrt(t))),0,1)-K*normcdf(((log(P/K)-t*x^2/2)/(x*sqrt(t))),0,1))-C ;
Now my code
clear;
r=0.01;
P=1.4334;
K=1.46;
C=0.0230;
t=0.26;
x0 = 0.3;
options=optimset('Display','iter');
[x,fval] = fsolve(@Black_Imp_Vol,x0,options)
will solve for 'x' using fsolve. Great!
However the above hard codes in the known variables r, P, K, C and t
I want to read the known variables r,P,K,C and t, from a .mat file where there are stored in a matrix 'q' a 150 by 5 matrix, each row hold one set of the known parameters and I want to call this fsolve row by row and compute 'x'.
So I coded this
clear;
load eurofx146_c_a_3.mat;
r=q(:,4);
P=q(:,2);
K=q(:,5);
C=q(:,1);
t=q(:,3);
n=size(q);
N=n(1);
s0 = 0.3;
for i=1:N
x(i) = fsolve(@Black_Imp_Vol,x0);
end
But I get an error
??? Undefined function or variable 'r'.
Error in ==> Black_Imp_Vol at 2
F = exp(-r*t)*(P*normcdf(((log(P/K)+t*x^2/2)/(x*sqrt(t))),0,1)-K*normcdf(((log(P/K)-t*x^2/2)/(x*sqrt(t))),0,1))-C ;
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Error in ==> black2 at 15
x(i) = fsolve(@Black_Imp_Vol,x0);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
anyone have an idea about how I can do this?
NOTE: this is NOT a simultaneous equation, is one equation, one unknown. just repeated for each data row.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 22 Mar 2012
I'd change the definition of your function Black_Imp_Vol to:
function F = Black_Imp_Vol(x,r,t,P,K,C)
F = exp(-r*t)*(P*normcdf(((log(P/K)+t*x^2/2)/(x*sqrt(t))),0,1)-K*normcdf(((log(P/K)-t*x^2/2)/(x*sqrt(t))),0,1))-C ;
...and then call fzero in a loop:
for i1=1:N
x(i1) = fsolve(@(x) Black_Imp_Vol(x,r(i1),t(i1),P(i1),K(i1),C(i1)),x0);
end
HTH

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!