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

2024 Week 7 Higer Order ODE Solvers - Jupyter Notebook

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)
24 views

2024 Week 7 Higer Order ODE Solvers - Jupyter Notebook

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/ 4

In [1]: 1 import numpy as np

What is the stability condition for the following differential equation for negative 𝛼 using Trapezoid method :
𝑥˙ = 𝛼𝑥
Trapezoid method :

𝑥𝑛+1 = 𝑥𝑛 + Δ𝑡 ( 𝑓(𝑥 , 𝑡𝑛 ) + 𝑓(2 𝑥 , 𝑡𝑛+1 ) )


𝑛 𝑛+1

∴ 𝑥𝑛+1 = 𝑥𝑛 + Δ𝑡 ( 𝛼𝑥 +2𝛼𝑥 )
𝑛 𝑛+1

𝑜𝑟, 𝑥𝑛+1 − 𝛼Δ𝑡2 𝑥𝑛+1 = 𝑥𝑛 + 𝛼Δ𝑡 𝑥𝑛


2
𝑜𝑟, (1 − 2 ) 𝑥 = (1 + 𝛼Δ𝑡
𝛼Δ𝑡 𝑛+1
2 ) 𝑥𝑛

𝑜𝑟, 𝑥𝑛+1 = ( 11 +− 𝛼Δ𝑡2 ) 𝑥𝑛 = 𝜆𝑥𝑛


𝛼Δ𝑡
2
𝑜𝑟, 𝜆 = 𝑥𝑥𝑛 = ( 11 +− 𝛼Δ𝑡2 )
𝑛+1 𝛼Δ𝑡
2
For the method to be stable, the amplification factor must satisfy :
∴ |𝜆| < 1
|| 1 + 𝛼Δ𝑡2 ||
𝑜𝑟, || 1 − 𝛼Δ𝑡 || < 1
| 2|
To solve the differential equation with time reversal symmetry, which of the following numerical scheme is
applicable:

Leap-Frog scheme

= 𝑡2−2𝑥2 .
Write a python code to solve the differential equation 𝑑𝑥
𝑑𝑡
Use second order Runge-Kutta (RK2) scheme, Δ𝑡 = 0.01 and 𝑥(𝑡 = 0) = 1 . What is the value of 𝑥(𝑡 = 1) ?
Round off the answer to two decimal places.

In [2]: 1 def f(t, x):


2 return (t**2 - x**2) / 2
3 ​
4 def RK2(f, t0, x0, dt, t_end):
5 t_values = np.arange(t0, t_end + dt, dt)
6 x = x0
7 for t in t_values[:-1]:
8 k1 = dt * f(t, x)
9 k2 = dt * f(t + dt, x + k1)
10 x = x + 0.5 * (k1 + k2)
11 return x
12 ​
13 t0 = 0
14 x0 = 1
15 dt = 0.01
16 t_end = 1
17 x_final = RK2(f, t0, x0, dt, t_end)
18 round( x_final, 2)

Out[2]: 0.81
𝑑𝑥 + 𝑥cos(𝑡) + 𝑡cos(𝑥) = 0 .
Write a python code to solve the differential equation 𝑑𝑡
Use RK4 scheme, Δ𝑡 = 0.01 and 𝑥(𝑡 = 0) = 1 and find the value of 𝑥(𝑡 = 1). Round of your result to two
decimal places.

In [3]: 1 def RK4(f, t0, x0, dt, t_end):


2 t_values = np.arange(t0, t_end + dt, dt)
3 x = x0
4 for t in t_values[:-1]:
5 k1 = dt * f(t, x)
6 k2 = dt * f(t + 0.5*dt, x + 0.5*k1)
7 k3 = dt * f(t + 0.5*dt, x + 0.5*k2)
8 k4 = dt * f(t + dt, x + k3)
9 x = x + (k1 + 2*k2 + 2*k3 + k4) / 6
10 return x
11 ​
12 def f(t, x):
13 return - (x*np.cos(t) + t*np.cos(x))
14 ​
15 t0 = 0
16 x0 = 1
17 dt = 0.01
18 t_end = 1
19 x_final = RK4(f, t0, x0, dt, t_end)
20 round( x_final, 2)

Out[3]: 0.06

Write a python code to solve the differential equation = 𝑒−𝑥 + 𝑒𝑡 using Predictor-Corrector Scheme.
𝑑𝑥
𝑑𝑡
Using Δ𝑡 = 0.01 and 𝑥(𝑡 = 0) = 1 , find the value of 𝑥(𝑡 = 2) . Round off your results to 2 decimal places.

In [4]: 1 def f(t, x):


2 return np.exp(-x) + np.exp(t)
3 ​
4 def predictor_corrector(f, t0, x0, dt, t_end):
5 t_values = np.arange(t0, t_end + dt, dt)
6 x = x0
7 for t in t_values[:-1]:
8 x_predict = x + dt * f(t, x) # Predictor step (Euler me
9 x = x + 0.5 * dt * (f(t, x) + f(t + dt, x_predict)) # Corrector step (Trapezoi
10 return x
11 ​
12 t0 = 0
13 x0 = 1
14 dt = 0.01
15 t_end = 2
16 x_final = predictor_corrector(f, t0, x0, dt, t_end)
17 round( x_final, 2)

Out[4]: 7.59

What is the name of the function in scipy.integrate module that can be used to integrate ordinary differential
equations?

odeint

Which of the following method is most accurate?

Runge-Kutta 4th order

What is the Fourier transform of the function 𝑓(𝑥) = cos(2𝑥)?


Using Euler's formula

cos(2𝑥) = 12 (𝑒𝑖2𝑥 + 𝑒−𝑖2𝑥 )


Rewrite the function𝑓(𝑥) = cos(2𝑥) as:
𝑓(𝑥) = 12 (𝑒𝑖2𝑥 + 𝑒−𝑖2𝑥 )
The general definition of the Fourier transform of a function 𝑓(𝑥) is:
𝑓 𝑘̂ = 2𝜋1 ∫−∞ 𝑓(𝑥)𝑒−𝑖𝑘𝑥 𝑑𝑥

Fourier transform of 𝑒𝑖2𝑥


 [𝑒𝑖2𝑥 ] = 2𝜋1 ∫−∞ 𝑒𝑖2𝑥 𝑒−𝑖𝑘𝑥 𝑑𝑥 = 2𝜋1 ∫−∞ 𝑒𝑖(2−𝑘)𝑥 𝑑𝑥
∞ ∞

The integral of 𝑒𝑖(2−𝑘)𝑥 over all 𝑥 is a known result:


it yields 2𝜋𝛿(2 − 𝑘), where 𝛿 is the Dirac delta function.
Therefore:

 [𝑒𝑖2𝑥 ] = 𝛿(𝑘 − 2)
Fourier transform of 𝑒−𝑖2𝑥

𝑒−𝑖2𝑥 is:
Similarly, the Fourier transform of

1 ∞ 1
 [𝑒 ] = 2𝜋 ∫−∞ 𝑒 𝑒 𝑑𝑥 = 2𝜋 ∫−∞ 𝑒−𝑖(2+𝑘)𝑥 𝑑𝑥
−𝑖2𝑥 −𝑖2𝑥 −𝑖𝑘𝑥

This results in 2𝜋𝛿(𝑘 + 2), so:

 [𝑒−𝑖2𝑥 ] = 𝛿(𝑘 + 2)
Since 𝑓(𝑥) = 2 (𝑒𝑖2𝑥 + 𝑒−𝑖2𝑥 ), the Fourier transform of 𝑓(𝑥) is the sum of the Fourier transforms of these two
1
terms:

𝑓 𝑘̂ = 12 (𝛿(𝑘 − 2) + 𝛿(𝑘 + 2))


In the discrete case, the Fourier coefficients 𝑓 𝑛 are defined as the sum over discrete modes.
𝑓 𝑛 = 12 (𝛿𝑛,2 + 𝛿𝑛,−2 )
𝑥˙ = 𝑥2 − 𝑥 using the odeint function from the scipy.integrate module and
Solve the differential equation
find the value of 𝑥(𝑡 = 2.0).
Use the following given values : 𝑥(𝑡 = 0) = 1.1,𝑑𝑡 = 0.2 . Round your answer to two decimal places.
Note: use the following line to import odeint from scipy.integrate import odeint
Also round the last value using np.round(x[-1],2) as the simple round function will give error because
x[-1] is a numpy array of one value.

In [5]: 1 from scipy.integrate import odeint


2 ​
3 def dxdt(x, t):
4 return x**2 - x
5 ​
6 x0 = 1.1
7 t = np.arange(0, 2.2, 0.2)
8 x = odeint(dxdt, x0, t)
9 x_final = np.round(x[-1], 2)
10 x_final[0]

Out[5]: 3.05
𝑥˙ = 𝑝, 𝑝˙ = −𝑥
Consider the following set of coupled differential equations

Solve these set of differential equations using the following given conditions :
x(t=0) = 1
p(t=0) = 0
dt = 0.01
What is the value of 𝑥(𝑡 = 2π) rounded off to 3 decimal places ?
In [6]: 1 dt = 0.01
2 T = 2 * np.pi
3 N = int(T / dt)
4 x = np.zeros(N + 1)
5 p = np.zeros(N + 1)
6 x[0] = 1
7 p[0] = 0
8 ​
9 # Euler's method to solve the equations
10 for i in range(N):
11 x[i + 1] = x[i] + p[i] * dt
12 p[i + 1] = p[i] - x[i] * dt
13 ​
14 round(x[-1], 3)

Out[6]: 1.032

You might also like