python22.sci2u.dk Active sci2u
Loading...
Login
Prefer latest booklet Prefer latest booklet Login
Link to booklet is copied to clipboard!
Saved to booklet!
Removed from booklet!
Started capturing to booklet.
Stopped capturing to booklet.
Preferring latest booklet.
No longer prefers latest booklet.
Entered edit mode.
Exited edit mode.
Edit mode

Click on the booklet title or a chapter title to edit (max. 30 characters)


How To's

How to get python How to plot points and lines How to use loops How to draw general shapes How to draw curves How to use functions How to manipulate Polygons How to create animations How to do mathematical analysis How to fit How to go from SymPy to NumPy How to solve a single differential equation How to do coupled and 2nd order ODEs
13.1 ODEs: Coupled
13.2 Different naming of variables
13.3 ODE: 2nd order
How to combine vectors and ODEs How to do ODE events Add Chapter..

How to do coupled and 2nd order ODEs

13.1 ODEs: Coupled

Consider these two differential equations:
Since the evolution in time of depends on and vice versa, the equations constitute a set of coupled differential equations.
Given some initial value condition, e.g.
the problem of finding and until some later time, e.g. , may be solved with solve_ivp like this:
from scipy.integrate import solve_ivp

tinit = 0
tfinal = 20
trange = [tinit,tfinal]
yinit = [1,-1]

dydt = lambda t, y: [y[1],-y[0]-1/10*y[1]]
mysol = solve_ivp(dydt, trange, yinit)
ts = mysol.t
y0s = mysol.y[0]
y1s = mysol.y[1]
Compared to the situation with a single ODE, a number of things has happened:
  1. the function dydt now assumes that its 2nd argument is a list with two elements, and .
  2. the function dydt returns a list of the right-hand sides of both differential equations, i.e. it returns .
  3. the initial value for the dependent variable, yinit, is now a list
  4. two lists of solutions (for and , respectively) are extracted from the result of solve_ivp with the mysol.y[0] and mysol.y[1] statements.
When plotted with these commands:
import matplotlib.pyplot as plt
plt.rc('font', size=16)

fig, ax = plt.subplots()
ax.plot(ts,y0s,linestyle='dashed',marker='o')
ax.plot(ts,y1s,linestyle='dashed',marker='x')
ax.set_xlabel('$t$')
ax.set_ylabel('$y$')
ax.legend(['$y_0$','$y_1$'])
the solution looks like this

Figure 1



13.2 Different naming of variables

Consider if we had encounted the exact same set of differential equations, only with another naming of the dependent variables, e.g.:
with the initial value condition: and for . Well, obviously, the solution would be the same, and the above Python program would do the job. However, it would be neat the write the program with a naming convention for the variables that is closer to the problem as it is formulated. That could for instance be achieved by writing:
tinit = 0
tfinal = 20
trange = [tinit,tfinal]
rinit = 1
winit = -1
yinit = [rinit,winit]

def dydt(t,y):
    r = y[0]
    w = y[1]
    drdt = w
    dwdt = -r - 1/10 * w
    return [drdt,dwdt]
    
mysol = solve_ivp(dydt, trange, yinit)
ts = mysol.t
rs = mysol.y[0]
ws = mysol.y[1]
which provides the exact same solution:

Figure 2

Note how the function dydt evaluating the right-hand side of the differential equations has been defined at length, rather than in the compact form of a lambda-function. This in particular allows for the section,
    r = y[0]
    w = y[1]
where the original naming of variables is recoved, so that when the right-hand sides of the differential equations are evaluated, they become readable:
    drdt = w
    dwdt = -r - 1/10 * w
and hence immediately recognizable compared to the problem formulation.


13.3 ODE: 2nd order

A 2nd order differential equation with one dependent variable generally looks like this:
where may be anything. Consider as an example that we want to solve:
and that the initial value condition is specified as and . Introducing a "helper"-variable,
we may rewrite the 2nd order differential equation as two 1st order differential equations without loss of generality:
whereby the initial value condition becomes and .
As a consequence of the rewriting, the 2nd order differential equation has turned into a set of coupled 1st order differential equations, that looks exactly like some we have already solved with solve_ivp. Adopting the same code, but using the present variable names, it may look like this:
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt

tinit = 0
tfinal = 12
trange = [tinit,tfinal]
x0 = 1
v0 = -1
yinit = [x0,v0]

def dydt(t,y):
    x = y[0]
    v = y[1]
    dxdt = v
    dvdt = -x - 1/10 * v
    return [dxdt,dvdt]

mysol = solve_ivp(dydt, trange, yinit)
ts = mysol.t
xs = mysol.y[0]
vs = mysol.y[1]

plt.rc('font', size=16)
fig,ax = plt.subplots(1,1)
ax.plot(ts,xs,'o',linestyle='--')
ax.plot(ts,vs,'x',linestyle='--')
ax.grid()
ax.set_xlim([-1,13])
ax.set_xlabel('$t$')
ax.set_ylabel('$x,v$')
ax.legend(['$x$',r'$v=\frac{dx}{dt}$'])
whereby we have solved a 2nd order differential equation. The plot looks like this:

Figure 3



Sci2u Assignment: 820
Delete "How To's"?
Once deleted this booklet is gone forever!
Block is found in multiple booklets!

Choose which booklet to go to:

© 2019-2022 Uniblender ApS