Difference between revisions of "Python:Ordinary Differential Equations/Templates"

From PrattWiki
Jump to navigation Jump to search
 
(Code)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
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 [[Python:Ordinary Differential Equations|ordinary differential equations]].  This program calls the <code>StatePlotter</code> program, available at the bottom.
+
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 [[Python:Ordinary Differential Equations|ordinary differential equations]].
  
 
== Code ==
 
== Code ==
 
<syntaxhighlight lang='python'>
 
<syntaxhighlight lang='python'>
#%% Imports
+
# %% Imports
 
import numpy as np
 
import numpy as np
import matplotlib.pyplot as plt
 
 
from scipy.integrate import solve_ivp
 
from scipy.integrate import solve_ivp
  
#%% Define independent function and derivative function
+
# %% Define derivative function
def x(t):
 
    return # Independent function here
 
 
 
 
def f(t, y, c):
 
def f(t, y, c):
'''
+
     dydt = [ ]  # list of derivatives as functions of t, y, and c
Template for calculating first derivatives of state variables
 
t is time
 
y is the state vector
 
c contains any required constants
 
dydt must be a list or an array
 
'''
 
     dydt = # list or array of first derivatives here
 
 
     return dydt
 
     return dydt
   
+
 
#%% Define time spans, initial value, and extra constants
+
# %% Define time spans, initial values, and constants
tspan =  
+
tspan = np.linspace( )  # start, finish, numpoints
yinit = #must be a list or an array
+
yinit = [ ]            # list of initial values
c =  
+
c = [ ]                # list of constants
  
 
#%% Solve differential equation
 
#%% Solve differential equation
sol = solve_ivp(f, [tspan[0], tspan[-1]], yinit, t_eval=tspan)
+
sol = solve_ivp(lambda t, y: f(t, y, c),  
 
+
                [tspan[0], tspan[-1]], yinit, t_eval=tspan, rtol = 1e-5)
#%% Determine if states should be plotted
 
plot_states = True;
 
 
 
#%% Plot independent and dependent variable
 
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
=== State Plotter ===
 
The <code>StatePlotter.m</code> code will accept a vector of times and a matrix of states (which should have each state in a column).  It will determine how many states there are based on the number of columns, break the figure window up into the appropriate number of subplots, and graph each state.
 
 
<source lang="matlab">
 
function StatePlotter(Time, States)
 
 
StateCount = size(States, 2);
 
 
NumCols = ceil(sqrt(StateCount));
 
NumRows = ceil(StateCount / NumCols);
 
clf;
 
for PlotNumber = 1:StateCount
 
        subplot(NumRows, NumCols, PlotNumber);
 
        plot(Time, States(:,PlotNumber), 'ko:');
 
        xlabel('Time');
 
        ylabel(sprintf('y_{%0.0f}(t)', PlotNumber))
 
        title(sprintf('y_{%0.0f}(t) vs. Time', PlotNumber));
 
end
 
</source>
 
  
 
== Questions ==
 
== Questions ==

Latest revision as of 14:39, 14 April 2021

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