Using for and fzero to compute IRR after x years

1 回表示 (過去 30 日間)
Alexandra
Alexandra 2016 年 5 月 18 日
コメント済み: Star Strider 2016 年 5 月 19 日
Hi, here's the code:
for a=1:y
for k=1:n
f = A(k,a)/(1+t(k,a))^(a-1);
B(k,a) = B(k,a) + f(k,a);
fun1 = @(t) B(k,a);
t(k,a+1) = fzero(fun1,0,optimset('display','off'));
So I was trying first to compound the discounted cash flow A that depends n (monte carlo) and on a years. Then, for each n and y I wanted to find the t such that f=0.
What am I doing wrong?
Thanks a lot,

回答 (1 件)

Star Strider
Star Strider 2016 年 5 月 18 日
One problem I see immediately:
fun1 = @(t) B(k,a);
is that ‘B’ is a matrix, and is not a function, and specifically not a function of ‘t’.
I assume you just forgot to include the two necessary end calls when you copied your code.
  2 件のコメント
Alexandra
Alexandra 2016 年 5 月 19 日
編集済み: Star Strider 2016 年 5 月 19 日
Hi, thanks.
Yes, I have the end calls in the code. When I did this code I was thinking that B depends on f, that depends on t. So B depends on t. k and a are just columns and rows numbers, not the variables. I changed it to this:
for a=2:y
for k=1:n
t(k,a-1) = fzero(@(t)B(k,a-1) + A(k,a-1)/(1+t)^(a-2),0,optimset('display','off'));
end
end
Not sure is doing what I hoped for :( The big problem is I need a different function for each a and k. So a different zero (root).
Star Strider
Star Strider 2016 年 5 月 19 日
My pleasure.
That code at least looks like it will work. The value you are solving for (here, ‘t’) has to be stated either explicitly in the function or as an argument to a function called by the fzero anonymous function. Your code appears correct.
You have to decide if it is doing what you want. Plotting the anonymous function argument separately as a function of ‘t’ or plotting the matrix as a surface or contour plot is frequently helpful to see if it is doing what you believe it is doing.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by