File:Forward and backward Euler method.png

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Forward_and_backward_Euler_method.png(800 × 600 pixels, file size: 36 KB, MIME type: image/png)

Captions

Captions

Add a one-line explanation of what this file represents

Summary

[edit]
Description
English: Comparison of the Forward and Backward Euler methods applied to the differential equation , on the interval .
Date
Source Own work
Author Pred

Python source code

[edit]
import numpy as np
import matplotlib.pyplot as plt

a = float(5)   # Maximal value of the domain
n = float(30)  # Number of divisions
dt = a/n       # Size of each subdivision

t1 = np.arange(0, a, 0.01)  # For plotting the analytical solution
t2 = np.arange(0, a, dt)    # For plotting the approximations

# The following function solves y'(t) = -y(t)^2 and y(0) = 1
def y(t):
    return 1/(1+t)

# Our approximations will simply be given as an array of approximate values
forward_euler = [1]   # The initial condition
backward_euler = [1]  # is the same for both approximations

for k in range(len(t2)-1):
    forward_euler.append(forward_euler[-1] - dt*forward_euler[-1]**2)
    backward_euler.append((-1 + np.sqrt(1+4*dt*backward_euler[-1])) / (2*dt))

# Plotting time
plt.plot(t1, y(t1), 'r-', label='Analytical solution')
plt.plot(t2, forward_euler, 'bs', label='Forward Euler method')
plt.plot(t2, backward_euler, 'g^', label='Backward Euler method')
plt.legend()
plt.show()

Licensing

[edit]
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.

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current14:53, 8 October 2015Thumbnail for version as of 14:53, 8 October 2015800 × 600 (36 KB)Pred (talk | contribs)User created page with UploadWizard

There are no pages that use this file.

Metadata