Uploaded by Perfectoid

Differential equations in computational physics 2

advertisement
11/1/22
PHYS 230
Computational Physics
First-order Differential Equations
Euler & Runge-Kutta
Tuesday, November 1, 2022
1
Announcements/Reminders
• Project #2 due at 11:59 PM
• Start thinking about an end-of-semester project
Today
• First-order ordinary differential equations
• Euler method
• Runge-Kutta method
• Solutions over infinite ranges
2
1
11/1/22
Ordinary Differential Equations
• Finding the solution of differential equations is the most common use of
computers in physics
• Ordinary differential equations in physics:
• Equations of motion of rigid bodies
𝑑! 𝑥 𝐹
=
𝑑𝑡 ! 𝑚
• with and without air resistance
• Harmonic oscillations
• damped, forced
• Fluid pressure
• Radioactivity
• Behavior of electrical circuits
and more!
𝑑𝑁
= −𝑘𝑁
𝑑𝑡
3
First-order Differential Equations
Once Variable
• Ordinary differential equations – those for which there is only one
independent variable
• Examples of first-order:
𝑑𝑥 2𝑥
=
𝑑𝑡
𝑡
𝑑𝑥 2𝑥 3𝑥 !
=
+ "
𝑑𝑡
𝑡
𝑡
• Once the equation is no longer separable and possibly nonlinear,
need numerical methods to solve instead.
• Actually, whether the ODE is still linear or nonlinear, the techniques used to
solve the ODE are the same.
• The general form we are considering is:
𝑑𝑥
𝑑𝑦
And we also require an initial
= 𝑓(𝑥, 𝑡)
= 𝑓(𝑥, 𝑦)
condition or boundary condition
𝑑𝑡
𝑑𝑥
4
2
11/1/22
Euler’s Method
"#
• We have an equation of the form = 𝑓(𝑥, 𝑡) and a boundary
"$
condition of 𝑥 at a time 𝑡.
• Can use a Taylor Series Expansion to write the value of 𝑥 a short time later ℎ
𝑑𝑥 1 ! 𝑑!𝑥
𝑥 𝑡+ℎ =𝑥 𝑡 +ℎ
+ ℎ
+⋯
𝑑𝑡 2 𝑑𝑡 !
𝑑𝑥
𝑥 𝑡+ℎ ≈𝑥 𝑡 +ℎ
𝑑𝑡
Can get 𝑥 at a future time
by knowing:
the value of 𝑥 at a time 𝑡
The first time derivative of 𝑥
#$
• But, the first time derivative is our original equation #% = 𝑓(𝑥, 𝑡)
5
Euler’s Method
• To first order, we now have a way to calculate 𝑥(𝑡)
• Start with the boundary condition to get 𝑥 𝑡 ! = 𝑥(𝑡 + ℎ) = 𝑥(𝑡) + ℎ 𝑓(𝑥, 𝑡)
• Iterate! 𝑥 𝑡 !! = 𝑥(𝑡′ + ℎ) and so on until cover desired range of 𝑡
• Don’t get all 𝑥(𝑡) for all values of 𝑡 from this calculation, only a finite set of
points.
• But if ℎ is small enough, get a pretty good picture of the solution
• This iterative process is Euler’s Method
• Is an approximation since have ignored the ℎ" term (and higher)
• But good approximation if ℎ is small (total error goes as ℎ)
• This method is not bad, but not used as much
• With little extra effort can get a more accurate and often faster method of Runge-Kutta
6
3
11/1/22
The Runge-Kutta Method
• You might think that the best way to improve our Euler’s method is to
keep higher order terms from our expansion.
• The next term is (see equation 8.9 in book)
• In order to do this, we need to know the derivative d𝑓/𝑑𝑡, which we may not
be able to do.
• However, even if we can calculate the derivative, Runge-Kutta is an
alternative method that doesn’t require a derivative (making it more
convenient!) and is more accurate.
7
The Runge-Kutta Method
• Let’s consider Euler’s method graphically.
• What we did was to take the slope at point 𝑡
and used that to extrapolate what the function
would be at 𝑡 + ℎ
• The idea of Runge-Kutta (also known as the
midpoint method) is that instead we
calculate the slope at 𝑡 + ℎ/2 and then
extrapolate this slope to get x(𝑡 + ℎ)
• Which is usually a better estimate!
• If evaluated the Taylor expansion, we find that the
ℎ! terms disappears
• This is actually the second-order Runge-Kutta
• Euler’s is also a Runge-Kutta method (first
order)
1
1
𝑥 𝑡 + ℎ = 𝑥 𝑡 + ℎ𝑓 𝑥 𝑡 + ℎ , 𝑡 + ℎ + O(ℎ!)
2
2
8
4
11/1/22
Second-Order Runge-Kutta Method
• From Taylor expansion, we get:
1
1
𝑥 𝑡 + ℎ = 𝑥 𝑡 + ℎ𝑓 𝑥 𝑡 + ℎ , 𝑡 + ℎ + O(ℎ" )
2
2
The full calculation is:
• The problem with this approach is that it
/
requires us to know 𝑥(𝑡 + ℎ), which we don’t.
𝑘( = ℎ𝑓(𝑥, 𝑡)
"
• How do we get around this?
/
• We first approximate 𝑥 𝑡 + ℎ using Euler’s
"
method:
1
1
𝑥 𝑡 + ℎ = 𝑥 𝑡 + ℎ𝑓(𝑥, 𝑡)
2
2
1
1
𝑘! = ℎ𝑓(𝑥 + 𝑘(, 𝑡 + ℎ)
2
2
𝑥 𝑡 + ℎ = 𝑥 𝑡 + 𝑘!
The final Runge-Kutta
estimate for 𝑥 𝑡 + ℎ
accurate to order ℎ!
then substitute into the equation above.
9
Fourth-order Runge-Kutta Method
• There is no need to stop at
second-order.
• In fact, fourth-order Runge-Kutta
is considered the best!
• Just the right compromise
between accuracy and complexity
of program
• Can develop equations to use in
the algorithm (shown on right).
• Accurate to terms of order ℎ0 so
carries error on order of ℎ1
𝑘( = ℎ𝑓(𝑥, 𝑡)
1
1
𝑘! = ℎ𝑓(𝑥 + 𝑘(, 𝑡 + ℎ)
2
2
1
1
𝑘" = ℎ𝑓(𝑥 + 𝑘!, 𝑡 + ℎ)
2
2
𝑘) = ℎ𝑓(𝑥 + 𝑘", 𝑡 + ℎ)
1
𝑥 𝑡 + ℎ = 𝑥 𝑡 + (𝑘( + 2𝑘! + 2𝑘" + 𝑘))
6
One thing to note about Runge-Kutta, the method is so accurate that if you make a mistake in your
coding of the algorithm, it may not at all be obvious. Thus one must take extra care in coding it up.
10
5
11/1/22
Solutions Over Infinite Ranges
• What if you want to use one of these methods to find the solution out to 𝑡 = ∞?
• Just like we did with integrals, can change variables:
𝑡
𝑢
𝑢=
𝑡=
1+𝑡
1−𝑢
• Now, as 𝑡 → ∞, u → 1
• Using the chain rule, can rewrite our differential equation:
𝑑𝑥 𝑑𝑢
= 𝑓 𝑥, 𝑡
𝑑𝑢 𝑑𝑡
𝑑𝑡
1
=
𝑑𝑢
1−𝑢 !
→
→
𝑑𝑥 𝑑𝑡
𝑢
=
𝑓(𝑥,
)
𝑑𝑢 𝑑𝑢
1−𝑢
𝑑𝑥
1
𝑢
=
𝑓(𝑥,
)
𝑑𝑢
1−𝑢 !
1−𝑢
• Could now define a new function:
𝑔 𝑥, 𝑢 = 1 − 𝑢
#!
𝑓 𝑥,
𝑢
1−𝑢
𝑑𝑥
= 𝑔(𝑥, 𝑢)
𝑑𝑢
11
6
Download