I wrote a formula that computes the price of an fx call option that takes in 6 different arguments,
我写了一个公式计算一个fx看涨期权的价格它包含6个不同的参数,
function [ result ] = fxcalloption( spotFX,di,fi,t,k,v )
d1=(log(spotFX/k)+(di-fi+0.5*(v^2))*t)/(v*sqrt(t));
d2=d1-v*sqrt(t);
result=spotFX*exp(-fi*t)*normcdf(d1)-k*exp(-di*t)*normcdf(d2);
end
and i also wrote a root solving algorithm which solves for the equation f=0 from range a-b within an allowable range and iterations
我还编写了一个根解决算法,它解决了在允许范围和迭代范围内从范围a-b的等式f=0。
function [ result ] = illinois( f,a,b , yAcc,nlter)
now, i have the following codes,
现在,我有下面的代码,
syms x
store=0.105813579581848
f(x)=fxcalloption(1.2378,0.01,0.005,1,x,store )
g(x)=fxcalloption(1.2378,0.01,0.005,1,x-0.1,store )
illinois(f-g-300,0,1000,0.001,1000000)
However i have the following error instead,
但是我有以下错误,
Error using NaN
Trailing string input must be 'single' or 'double'.
Error in normcdf (line 60)
p = NaN(size(z),class(z));
i then tried to debug, by trying
然后我试着调试。
f(x)=fxcalloption(1.2378,0.01,0.005,x,2,store)
and i have the same error, so how can i bypass this?
我也有同样的错误,怎么绕过这个呢?
I must make use of the illinois function
我必须利用伊利诺斯州的功能。
if for any interest, my illinois function is as follows:
如果有兴趣,我的伊利诺斯州的功能如下:
function [ result ] = illinois( f,a,b , yAcc,nlter)
if sign(f(a))==sign(f(b))
%to satisify the initial condition of running the algorthim
error('f(a) and f(b) have the same sign')
end
for i=1:nlter;
c=b-( (f(b)*(b-a))/(f(b)-f(a)));
if abs(f(c))<yAcc;
result=c;
break %it satisifys the accuracy
elseif i==nlter;
error('max nlter reached')
else
if sign(f(c))==sign(f(a))
a=c;
else
b=c;
end
end
end
1 个解决方案
#1
0
syms x
creates a symbolic variable x
using the Symbolic Math Toolbox. But if I understand you correctly, you want to solve the equation numerically not symbollically. Instead, try to use function handles for the functions f(x)
and g(x)
. So your code could look like
syms x使用符号数学工具箱创建一个符号变量x。但是如果我理解正确的话,你想要用数字的方式来解这个方程。相反,尝试使用函数处理函数f(x)和g(x)。代码看起来是这样的。
store=0.105813579581848
f=@(x) fxcalloption(1.2378,0.01,0.005,1,x,store )
g=@(x) fxcalloption(1.2378,0.01,0.005,1,x-0.1,store )
illinois(@(x)(f(x)-g(x)-300),0,1000,0.001,1000000)
#1
0
syms x
creates a symbolic variable x
using the Symbolic Math Toolbox. But if I understand you correctly, you want to solve the equation numerically not symbollically. Instead, try to use function handles for the functions f(x)
and g(x)
. So your code could look like
syms x使用符号数学工具箱创建一个符号变量x。但是如果我理解正确的话,你想要用数字的方式来解这个方程。相反,尝试使用函数处理函数f(x)和g(x)。代码看起来是这样的。
store=0.105813579581848
f=@(x) fxcalloption(1.2378,0.01,0.005,1,x,store )
g=@(x) fxcalloption(1.2378,0.01,0.005,1,x-0.1,store )
illinois(@(x)(f(x)-g(x)-300),0,1000,0.001,1000000)