调用函数时输入错误

时间:2014-02-24 15:03:59

标签: matlab

我写了一个公式来计算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

我还写了一个根求解算法,它解决了允许范围和迭代范围内a-b范围内的方程f = 0

function [ result ] = illinois( f,a,b , yAcc,nlter)

现在,我有以下代码,

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)

但是我有以下错误,

Error using NaN
Trailing string input must be 'single' or 'double'.

Error in normcdf (line 60)
p = NaN(size(z),class(z));
然后我尝试通过尝试

进行调试
f(x)=fxcalloption(1.2378,0.01,0.005,x,2,store)

我有同样的错误,所以我该如何绕过这个?

我必须利用伊利诺伊功能

如果有兴趣,我的伊利诺伊功能如下:

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 个答案:

答案 0 :(得分:0)

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)