2014-02-24 90 views
0

我寫的公式計算,取入6個不同參數的FX看漲期權的價格,錯誤與輸入調用函數時

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 

,我也寫這解決了該方程根求解算法F = 0從範圍AB可允許的範圍內,並且迭代

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 

回答

0

syms x使用符號數學工具箱創建一個符號變量x。但是,如果我正確地理解了你,你想要在數字上解決方程而不是象徵性的。請嘗試使用功能手柄f(x)g(x)。所以,你的代碼可能看起來像

store=0.105813579581848 
[email protected](x) fxcalloption(1.2378,0.01,0.005,1,x,store) 
[email protected](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) 
+0

嗨,這是我得到 >>明確 >> SYMS X 店= 0.105813579581848 店= 0.1058 >> F(X)= @ (x)fxcalloption(1.2378,0.01,0.005,1,x,存儲) 使用sym/subsindex時出錯(第1558行) 索引輸入必須是數字,邏輯或':'。 –

+0

你不需要'syms'命令。只需運行上面四行(發出'clear'命令後)。 – munichgrizzly

+0

錯誤使用erfc 輸入必須真實且完整。 normcdf中的錯誤(第90行) p(todo)= 0.5 * erfc(-z ./ sqrt(2)); fxcalloption中的錯誤(第5行) result = spotFX * exp(-fi * t)* normcdf(d1)-k * exp(-di * t)* normcdf(d2); 錯誤@(X)fxcalloption(1.2378,0.01,0.005,1,X-0.1,商店) 錯誤@(X)(F(X)-g(x)的-300) 錯誤在伊利諾伊州(第3行) if sign(f(a))== sign(f(b)) –