File:Instability of Euler's method.svg

Summary

Description
English: This graph shows that the instability of Euler's method when solving the equation . The black curve shows the exact solution, the blue squares the numerical approximation with step size , and the red circles the numerical solution with .
Date
Source Own work
Author Jitse Niesen

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Category:CC-Zero#Instability%20of%20Euler's%20method.svg
Category:Self-published work

Source code

import matplotlib.pyplot as plt
from numpy import linspace, exp
from math import floor

la = -2.3
end_time = 5

exact_times = linspace(0, end_time, 100)
exact_solution = exp(la * exact_times)

euler1_h = 1;
euler1_N = int(floor(end_time / euler1_h))
euler1_times = [ k * euler1_h for k in range(euler1_N + 1) ]
euler1_solution = [ (1 + euler1_h * la) ** k for k in range(euler1_N + 1) ]

euler2_h = 0.7;
euler2_N = int(floor(end_time / euler2_h))
euler2_times = [ k*euler2_h for k in range(euler2_N + 1) ]
euler2_solution = [ (1 + euler2_h * la) ** k for k in range(euler2_N + 1) ]

plt.plot(exact_times, exact_solution, '-k',
         euler1_times, euler1_solution, ':sb',
         euler2_times, euler2_solution, ':or', markersize = 10)

plt.gca().tick_params(labelsize = 20)
plt.savefig('euler-instability.svg')
Category:Euler method Category:SVG created with Matplotlib Category:Images with Python source code
Category:CC-Zero Category:Euler method Category:Images with Python source code Category:SVG created with Matplotlib Category:Self-published work