0

I'm developing the code below to check the difference between Euler and Enhanced Euler methods for the function y'= y.

In this case, I see that the more the iterations advance, the greater the difference between the values. Does anyone know why?

def euler_explicit(y_0, h, n):
    steps = [None]*(n+1)
    steps[0] = y_0

    for k in range(1, n+1):
        steps[k] = steps[k-1] + h*steps[k-1]
    return steps

def euler_aprim(y_0, h, n):
    steps = [None]*(n+1)
    steps[0] = y_0

    for k in range(1, n+1):
        steps[k] = steps[k-1] + h*0.5*(steps[k-1] + steps[k-1] + h*steps[k-1])
    return steps

def main():
    t_f = 5
    n = 5
    y_0 = 1
    t_i = 0
    h = (t_f - t_i)/n

    euler_explicit_results = euler_explicit(y_0, h, n)
    
    euler_aprim_results = euler_aprim(y_0, h, n)
    
    for i in range(0, n+1):
        print("Diff in position "+str(i)+" is:"+str(euler_explicit_results[i]-euler_aprim_results[i]))

if __name__ == "__main__":
    main()

My output is:

Diff in position 0 is:0
Diff in position 1 is:-0.5
Diff in position 2 is:-2.25
Diff in position 3 is:-7.625
Diff in position 4 is:-23.0625
Diff in position 5 is:-65.65625
  • 2
    You should rather compare both solutions with exp(t) and re-evaluate your question. – Peter Meisrimel Dec 20 '20 at 19:06
  • 1
    You are comparing `(1+h)^n = 2^n` with `(1+h+h^2/2)^n=2.5^n` which obviously will give different, rapidly diverging results. The second will be closer to `e^n=(2.7182818284...)^n` but still rather different. – Lutz Lehmann Dec 20 '20 at 21:14

0 Answers0