0% found this document useful (0 votes)
30 views

Acmefiles 08 BVP 2021 PDF

1. The document introduces initial value problems (IVPs) and boundary value problems (BVPs). IVPs involve a differential equation and initial conditions, while BVPs involve boundary conditions at both endpoints in addition to the differential equation. 2. Scipy's solve_ivp function can be used to solve IVPs by specifying the differential equation, time domain, and initial conditions. solve_bvp can solve BVPs by additionally specifying boundary conditions and an initial guess. 3. Choosing an appropriate initial guess for solve_bvp is important, as small changes to the guess can significantly impact the solution. The document demonstrates this effect with an example problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Acmefiles 08 BVP 2021 PDF

1. The document introduces initial value problems (IVPs) and boundary value problems (BVPs). IVPs involve a differential equation and initial conditions, while BVPs involve boundary conditions at both endpoints in addition to the differential equation. 2. Scipy's solve_ivp function can be used to solve IVPs by specifying the differential equation, time domain, and initial conditions. solve_bvp can solve BVPs by additionally specifying boundary conditions and an initial guess. 3. Choosing an appropriate initial guess for solve_bvp is important, as small changes to the guess can significantly impact the solution. The document demonstrates this effect with an example problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1

Intro to IVP and BVP

Initial Value Problems


An initial value problem is a differential equation with a set of constraints at the initial point. An
IVP may look something like this

y 00 + y 0 + y = f (t)
y(a) = α
y 0 (a) = β
t ∈[a, b].

This problem gives a differential equation with initial conditions for y and y 0 .
Formulating and solving initial value problems is an important tool when solving many types of
problems. One simple example of an IVP would be a differential equation modeling the path of a ball
thrown in the air where the initial position (y(a)) and velocity (y 0 (a)) are known. These problems
can be tricky to solve by hand. Luckily, SciPy has great tools that help us solve initial value problems
for most systems of first order ODEs. We will be using solve_ivp from scipy.integrate.
Consider the following example

y 00 + 3y = sin(t), y(0) = −π/2, y 0 (0) = π, t ∈ [0, 5]

We begin by changing this second order ODE into a first order ODE system.

Let y1 = y and y2 = y 0 so that,


 0  
y1 y2
= .
y2 sin t − 3y1

This formulation allows us to use solve_ivp. We need three code elements in order to implement
solve_ivp:

1. the ODE function:


This function defines the ODE system by returning an array containing our first order system
of ODES.

2. the time domain:


This is a tuple giving the interval of integration.

1
2 Lab 1. Intro to IVP and BVP

3. the initial conditions:


This is an array containing the initial conditions starting with the "zeroeth" derivative con-
straint, followed by the first derivative constraint, and so on if there are other constraints of
higher order derivatives.

The following code is for the example given above.

from scipy.integrate import solve_ivp


import numpy as np

# element 1: the ODE function


def ode(t, y):
'''defines the ode system'''
return np.array([y[1],np.sin(t)-3*y[0]])

# element 2: the time domain


t_span = (0,5)

# element 3: the initial conditions


y0 = np.array([-np.pi /2, np.pi])

# solve the system


# max_step is an optional parameter that controls maximum step size and
# a smaller value will result in a smoother graph
sol = solve_iv

Then we can plot the solution with the following code

from matplotlib import pyplot as plt

plt.plot(sol.t,sol.y[0])
plt.xlabel('t')
plt.ylabel('y(t)')
plt.show()
3

Figure 1.1: The solution to the above example

Problem 1. Use solve_ivp to solve for y in the equation y 00 −y = sin(t) with initial conditions
y(0) = − 21 , y 0 (0) = 0 and plot your solution on the interval [0, 5]. Compare this to the analytic
solution y = − 12 (e−t + sin(t)).
Note: Using max_step = 0.1 with give you the smoother graph seen in figure 1.2.
4 Lab 1. Intro to IVP and BVP

Figure 1.2: The solution to problem 1

Boundary Value Problems


A boundary value problem is a differential equation with a set of constraints. It is similar to initial
value problems, but may give end constraints as well as initial constraints. A boundary value problem
may look something like this
y 00 + y 0 + y = f (t)
y(a) = α
y(b) = β
t ∈[a, b],
where we have both right and left hand boundary conditions on y. One simple example of an IVP
would be a differential equation modeling the path of a ball thrown in the air where the initial
position (y(a)) and final position (y(b)).
SciPy has great tools that help us solve boundary value problems. We will be using solve_bvp
from scipy.integrate. Consider the following example:
5
y 00 + 9y = cos(t), y 0 (0) = 5,
y (π) = − . (1.1)
3
We begin by changing this second order ODE into a first order ODE system.
Let y1 = y and y2 = y 0 so that,
 0  
y1 y2
= .
y2 cos t − 9y1
5

This formulation allows us to use solve_bvp. It is important to notice that there are several key
differences between solve_ivp and solve_bvp. We need four code elements in order to implement
solve_bvp:

1. the ODE function:


This is the same function we used in solve_ivp.

2. the boundary condition function:


Instead of just having a tuple containing our initial values, we now must use a function that
returns an array of the residuals of the boundary conditions. We pass in 2 arrays: ya, repre-
senting the initial values, and yb, representing the final values. The ith entry of those arrays
represents the boundary condition at the ith derivative. ya[0]-x would indicate that we know
y(a)=x, ya[1]-x would indicate that we know y`(a)=x, yb[0]-x would indicate that we know
y(b)=x, or yb[1]-x would indicate that we know y`(b)=x,

3. the time domain:


Instead of a tuple giving the interval of integration, we now must pass in a linspace from the
starting time to the ending time, containing the desired number of points (we now must choose
the number).

4. the initial guess:


As we no longer know all of the initial values, we now must make an educated guess. This is
an array of shape (n,t_steps) where n is the shape of the output of the ODE function and
t_steps is the chosen number of steps in our time domain linspace.

from scipy.integrate import solve_bvp


import numpy as np

# element 1: the ODE function


def ode(t,y):
''' define the ode system '''
return np.array([y[1], np.cos(t) - 9*y[0]])
# element 2: the boundary condition function
def bc(ya,yb):
''' define the boundary conditions '''
# ya are the initial values
# yb are the final values
# each entry of the return array will be set to zero
return np.array([ya[1] - 5, yb[0] + 5/3])

# element 3: the time domain.


t_steps = 100
t = np.linspace(0,np.pi,t_steps)

# element 4: the initial guess.


y0 = np.ones((2,t_steps))

# Solve the system.


sol = solve_bvp(ode, bc, t, y0)
6 Lab 1. Intro to IVP and BVP

The syntax for plotting the function is also slightly different:

import matplotlib.pyplot as plt

# here we plot sol.x instead of sol.t


plt.plot(sol.x, sol.y[0])
plt.xlabel('t')
plt.ylabel('y(t)')
plt.show()

Figure 1.3: The solution to the above problem

Problem 2. Use solve_bvp to solve for y in the equation y 00 −y = sin(t) with initial conditions
y(0) = − 21 , y 0 (0) = 0 and plot your solution on the interval [0, 5]. Compare this to the analytic
solution y = − 12 (e−t + sin(t)).
Note: Using max_step = 0.1 with give you the smoother graph seen in figure 1.4.
7

Figure 1.4: The solution to problem 2.

One other useful function of solve_bvp: sol.sol creates a callable function, which is the
estimation of the boundary value problem. You can utilize it by plugging in any int or linspace
(sol.sol(np.linspace), sol.sol(int), sol.sol(list)), like a normal lambda function.

The Pitfalls of solve_bvp

One of the common issues with solve_bvp is choosing a guess for the initial value. Often, small
changes in the guess can cause large changes in the final approximation. The next problem demon-
strates the huge difference that can be made between an initial guess of 10 and an initial guess of
9.99

Problem 3. Use solve_bvp to solve for y in the equation y 00 = (1 − y 0 ) ∗ 10y with boundary
conditions y(0) = −1 and y(1) = 23 and plot your solution on the interval [0, 1]. Use an initial
guess of 10. Compare this to the the same solution using an initial guess of 9.99.
The solution is found in figure 1.5.
8 Lab 1. Intro to IVP and BVP

Figure 1.5: The solution to problem 3.

solve_ivp and Strange Attractors


In the growing field of dynamical systems, an attractor is a set of states toward which a system tends
to evolve. Strange Attractors are special in that they showcase complex behavior in a simple set of
equations. A minute change in the initial values can cause massive discrepancies in the outcome.
The most famous of these is the Lorenz Attractor, introduced by Edward Lorenz in 1963. Later on,
we dedicate a full lab to the study of the Lorenz Attractor, but today we focus on modeling the
Four-Wing attractor. This is a system of first order ODEs defined by the following set of equations
dx
= ax + yz (1.2)
dt
dy
= bx + cy − xz (1.3)
dt
dz
= −z − xy (1.4)
dt
given some constants a, b, and c. As we mentioned earlier, solve_ivp and solve_bvp can be used
to solve and model systems of first order ODEs. We will now use solve_ivp to model a Four-Wing
attractor.

Problem 4. Use solve_ivp to solve the Four-Wing Attractor as described in equations (1.2),
(1.3), and (1.4) where a = 0.2, b = 0.01, and c = −0.4. Try this with 3 different initial values
9

and plot (in three dimensions) the 3 corresponding graphs.


Possible solutions are seen in Figure 1.6.

Figure 1.6: Possible solutions to problem 4.

You might also like