Physics 281: Computational Physics, Spring 2019

Activities for Meetings 16-17, Tues-Thurs 3/26-28/191

Reading

• Newman Ch. 8: Ordinary differential equations:

– 8.1 First-order differential equations with one variable.

– 8.2 Differential equations with more than one variable.

– 8.3 Second-order differential equations.

These three sections cover the material we will use in P281. You should

take a look at the remaining sections of Ch. 8 to get an idea of some

more advanced methods for solving ODEs.

• Section “Solving ordinary differential equations” in handout Summary

of numerical methods.

In-Class Activities

Completing all of the non-starred activities will count as A-level work. To

receive credit, these activities must be checked off in class or office hours

within three weeks (by Fri 4/19).

- Solving an ODE with odeint: The low-pass filter. The circuit

shown in the figure below built from a resistor R and a capacitor C is

called a “low-pass filter”. The input voltage Vin(t) is specified. Then

the output voltage Vout(t) is determined by the following first-order

ODE:

This is called a low-pass filter because low-frequency sine waves with

angular frequencies ω 1/RC pass through this circuit nearly unchanged, while high frequency sine waves with ω 1/RC are (imperfectly) blocked. For this problem use R = 10 kΩ, C = 1 µF which gives

1/RC = 100 rad/s.

12019-03-27 P281 W9ab.tex

c 2019 Donald Candela

(a) Assume Vin(t) = A sin(ωt) with A = 2.0 V and ω = 50 rad/s.

Write a program that defines a function to compute Vin(t) and

uses this function to make a plot of Vin(t) for 0 < t < tmax, with

tmax chosen to show about two or three complete cycles of the sine

wave. Make the number of time points a variable that you can

easily change it (or input the number of points when you run your

program. Start with several hundred points to get a nice smooth

curve.

(b) Write a program to that uses odeint to find Vout(t) at the same

set of time points. Use the initial condition Vout(0) = 0.0 V.

Plot Vin(t) and Vout(t) on the same graph (with labeled axes and

a legend for the two curves, of course). Because ω < 1/RC,

the output should be only a little bit smaller than the input and

slightly shifted in phase.

(c) Run your program again, changing ω to 300 rad/s. Now the output should be quite a bit smaller than the input, and shifted in

phase by nearly π/2. For both values of ω you should find that

Vout(t) is a sine wave except for an initial transient near t = 0.

- Euler’s method. Add code to your program to compute Vout(t) at

the same set of time points using Euler’s method, and plot the results

on the same graph as the odeint results. Find the minimum number of

time points for which the Euler’s-method results appear identical (by

eye) to the odeint results. (For this purpose, you may want to remove

the Vin(t) curve from your plot.) - RK2. Add code to compute Vout(t) using the second-order RungeKutta method, and plot the RK2, Euler’s-method, and odeint results

together. As with the previous activity, find the minimum number of

2

time points for which the RK2 results appear identical (by eye) to the

odeint results. - *RK4. As explained in Newman Sec. 8.1.3, if you didn’t have an

ODE integrator like scipy.integrate.odeint and had to program

one yourself, you would probably use fourth-order Runge-Kutta, which

is quite accurate. Modify your program from the last activity to include

an RK4 integration of the ODE, using the formulas from Newman

Sec. 8.1.3. To check the accuracy, have your program print out y(tmax)

for the various methods (odeint, RK2, RK4).