**Physics 281: Computational Physics, Spring 2019 **

**Physics 281: Computational Physics, Spring 2019**## Activities for Meetings 16-17, Tues-Thurs 3/26-28/19 1

**Reading**

**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**

**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**).

**1. Solving an ODE with **odeint

**The circuit shown in the figure below built from a resistor**

**: The low-pass filter.***and a capacitor*

*R**is called a “low-pass filter”. The input voltage*

*C*

*V*_{in}(

*) is specified. Then the output voltage*

*t*

*V*_{out}(

*) is determined by the following first-order ODE:*

*t*This is called a low-pass filter because low-frequency sine waves with angular frequencies * ω *1

*pass through this circuit nearly un- changed, while high frequency sine waves with*

*/RC**1*

*ω**are (imper- fectly) blocked. For this problem use*

*/RC**= 10 kΩ,*

*R**= 1*

*C**F which gives 1*

*µ**= 100 rad/s.*

*/RC*12019-03-27 P281 W9ab.tex * §*c 2019 Donald Candela

(a) Assume *V*_{in}(* t*) =

*sin(*

*A**) with*

*ωt**= 2*

*A**0 V and*

*.**= 50 rad/s. Write a program that defines a function to compute*

*ω*

*V*_{in}(

*) and uses this function to make a plot of*

*t*

*V*_{in}(

*) for 0*

*t*

*< t < t*_{max}, with

*t*_{max}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 *V*_{out}(* t*) at the same set of time points. Use the initial condition

*V*_{out}(0) = 0

*0 V. Plot*

*.*

*V*_{in}(

*) and*

*t*

*V*_{out}(

*) on the same graph (with labeled axes and a legend for the two curves, of course). Because*

*t**1*

*ω <**, the output should be only a little bit smaller than the input and slightly shifted in phase.*

*/RC*(c) Run your program again, changing * ω *to 300 rad/s. Now the out- put should be quite a bit smaller than the input, and shifted in phase by nearly

*2. For both values of*

*π/**you should find that*

*ω*

*V*_{out}(

*) is a sine wave except for an initial transient near*

*t**= 0.*

*t***2. Euler’s method. **Add code to your program to compute

*V*_{out}(

*) 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*

*t*

*V*_{in}(

*) curve from your plot.)*

*t***3. RK2. **Add code to compute

*V*_{out}(

*) using the second-order Runge- Kutta method, and plot the RK2, Euler’s-method, and odeint results together. As with the previous activity, find the minimum number of time points for which the RK2 results appear identical (by eye) to the odeint results.*

*t***4. *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*

*t*_{max}) for the various methods (odeint, RK2, RK4).