Python:Ordinary Differential Equations/Templates

From PrattWiki
Jump to navigation Jump to search

The codes below present templates for creating the function file responsible for computing values of the first derivatives of all the variables and the script whose job is to solve a system of initial value problems based on ordinary differential equations.

Code

# %% Imports
import numpy as np
from scipy.integrate import solve_ivp

# %% Define derivative function
def f(t, y, c):
    dydt = [ ]  # list of derivatives as functions of t, y, and c
    return dydt

# %% Define time spans, initial values, and constants
tspan = np.linspace( )  # start, finish, numpoints
yinit = [ ]             # list of initial values
c = [ ]                 # list of constants

#%% Solve differential equation
sol = solve_ivp(lambda t, y: f(t, y, c), 
                [tspan[0], tspan[-1]], yinit, t_eval=tspan, rtol = 1e-5)

Questions

Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.

External Links

References