Internal Rate of Return - Code
Show older comments
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 Comments
Rik
on 11 May 2021
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
on 11 May 2021
Rik
on 11 May 2021
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;).
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!