フィルターのクリア

Where's the bug with my auxiliary function?

3 ビュー (過去 30 日間)
Shawn Miller
Shawn Miller 2015 年 10 月 10 日
コメント済み: Stephen23 2015 年 10 月 10 日
My code is as follows, it includes a main function (FindVolatility) and a local function (selectOutput). The idea here is I'd like to select which price to compute: either call option or put option using blsprice function, and this is executed in the auxiliary function "selectOutput". Next, I'd like to calculate volatility using my auxiliary function and also using fzero function. But I got errors, who knows why? Thanks in advance!
function impVol=FindVolatility(S,K,r,T,q,number,c0)
impVol=fzero(@(x)selectOutput(S,K,r,T,x,q,number)-c0,0);
end
function c=selectOutput(S,K,r,T,sigma,q,number)
[callPrice,putPrice]=blsprice(S,K,r,T,sigma,q);
if number==1
c=callPrice;
else
c=putPrice;
end
end
  2 件のコメント
Stephen23
Stephen23 2015 年 10 月 10 日
編集済み: Stephen23 2015 年 10 月 10 日
What errors are you getting? Please give us the complete error messages (all of the red text), and show us how you are calling the function. Sorry, but our mind-reading abilities are a bit weak today, so you actually need to give us this information in writing.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 10 日
Just like before, you are failing to constrain your input Volatility to be positive. When you use the one-value form of x0 in fzero then fzero is free to try negative numbers (and definitely will try negative numbers when you use 0 as the initial value.) You need to pass x0 as a vector of two elements, like I showed in your previous Question.
  1 件のコメント
Shawn Miller
Shawn Miller 2015 年 10 月 10 日
Thanks again, you're just genius!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFinancial Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by