Help with using the solve function given parameters and three equations

Here is my code for an internal rate of return (IRR)
clear
clc
D1=45000;
g1=0.04;
n1=40; %years
D2=-60000;
g2=0.06;
n2=4; %years
D3=-30000;
g3=0.02;
n3=44; %years
syms i
%Benefits: Positive Cash Flows
P1=D1*(((1-(1+g1)^(n1))*(1+i)^-(n1))/(i-g1));
%Costs: Negative Cash Flows
P2=D2*(((1-(1+g2)^(n2))*(1+i)^-(n2))/(i-g2));
%Costs: Opportunity Cost (Lost)
P3=D3*(((1-(1+g3)^(n1))*(1+i)^-(n3))/(i-g3));
i = solve(P1+P2+P3==0,i)
I should be getting an answer that is equal to 7.44%. Instead it is displaying a 42x1 matrix with random numbers. I am trying to get the three equations added together and set equal to zero in order to find i. Please help! I'm sure it's an easy fix.

 Accepted Answer

It’s probably best not to use the Symbolic Toolbox for this. Using fzero with a slight restatement of your code will get you closer. Your code may have errors I can’t detect, so experiment with this approach:
D1=45000;
g1=0.04;
n1=40; %years
D2=-60000;
g2=0.06;
n2=4; %years
D3=-30000;
g3=0.02;
n3=44; %years
%Benefits: Positive Cash Flows
P1 = @(i) D1*(((1-(1+g1)^(n1))*(1+i)^(-n1))/(i-g1));
%Costs: Negative Cash Flows
P2 = @(i) D2*(((1-(1+g2)^(n2))*(1+i)^(-n2))/(i-g2));
%Costs: Opportunity Cost (Lost)
P3 = @(i) D3*(((1-(1+g3)^(n1))*(1+i)^(-n3))/(i-g3));
i = fzero(@(i) P1(i)+P2(i)+P3(i), 1)
i =
-311.5344e-003
This is not the answer you’re expecting, but it beats getting the roots of a 42-degree polynomial!

4 Comments

I modified my code a little bit to this
D1=45000;
D2=-60000;
D3=-30000;
g1=0.04;
g2=0.06;
g3=0.02;
n1=40; %years
n2=4; %years
n3=44; %years
syms i
P1=D1*((((1-(1+g1)^(n1))*(1+i)^(-n1))/(i-g1)));
P2=D2*((((1-(1+g2)^(n2))*(1+i)^(-n2))/(i-g2)));
P3=D3*((((1-(1+g3)^(n3))*(1+i)^(-n3))/(i-g3)));
i = solve(P1+P2+P3==0, i, 'ReturnConditions', true)
And it is is returning i =
i: [1x1 sym]
parameters: [1x1 sym]
conditions: [1x1 sym]
No longer getting the long polynomial... but this still isn't correct
If it’s not giving you the correct output, check it against the equations you’re coding. We don’t have them, so we can’t check to see if you coded them correctly.
Go back and check your code. When I coded it, I got:
D1=45000;
g1=0.04;
n1=40; %years
D2=-60000;
g2=0.06;
n2=4; %years
D3=-30000;
g3=0.02;
n3=44; %years
P1 = @(i) D1*((1-(1+g1)^n1*(1+i)^-n1)/(i-g1))/(1+i)^4;
P2 = @(i) D2*((1-(1+g2)^n2*(1+i)^-n2)/(i-g2));
P3 = @(i) D3*((1-(1+g3)^n3*(1+i)^-n3)/(i-g3));
int = fzero(@(i) P1(i)+P2(i)+P3(i), 1);
fprintf('\n\tInterest = %.4f%%\n', int*100)
Interest = 7.4445%

Sign in to comment.

More Answers (1)

With those powers of n1 and n3 being 40 and 44, it is not surprising that the solution is a polynomial of about that order. All of those values are mathematical solutions to the equations you set out.
Cutting short the explanations: use
syms i positive

2 Comments

Warning: The solutions are parameterized by the symbols: z1.
To include parameters and conditions in the solution, specify
the 'ReturnConditions' option.
> In solve>warnIfParams (line 500)
In solve (line 356)
In Rorys_IRR (line 24)
Warning: The solutions are valid under the following
conditions: (825432267338751673*z1)/1477613924061677500 -
(2193686941466940022*z1^2)/369403481015419375 -
(13734013560400584693*z1^3)/369403481015419375 +
(41923794702955407671*z1^4)/211087703437382500 +
(257549755141161105337*z1^5)/52771925859345625 +
(4061174807376957043163*z1^6)/73880696203083875 +
(55327506*z1^7)/125 + (695322537*z1^8)/250 +
(3562007501*z1^9)/250 + (38235300389*z1^10)/625 +
(28053586184*z1^11)/125 + (89177740548*z1^12)/125 +
(248277101188*z1^13)/125 + (610428206384*z1^14)/125 +
(6670617103528*z1^15)/625 + (521092270449*z1^16)/25 +
(913130121501*z1^17)/25 + (1440471578286*z1^18)/25 +
(2050864725468*z1^19)/25 + (13201699314882*z1^20)/125 +
(3077916064938*z1^21)/25 + (3251507213136*z1^22)/25 +
(3113764464876*z1^23)/25 + (2702665018833*z1^24)/25 +
(53120895610653*z1^25)/625 + (7557143311314*z1^26)/125 +
(4855319480488*z1^27)/125 + (2811175743748*z1^28)/125 +
(1462482639044*z1^29)/125 + (3405514701264*z1^30)/625 +
(282645069928*z1^31)/125 + (207828929737*z1^32)/250 +
(67209878437*z1^33)/250 + (9473642763*z1^34)/125 +
(11508614754*z1^35)/625 + (474871579*z1^36)/125 +
(81566563*z1^37)/125 + (11349728*z1^38)/125 +
(1229154*z1^39)/125 + (972001*z1^40)/1250 + (1997*z1^41)/50 +
z1^42 - 991517786144719/147761392406167750 == 0 & 0 < z1.
To include parameters and conditions in the solution, specify
the 'ReturnConditions' option.
> In solve>warnIfParams (line 507)
In solve (line 356)
In Rorys_IRR (line 24)
Is what it came back with

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!