[Science / technical calculation by Python] Numerical solution of first-order ordinary differential equations, initial value problem, numerical calculation

Using numpy, sympy, scipy, Solve the first-order ordinary differential equation under the initial conditions.

problem: $ x'(t) + k x (t) = 0 $, initial condition $ x (0) = 1 $, (k = 1 is a parameter)

The exact solution is $ x (t) = e ^ {-t} $.

from sympy import *      #Imoprt all features from the sympy library
import numpy as np      #import numpy with the name np
from scipy.integrate import odeint 
import matplotlib.pyplot as plt

"""
example:First-order ordinary differential equation: 
dx/dt = -kx x(0)=Solve under the initial condition of 1
"""

x=Symbol('x')                  #letter'x'Is defined as the variable x
y=Symbol('y')                  #letter'y'Is defined as the variable y
t=Symbol('t')                  #letter't'Is defined as the variable y
k=Symbol('k')

def func(x, t, k): # dx/Define dt as a function
    dxdt=-k*x
    return dxdt

k = 1.0 #Set the value to the parameter
x0 = 1.0 #Initial conditions

t=np.linspace(0,10,101) #Time step setting of integration:Divide 0 to 10 into 101 equal parts

sol=odeint(func, x0, t, args=(k,)) #Solve the differential equation numerically. Store the results in a list called sol.

#Exact solution for comparison(e^-t)List excact_Set to sol
exact_sol=[]
tt=np.linspace(0,10,101)
for i in tt:
    exact_sol.append(exp(-1.0*float(i)))
#

    
#Visualization

plt.plot(t, sol, linewidth=1,label='numerical') #Numerical solution
plt.plot(tt, exact_sol, color='red',linewidth=1, label='exact') #True solution

plt.xlabel('t', fontsize=18)
plt.ylabel('x', fontsize=18,rotation='horizontal')
plt.legend(loc='upper right')
plt.show()

result

t.png

Recommended Posts

[Science / technical calculation by Python] Numerical solution of first-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
[Scientific / technical calculation by Python] Solving second-order ordinary differential equations by Numerov method, numerical calculation
[Scientific / technical calculation by Python] Numerical solution of two-dimensional Laplace-Poisson equation by Jacobi method for electrostatic potential, elliptic partial differential equation, boundary value problem
Solve the initial value problem of ordinary differential equations with JModelica
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Science / technical calculation by Python] Comparison of convergence speeds of SOR method, Gauss-Seidel method, and Jacobi method for Laplace equation, partial differential equation, boundary value problem
[Scientific / technical calculation by Python] Solving simultaneous linear equations, numerical calculation, numpy
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
Numerical calculation of differential equations with TensorFlow 2.0
[Scientific / technical calculation by Python] Numerical solution of one-dimensional unsteady heat conduction equation by Crank-Nicholson method (implicit method) and FTCS method (positive solution method), parabolic partial differential equation
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] List of matrices that appear in Hinpan in numerical linear algebra
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
[Scientific / technical calculation by Python] Numerical integration, trapezoidal / Simpson law, numerical calculation, scipy
Numerical analysis of ordinary differential equations with Scipy's odeint and ode
[Science and technology calculation by Python] (Partial) differential, mathematical formula, sympy
Find the numerical solution of the second-order ordinary differential equation with scipy
Solve ordinary differential equations in Python
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
Calculation of technical indicators by TA-Lib and pandas
Initial value problem of NMF (Nonnegative Matrix Factorization)
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
[Python] Numerical calculation of two-dimensional thermal diffusion equation by ADI method (alternate direction implicit method)