Matlab 调用函数时输入出错

Matlab 调用函数时输入出错,matlab,Matlab,我写了一个公式来计算外汇看涨期权的价格,这个公式包含了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 我还写了一个求根算法,在允许的范围和迭

我写了一个公式来计算外汇看涨期权的价格,这个公式包含了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
函数[结果]=伊利诺伊州(f、a、b、yAcc、nlter)
如果符号(f(a))==符号(f(b))
%满足运行算法的初始条件
错误('f(a)和f(b)具有相同的符号')
终止
对于i=1:nlter;
c=b-((f(b)*(b-a))/(f(b)-f(a));

如果abs(f(c))
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)

您好,这就是我得到的>>清除>>syms x store=0.105813579581848 store=0.1058>>f(x)=@(x)fxcalloption(1.2378,0.01,0.005,1,x,store)错误使用sym/subindex(第1558行)索引输入必须是数字、逻辑或“:”。您不需要
syms
命令。只需运行上述四行(在发出
clear
命令之后)。使用erfc输入的错误必须是真实的和完整的。normcdf(第90行)p(todo)=0.5*erfc(-z./sqrt(2))中的误差;fxcalloption(第5行)结果中的错误=spotFXexp(-fit)*normcdf(d1)-kexp(-dit)*normcdf(d2);@(x)fxcalloption中的错误(1.2378,0.01,0.005,1,x-0.1,存储)伊利诺伊州的@(x)(f(x)-g(x)-300)中的错误(第3行)如果符号(f(a))==符号(f(b))