Modules Python utilisés dans ce notebook :
In [81]:
import math
import numpy as np
import sympy as sym
import matplotlib.pyplot as plt
from matplotlib import animation
import seaborn as sns
from JSAnimation import IPython_display
%matplotlib inline
sns.set_context('notebook')
sns.set_palette('winter')
sym.init_printing()
Les équations aux dérivées partielles (EDP) telles que l'équation de diffusion ou le modèle d'érosion fluviatile stream-power (équation d'advection) peuvent avoir une solution analytique, mais pour un nombre de limité de cas simples. Dans beaucoup de cas intéressants à étudier, il est difficile voire impossible de trouver une solution analytique. On a alors recours à des méthodes numériques pour résoudre ces équations.
Ces méthodes numériques sont basées sur la discrétisation du continuum espace-temps qu'on étudie. Pour l'espace, on parle de maillage (grille régulière ou irrégulière) et pour le temps on emploie le terme pas de temps. Pour chaque élément de cette discrétisation, on calcule une approximation de la solution des EDPs.
In [125]:
# sympy function
z_xs = lambda xs: sym.exp(xs)
# truncate order
trunc_order = 3
# ----------------------
def truncated_taylor_expr(func, order=3):
"""
Return the sympy expression of the Taylor
expansion of a given function, truncated at
a given order.
"""
# define the symbols
xs, dxs = sym.symbols('x Delta_x')
# calculate the truncated expression of the Taylor series
taylor_expr = func(xs)
for n in range(1, order):
taylor_expr += dxs**n / sym.factorial(n) * sym.diff(func(xs), xs, n)
taylor_expr += sym.O(dxs**order)
return taylor_expr
truncated_taylor_expr(z_xs, order=trunc_order)
Out[125]:
In [121]:
# sympy function
z_xs = lambda xs: sym.exp(xs)
# x0
x0 = 0
# x-axis and y-axis limits
x_limits = -3., 3.
y_limits = -5., 20.
# ----------------------
# init data
xs = sym.symbols('x')
z_xs_func = z_xs(xs)
z_func = sym.lambdify(xs, z_xs_func, modules=['numpy'])
x = np.linspace(*x_limits, num=200)
# init figure
fig, ax = plt.subplots()
line1, = ax.plot([], [], c='r', lw=2)
line2, = ax.plot([], [], c='b', lw=2)
point = ax.plot(x0, z_func(x0), c='r',
marker='o', ms=8, zorder=5)
nlabel = plt.text(0.8, 0.9, '',
transform=ax.transAxes,
fontsize=14)
ax.hlines(0, *x_limits, lw=0.5)
ax.vlines(0, *y_limits, lw=0.5)
plt.setp(ax,
xlim=x_limits,
ylim=y_limits,
xlabel='$x$',
ylabel='$z(x)$')
# animation
def init():
line1.set_data(x, z_func(x))
return line1,
def animate(order):
truncated_taylor = z_xs_func.series(xs, x0=x0, n=order+1).removeO()
# specific handling if the nth-order truncated series
# is a constant
if isinstance(truncated_taylor, sym.numbers.Number):
z = np.zeros_like(x)
z.fill(truncated_taylor.evalf())
else:
z_func_trunc = sym.lambdify(xs, truncated_taylor,
modules=['numpy'])
z = z_func_trunc(x - x0)
line2.set_data(x, z)
nlabel.set_text('$n$ = {}'.format(order))
return line2, nlabel
animation.FuncAnimation(fig, animate, init_func=init,
frames=range(0, 11), interval=800,
blit=True)
Out[121]:
In [ ]: