Explain code to me well found in the program I am new here

1 view (last 30 days)
function irr = CalculateIRR(initial_outlay, cash_flow, T)
% CalculateIRR is a function to calculate Internal Rate of Return (IRR)
% Input/s:
% initial_outlay : Initial outlay ( scalar value )
% cash_flow : a vector of Cash flow corresponding to each year
% T : Total period
% Output/s:
% irr : Calculate value of internal rate of return
f = @(irr) -1*initial_outlay;
for t = 1:T
f = @(irr) f(irr)+cash_flow(t)./(1+irr).^t;
end
irr = fzero(f,0)*100; % Computing the root of the function
end
% Explain it to me very well the use or function of the following the following
%1.f = @(irr) -1*initial_outlay
%2.t = 1:T
%3.f = @(irr) f(irr)+cash_flow(t)./(1+irr).^t
%4.irr = fzero(f,0)*100
  3 Comments
Rik
Rik on 18 Jul 2021
What is your question? What exactly is unclear to you? Without knowing what level to start, it is impossible to give an explanation. Do we need to explain what multiplication is? What a variable is? What an assignment is? What memory model an data types Matlab uses?
Stephen Ken Lantapon
Stephen Ken Lantapon on 19 Jul 2021
% Explain it to me very well the use or function of the following the following
%1.f = @(irr) -1*initial_outlay
%2.t = 1:T
%3.f = @(irr) f(irr)+cash_flow(t)./(1+irr).^t
%4.irr = fzero(f,0)*100

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 19 Jul 2021
See the documentation for anonymous functions, the colon operator, and the fzero function.
doc colon
doc fzero

Walter Roberson
Walter Roberson on 19 Jul 2021
f = @(irr) -1*initial_outlay;
That initializes f to be a function handle that ignores its input parameter and returns the constant value -1*initial_outlay . This code has a bug, and should instead be
f = @(irr) -ones(size(irr))*initial_outlay;
Then
for t = 1:T
loop. Each iteration, t will be assigned the next positive integer out of 1, 2, 3, ... up to T
f = @(irr) f(irr)+cash_flow(t)./(1+irr).^t;
This redefines f to be a new function handle. The new function handle first calls the previous function handle with the input argument; then it uses the current value of the for loop variable t in order to look up the next value in the variable cash_flow and divide that value by (1+irr) raised to the power of the current value of the for loop variable t; that ratio is added to what was calculated from f(irr)
It is important for this point to understand that the old function handle f does not disappear when f is assigned to here. Instead, due to the way that anonymous functions are handled, the old f gets stored into the anonymous function. And that old handle in turn has a copy of the previous function handle, and that has a copy of the one before that, and so on.
The effect is similar to as if we had used
f{1} = @(irr) -1*initial_outlay;
for t = 1:T
f{t+1} = @(irr) f{t}(irr)+cash_flow(t)./(1+irr).^t;
end
so the fourth function handle, f{4} first calls the third function handle, f{3}, and the first thing the third handle does is invoke the second handle, f{2}, and the first thing the second handle f{2} does is invoke the first handle... but this time the first handle does not call anything prior and caclulates the fixed value instead. Then, returning back through the chain, the second handle adds a value based upon power 2 of a division, and returning back from calling the second handle, the third handle adds a value based upon power 3 of a division, and so on until eventually the entire chain is finished.
Nesting function handles like this, having each one calling the one before, is inefficient (and confusing.) MATLAB is not known to have any internal optimizations for "head recursion" (there are techniques for automatically rewriting such things into looping). I do not recommend this process.

Tags

Community Treasure Hunt

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

Start Hunting!