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!