Difference between revisions of "Python:Ordinary Differential Equations"

From PrattWiki
Jump to navigation Jump to search
Line 7: Line 7:
 
<center><math>
 
<center><math>
 
\begin{align}
 
\begin{align}
\frac{dy}{dt}&=f(t, y, C)
+
\frac{dy}{dt}&=f(t, y, c)
 
\end{align}
 
\end{align}
 
</math></center>
 
</math></center>
where <math>y</math> represents an ''array'' of dependent variables, <math>t</math> represents the independent variable, and <math>C</math> represents an array of constants.
+
where <math>y</math> represents an ''array'' of dependent variables, <math>t</math> represents the independent variable, and <math>c</math> represents an array of constants.
 
Note that although the equation
 
Note that although the equation
 
above is a first-order differential equation, many higher-order equations
 
above is a first-order differential equation, many higher-order equations
Line 23: Line 23:
 
first derivatives of each of the dependent variables.  In other words,
 
first derivatives of each of the dependent variables.  In other words,
 
you will need to write a function that takes <math>t</math>, <math>y</math>, and possibly
 
you will need to write a function that takes <math>t</math>, <math>y</math>, and possibly
<math>C</math> and returns <math>f(t, y, C)</math>.  Note that the output needs to be returned as an array or a list.   
+
<math>c</math> and returns <math>f(t, y, c)</math>.  Note that the output needs to be returned as an array or a list.   
  
The second part will use the
+
The second part will use this function in concert with SciPy's ODE solver to calculate  
first function in concert with SciPy's ODE solvers to calculate  
+
solutions over a specified time range assuming given initial conditions. Specifically it will use [https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html scipy.integrate.solve_ivp].
solutions over a specified time range assuming given initial
 
conditions.  
 
  
<!--
 
=== Formal Organization ===
 
==== Differential Function File ====
 
An easy way to set up the function that will calculate the values of the first derivatives of each of the dependent variables is to pass it
 
three inputs - the independent variable, the dependent
 
variable, and the constants.  The function should return the values of the derivatives in a single matrix. 
 
Note that for multi-variable systems, the derivatives
 
should be returned as a column vector.  For many systems, this
 
code only needs to have one (possibly algebraically complicated)
 
command in it.  A template is available at [[MATLAB:Ordinary Differential Equations/Templates]] while several examples - combined with Execution File examples - are at [[MATLAB:Ordinary Differential Equations/Examples]].
 
 
==== Execution Script ====
 
Once the function for the differential is done, you need to write code
 
to actually use it for a specific case.  This file needs to set up your
 
time base (either the initial and final time for which to solve or the
 
complete set of times at which to solve), define the initial values
 
for your dependent variables, and set any constants needed for the problem.  You will then use one of MATLAB's
 
ODE solvers to calculate the values for the variables at the specified
 
times.  Typically, you will then generate a graph of the answer. 
 
A template is available at [[MATLAB:Ordinary Differential Equations/Templates]] while several examples - combined with Differential File examples - are at [[MATLAB:Ordinary Differential Equations/Examples]].
 
-->
 
 
=== Example ===
 
=== Example ===
 
Imagine you want to look at the value of some output <math>y(t)</math> obtained from a differential system
 
Imagine you want to look at the value of some output <math>y(t)</math> obtained from a differential system
 +
<center><math>
 +
\begin{align}
 +
\frac{dy(t)}{dt}+\frac{y(t)}{4}&=x(t)
 +
\end{align}
 +
</math></center>
 +
You first need to rearrange this to be an equation for <math>\frac{dy(t)}{dt}</math> as a function of <math>t</math>, <math>y</math>, and constants.  This equation also has an independent <math>x(t)</math> so that will be a part of the function as well:
 
<center><math>
 
<center><math>
 
\begin{align}
 
\begin{align}
 
\frac{dy(t)}{dt}&=x(t)-\frac{y(t)}{4}\\
 
\frac{dy(t)}{dt}&=x(t)-\frac{y(t)}{4}\\
 +
\end{align}
 +
</math></center>
 +
Now if you further assume that:
 +
<center><math>
 +
\begin{align}
 
x(t)&=\cos(3t)\\
 
x(t)&=\cos(3t)\\
y(0)&=5
+
y(0)&=-1
 
\end{align}</math></center>
 
\end{align}</math></center>
you can write that code as:
+
you can numerically solve this differential equation (and plot the results for both <math>x(t)</math> and <math>y(t)</math>) with:
 
<syntaxhighlight lang='python'>
 
<syntaxhighlight lang='python'>
#%% Imports
+
# %% Imports
 
import numpy as np
 
import numpy as np
 
import matplotlib.pyplot as plt
 
import matplotlib.pyplot as plt
 
from scipy.integrate import solve_ivp
 
from scipy.integrate import solve_ivp
  
#%% Define independent function and derivative function
+
# %% Define independent function and derivative function
 
def x(t):
 
def x(t):
     return np.cos(3*t)
+
     return np.cos(3 * t)
  
def f(t, y):
+
def f(t, y, c):
     dydt = x(t)-y/4
+
     dydt = x(t) - y / 4
 
     return dydt
 
     return dydt
   
+
 
#%% Define time spans and initial value
+
# %% Define time spans, initial values, and constants
 
tspan = np.linspace(0, 15, 1000)
 
tspan = np.linspace(0, 15, 1000)
yinit = [5]
+
yinit = [3]
 +
c = []
  
#%% 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)
  
#%% Plot independent and dependent variable
+
# %% Plot independent and dependent variable
 
# Note that sol.y[0] is needed to extract a 1-D array
 
# Note that sol.y[0] is needed to extract a 1-D array
 
plt.figure(1)
 
plt.figure(1)
Line 87: Line 77:
 
plt.plot(sol.t, x(sol.t), 'k-', label='Input')
 
plt.plot(sol.t, x(sol.t), 'k-', label='Input')
 
plt.plot(sol.t, sol.y[0], 'k--', label='Output')
 
plt.plot(sol.t, sol.y[0], 'k--', label='Output')
plt.legend()
+
plt.legend(loc='best')
 
</syntaxhighlight >
 
</syntaxhighlight >
  
Line 95: Line 85:
  
 
deqn := diff(y(t), t) = x(t)-(1/4)*y(t);
 
deqn := diff(y(t), t) = x(t)-(1/4)*y(t);
 
+
 
 
Src := x(t) = cos(3*t);
 
Src := x(t) = cos(3*t);
  
MySoln := dsolve({subs(Src, deqn), y(0) = 5}, [y(t)]);
+
MySoln := dsolve({subs(Src, deqn), y(0) = 3}, [y(t)]);
  
 
plot(subs(Src, MySoln, [x(t), y(t)]), t = 0 .. 15);
 
plot(subs(Src, MySoln, [x(t), y(t)]), t = 0 .. 15);

Revision as of 22:50, 26 November 2018

Introduction

This page, based very much on MATLAB:Ordinary Differential Equations is aimed at introducing techniques for solving initial-value problems involving ordinary differential equations using Python. Specifically, it will look at systems of the form:

\( \begin{align} \frac{dy}{dt}&=f(t, y, c) \end{align} \)

where \(y\) represents an array of dependent variables, \(t\) represents the independent variable, and \(c\) represents an array of constants. Note that although the equation above is a first-order differential equation, many higher-order equations can be re-written to satisfy the form above.

In addition, the examples on this page will assume that the initial values of the variables in \(y\) are known - this is what makes these kinds of problems initial value problems (as opposed to boundary value problems).

Solving initial value problems in Python may be done in two parts. The first will be a function that accepts the independent variable, the dependent variables, and any necessary constant parameters and returns the values for the first derivatives of each of the dependent variables. In other words, you will need to write a function that takes \(t\), \(y\), and possibly \(c\) and returns \(f(t, y, c)\). Note that the output needs to be returned as an array or a list.

The second part will use this function in concert with SciPy's ODE solver to calculate solutions over a specified time range assuming given initial conditions. Specifically it will use scipy.integrate.solve_ivp.

Example

Imagine you want to look at the value of some output \(y(t)\) obtained from a differential system

\( \begin{align} \frac{dy(t)}{dt}+\frac{y(t)}{4}&=x(t) \end{align} \)

You first need to rearrange this to be an equation for \(\frac{dy(t)}{dt}\) as a function of \(t\), \(y\), and constants. This equation also has an independent \(x(t)\) so that will be a part of the function as well:

\( \begin{align} \frac{dy(t)}{dt}&=x(t)-\frac{y(t)}{4}\\ \end{align} \)

Now if you further assume that:

\( \begin{align} x(t)&=\cos(3t)\\ y(0)&=-1 \end{align}\)

you can numerically solve this differential equation (and plot the results for both \(x(t)\) and \(y(t)\)) with:

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

# %% Define independent function and derivative function
def x(t):
    return np.cos(3 * t)

def f(t, y, c):
    dydt = x(t) - y / 4
    return dydt

# %% Define time spans, initial values, and constants
tspan = np.linspace(0, 15, 1000)
yinit = [3]
c = []

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

# %% Plot independent and dependent variable
# Note that sol.y[0] is needed to extract a 1-D array
plt.figure(1)
plt.clf()
plt.plot(sol.t, x(sol.t), 'k-', label='Input')
plt.plot(sol.t, sol.y[0], 'k--', label='Output')
plt.legend(loc='best')

As an aside, to get the same graph in Maple, you could use:

restart;

deqn := diff(y(t), t) = x(t)-(1/4)*y(t);

Src := x(t) = cos(3*t);

MySoln := dsolve({subs(Src, deqn), y(0) = 3}, [y(t)]);

plot(subs(Src, MySoln, [x(t), y(t)]), t = 0 .. 15);

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

SciPy page for solve_ivp

References