Last active
February 11, 2020 12:32
-
-
Save IamPhytan/5ab87832e5dc65bbebafbf60ab9477db to your computer and use it in GitHub Desktop.
ODE Examples in Sympy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sympy as sym | |
import numpy as np | |
import matplotlib.pyplot as plt | |
sym.init_printing() | |
# Integral calculation constants | |
a = 0 | |
b = 20 | |
h = 0.4 | |
# Variables | |
t = sym.symbols('t') | |
y = sym.Function('y') | |
# Initial conditions | |
ics = {y(0): 1} | |
# Equation hands | |
left_hand = sym.Derivative(y(t), t) | |
right_hand = -5 * y(t) | |
eq = sym.Eq(left_hand, right_hand) | |
print(sym.pretty(eq)) | |
# Solve equation | |
sol = sym.dsolve(eq, y(t), ics=ics) | |
print(sym.pretty(sol)) | |
# Transform into function | |
fun_y = sym.lambdify(t, sol.rhs, modules=['numpy']) | |
print(fun_y(0), fun_y(1)) | |
# Séries de données | |
t = np.arange(a, b + h, h) | |
y = fun_y(t) | |
print(t, y) | |
# Graphique de y(t) | |
plt.plot(t, y, color='b') | |
plt.xlim(0, 20) | |
plt.show() | |
# ================================== Ex 2 | |
x = sym.var('x') | |
f = sym.Function('f') | |
diffeq = sym.Eq(sym.Derivative(f(x), x), x + f(x) / 5) | |
ics = {f(0): -3} | |
sol2 = sym.dsolve(diffeq, f(x), ics=ics).rhs | |
print(sym.pretty(sym.simplify(sol2))) | |
# ================================== Ex 3 | |
f_2 = sym.Function('f_2') | |
x_2 = sym.var('x_2') | |
A = sym.var("A") | |
ics = {f_2(0): A} | |
sol3 = sym.dsolve(f_2(x_2).diff(x_2) - f_2(x_2), f_2(x_2), ics=ics) | |
print(sym.pretty(sol3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment