Internal Rate of Return - Code
显示 更早的评论
Hi, I'm trying to write a iteration- loop to calculate te internal rate of reuturn (IRR) (without the financial toolbox).
Problems are: how to set the while loop
how to set the iteration steps
solution so far: IRR= -Inf -> why -Inf ?
My code so far:
zaeler=1; %% counter
w(1)=10;
w(2)=0;
IRR=0.05;
IRR1=0.1;
IRR2=0.2;
while zaeler<10000 %% better would be: while interval by 0
for jahr=1:1:25
s(jahr) = (cashflow(jahr)/((1+IRR)^jahr));
end
w(zaeler) = sum(s)-invest ; %% w should be 0
if w(zaeler)>0
IRR=IRR1-(w(1)/(w(2)-w(1)))*(IRR2-IRR1);
IRR1=IRR;
elseif w(zaeler)<0
IRR=IRR1-(w(1)/(w(2)-w(1)))*(IRR2-IRR1);
IRR2=IRR;
end
zaeler=zaeler+1;
end
Thank you in advance for your ideas and help.
3 个评论
Rik
2021-5-11
Did you step through your code with the debugger to see when the IRR becomes -inf?
Since you know the number of iterations: why aren't you using a for-loop instead?
Are you sure w will never be 0? Because neither branch will execute in that case, leaving IRR the same for the remaining iterations. Don't you want to put a break statement there?
MK
2021-5-11
Rik
2021-5-11
You should step through your code and closely look at what values your variables have after each line.
And why did you write for iteration<10000? It does something, but I expect you want for iteration=1:10000 instead (and remove iteration=iteration+1;).
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!