Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

HW11: Solving Ordinary Differential Equations (ODEs)

Ordinary Differential Equations are equation that deal with changes with respect to one variable. For example, the equation for the velocity of a falling object is a differential equation because it describes how the velocity changes with respect to time. In this homework, we will practice solving differential equations using the Euler method and Scipy’s ODEINT.

A simple version of an ODE is:

dydt=C\frac{dy}{dt} = C

where C is a constant. Here, y is changing at a constant rate with respect to time. The analytical solution is simply

∫dy=∫Cdty=Ct+y0,\int dy = \int C dt \\ y = Ct + y0,

where y0 is the initial value of y = 0.

Some ODE’s can be solved analytically, but many cannot. We’ll use this simple example to also integrate it numerically and compare the results.

<Figure size 640x480 with 1 Axes>

The three versions of the solution (analytical, Euler, and ODEINT) are the same (or visually indistinguishable) as seen in the above plot.


Problem 1

Please solve the below equation using the Euler method. Plot the solution and compare it to the analytical solution.:

dydt=−y+C\frac{dy}{dt} = -y + C

where C is a constant. Here, y is changing at a rate proportioinal to its value with respect to time.

Action 1: Solve for the analytical solution and specify a function called Analytical1 that takes values of time and returns the value of y given the C value of 3.2 and an initial vallue of 2.3.

Loading...

Action 2: Setup a derivative function as in the example above and fill in arrays with the value of y as a function of time using the Euler method. Use a step size of 0.1 and a total time of 10.0 starting from 0.

Action 3: Use the ODEINT function to solve the same equation.

Action 4: Plot the analytical solution and the numerical solutions (Euler and ODEINT) on the same plot. Make sure to label the axes and include a legend.

Problem 2: Radioactive decay.

dCdt=−Cτ.\frac{dC}{dt} = -\frac{C}{\tau}.

The exact solution is

C(t)=C0e−t/τ,C(t) = C_0e^{-t/\tau},

where C0C_0 is the initial concentration.

Action 1: Use sympy dsolve to solve the above equation. You should get the same answer as given above.

Action 2: Use the Euler method to solve the above equation. Plot the solution and compare it to the analytical solution. Use an initial concentration of 1 and a decay constant of 1. Use a step size of 1 and a total time of 10.0 starting from 0. Thus you’ll have 11 points for time for each trace: analytical, euler, and odeint.

Action 3: Solve the same with odeint

Action 4: Plot the analytical solution and the numerical solutions (Euler and ODEINT) on the same plot. Make sure to label the axes and include a legend.

Action 5: Comment on what you observe about the plot. Perhaps you want to change the time step size (1 to 0.1) and see what happens.

Your answer here

Problem 3: Falling Raindrop

A dpd_p = 1 mm diameter raindrop falls in still air at 300 K, 1 atm. The drop’s falling velocity and position evolve according to the following 2 rate equations:

dvdt=−g+Cd3/4ρairv2ρwdpdxdt=v\frac{dv}{dt} = -g + C_d \frac{3/4\rho_{air}v^2}{\rho_{w}d_p} \\ \frac{dx}{dt} = v

Here ρw\rho_w is the density of water at 1000 kg/m3, ρair\rho_{air} is the density of air at 1.2 kg/m3, and CdC_d is the drag coefficient at approximately 0.5, and g = 9.81 m/s^2 . The initial position and velocity of the drop are both 0.

  • (a) Numerically solve for velocity and position, each of which should be plotted on their own labeled plot for sufficient time to show initial and asymptotal behavior. Use odeint to generate smooth curves.

  • (b) Find the infinite-time velocity ( called the terminal velocity) of the droplet (approximated at a long time value).

Hint : setup your derivative function (def derivative(vx,t):) to take in both velocity and position (in a single array or tuple) and return both dvdt and dxdt in an array or tuple.

Action: Solve the above system of equations for the velocity and position as a function of time and plot the result. Also find the terminal velocity.

Action: Change the size of the droplet to 5mm and then 10 mm and plot the terminal velocity as a function of droplet size. Comment on the result.