HP Forums
Difficulties with IRR Program - Printable Version

+- HP Forums (https://archived.hpcalc.org/museumforum)
+-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html)
+--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html)
+--- Thread: Difficulties with IRR Program (/thread-257389.html)



Difficulties with IRR Program - Jose Gonzalez Divasson - 11-30-2013

I cannot get my IRR program to work.

First I have created the Net Present Value program. It uses a list and a discount rate:

EXPORT NPV(L1,R)

BEGIN
LOCAL PV,I,R1;
R1:=R/100;
FOR I FROM SIZE(L1) DOWNTO 2 DO
PV:=(PV+L1(I))/(1+R1);
END;
PV:=PV+L1(1);
RETURN PV;

END;

And it works fine for all cases I have tried - not the ones that will produce errors, of course.

But when using it to create the IRR, it does not work:

First the program


EXPORT IRR(L1)
BEGIN
LOCAL R;
RETURN FNROOT(NPV(L1,R),R,0.1);
END;

Now the message (seems to be at the input):

X IRR({-100,12,12,12,12,112})

Can anyone of you help me?


Re: Difficulties with IRR Program - Michael de Estrada - 11-30-2013

Do you have the two programs under separate catalog names ? To treat the NPV program as a subroutine, it needs to be under the same catalog name and come before the calling program IRR:

IRR_and_NPV

EXPORT NPV()
...
END;

EXPORT IRR()
...
END;

Edited: 30 Nov 2013, 12:12 p.m.


Re: Difficulties with IRR Program - Jose Gonzalez Divasson - 11-30-2013

Many thanks - it may be one of wrong things, but not the one of that problem: once changed, it gives the same error.

Any other guess? Thank you in advance!


Re: Difficulties with IRR Program - Tim Wessman - 11-30-2013

My guess would be using a globally named variable L1 as your function input name. Change it from L1 to somethimg else and see if that makes a difference.


Re: Difficulties with IRR Program - cyrille de Brébisson - 12-02-2013

Hello,

Here is a working program:

EXPORT NPV(l, r)
BEGIN // Using sigma list and makelist is WAY cooler than loops :-)
∑LIST(makelist(l(I)/(1+r)^(I-1), I, 1, SIZE(l)));
END;

// OK, here is the trick there...
// fnroot does NOT carry over local variables from 'above'...
// meaning that using a local variable from the containing function
// in a fnroot call will NOT work (althrough locals can be used
// IN the fnroot call).
// This is why we are creating here a LIST Global variable
// which will be used to contain the list and will work with
// fnroot.
export LIST;
export irr(l)
begin
local r;
LIST:= l;
fnroot(NPV(LIST, r), r, 0.001, 1)*100;
end;

cyrille