Different results between ode and using python

66 Views Asked by At

I found an example online in solving first order ode (2nd example) and decided to see if I could use Python to solve it. Here's the problem statement:

"You just won the lottery. You put your \$5,000,000 in winnings into a fund that has a rate of return of 4%. Each year you use \$300,000. How much money will you have twenty years from now?"

The answer from the website is: $$ x = -2,500,000 \,e^{t/25} + 7,500,000 = 1,936,148 $$

Using Python here's my simple code:

#total = 5,000,000
def calc(total, years):
    for year in range(1, years):
        #total after 300,000 deduction
        total = total  - 300000
        #total after interest rate of 4%
        total = total * 1.04
    print("Final amount: " + str(total))

The result from python is about 1,900,822, which is about 35,325 difference. My question is, why there's a big gap? Did I program it wrong or is the method only an approximation and not an accurate answer?