**Lab 2 - Numerical Solutions of ODEs** **[ENGR 12 Spring 2026](index.html)** (#) Background In this lab, you will study a family of iterative numerical algorithms known as the **Runge- Kutta** methods. In lecture, you have used MATLAB's `ode45`, which uses a Runge-Kutta algorithm to numerically approximate the solution to initial value problems. Initial value problems occur frequently in linear physical systems, and Runge-Kutta solvers are used when a linear system cannot easily be solved analytically. Unlike the other labs we will do this semester, this lab will not involve collecting data. (##) Objectives * Program numerical solvers for initial value problems. * Simulate physical systems using Simulink. * Compare approximate numerical solutions with exact analytical solutions. (##) Before you begin * Find your seat * [Section B](B2.pdf) - 2/11 * [Section C](C2.pdf) - 2/16 * [Section D](D2.pdf) - 2/18 * [Section A](A2.pdf) - 2/23 * Log into the desktop PC * Open MATLAB and Simulink (##) Resources * Reference the [pre-lab slides](lab2.pdf) as needed. * Download the [E12 lab report template](E12_Lab_Report_Template.zip) and upload it to Overleaf. Share it with your lab partner. # Tasks (##) Use numerical methods to approximate solutions to ODEs (###) Practice Runge-Kutta Consider the following first-order ODE from the pre-lab slides: $$\frac{d}{dt} y(t) = -2y(t)$$ with the initial condition $y(0) = 3$. The exact solution to this ODE is $y(t) = 3e^{-2t}$. Write a first-, second-, and fourth-order Runge-Kutta solver for this ODE. (###) Practice Simulink Consider the RLC circuit in Figure [RLC] from the pre-lab slides. ![Figure [RLC]: An RLC circuit that may be familiar from E11.](RLC.png class="pixel" alt="A circuit diagram for an RLC circuit.") Use Kirchoff's Laws to write the second-order ODE that describes this system. Use that second-order ODE to make a Simulink model of the system. Try it yourself, but consult [my Simulink model](RLC.slx) if you get stuck. You should also use [this script](RLC_parameters.m) to set up the system parameters. ## Analyze a linear system from fluid mechanics Consider the cylindrical tank being filled in Figure [tank]. Assume the tank is empty at time $t = 0$, after which a pump at the inlet intantaneously changes the pressure from $0$ to $P_{in}$, and fluid starts to flow into the tank. The inlet pressure from the pump is 20 psi. The physical parameters are given in the table below. ![Figure [tank]: A cylindrical tank being filled with fluid from an inlet at the bottom.](tank.png class="pixel" align="center" width="288 px" alt="A tank being filled with fluid from an inlet at the bottom. The input pressure is P in. The height of the fluid is y of t. The diamter of the tank is D.") | Parameter | Value | |:--------|:--------------------| | $K$ | $5.464 \times 10^8$ Pa*s/m^3 | | $\rho$ | 997 kg/m^3 | | $g$ | 9.81 m/s^2 | | $P_{in}$ | $1.379 \times 10^5$ Pa | | $D$ | 1 m | To analyze this system, you need to understand two principles from fluid mechanics. First, the hydrostatic pressure of a fluid at a depth $h$ is given by $P = \rho g h$ where $\rho$ is the fluid's density and $g$ is the acceleration due to gravity. Second, the volume flow rate of an incompressible fluid through a pipe is proportional to the pressure difference: $K\frac{dV}{dt} = P_1 - P_2$, where $K$ is a proportionality constant that incorporates the viscosity of the fluid and the dimensions of the pipe. For more details, you can [read about the Hagen-Poiseuille equation](https://en.wikipedia.org/wiki/Hagen%E2%80%93Poiseuille_equation). 1. Derive the first-order ODE that describes the height of the water in the tank. 2. Is this ODE similar to any of the ODEs we saw in Lab 1? What are the analogous variables and constants? What is the time constant $\tau$? What is the solution? 3. Plot the analytical solution at increments of $\frac{\tau}{100}$ from $t = 0$ until the system comes to equilibrium at $t=5\tau$. 4. Write a first-order Runge-Kutta solver for this system with a step size $h$. Choose a reasonable value for $h$ and plot the numerical solution on the same graph as the analytical solution. 5. Repeat the previous step with a second-order Runge-Kutta algorithm. 6. Repeat the previous step with a fourth-order Runge-Kutta algorithm. 7. Run a parameter sweep: a. Choose **at least** three different values for $h$ (not all of them have to be reasonable), and run a first-order Runge-Kutta for those step sizes. Graph all your numerical solutions on the same plot as the analytical solution. Calculate the root-mean-square error (RMSE) for each numerical approximation. b. Choose one value for $h$ from the previous step. Now, compare a first-, second-, and fourth-order Runge-Kutta algorithm at that step size. Graph all your numerical solutions on the same plot as the analytical solution. Calculate the RMSE for each numerical approximation. 8. Create a Simulink model to solve this system. You can find the step input block under `sources`. Plot the solution from the simulation on the same graph as the analytical solution. Calculate the RMSE for this numerical approximation. ## Analyze a coupled mechanical system Consider the coupled mass spring system shown in Figure [coupled_system]. There is no friction, the masses are equal, and the springs are ideal and linear. ![Figure [coupled_system]: A coupled mass-spring system.](coupled_system.png class="pixel" alt="Two masses are attached to walls each with a spring k1, and they are coupled to each other with a spring k2. The variable x1 shows the postion of one mass while the variable x2 shows the position of the other. Both masses have mass m.") The parameters of this system are given in the following table. | Parameter | Value | |:--------|:--------------------| | $x_1(0)$ | 1 m | | $x_2(0)$ | 0 m | | $\dot{x_1}(0)$ | 0 m/s | | $\dot{x_2}(0)$ | 0 m/s | | $k_1$ | 64 N/m | | $k_2$ | 2 N/m | | $m$ | 1 kg | The first step to analyzing a mechanical system like this is to draw a free-body diagram (FBD). Examine the FBDs in Figure [FBD]. Unlike in your physics class, we use [d'Alembert's principle](https://en.wikipedia.org/wiki/D%27Alembert%27s_principle) and write the inertia ($m\ddot{x}$) as a force. ![Figure [FBD]: Free-body diagrams of the two masses.](masses.png class="pixel" width="432 px" alt="The first mass has two forces pointing in the negative x direction: k1 x1 and m x1 double dot. It has one force pointing in the positive x direction: k2 times x2 - x1. The second mass has three forces pointing in the negative x direction: k2 times x2 - x1, m x2 double dot, and k1 x2.") 1. Use these FBDs and Newton's laws to write a system of two coupled ODEs that describe this system. 2. The analytical solution for this system is given by the following equations: $$ x_1 = \frac{1}{2}\left(\cos\left(\omega_1t\right)+\cos\left(\omega_2t\right)\right) $$ $$ x_2 = \frac{1}{2}\left(-\cos\left(\omega_1t\right)+\cos\left(\omega_2t\right)\right) $$ where $\omega_1 = \sqrt{68}$ and $\omega_2 = 8$. Use this analytical solution to check your differential equations from the previous step. Plot the analytical solution from $t = 0$ to $t = 30$ seconds. 3. Create a Simulink model to solve this system. Plot the solution from the simulation on the same graph as the analytical solution. Calculate the RMSE for each numerical approximation. 4. Simulate this system for two more sets of initial conditions (initial velocities are still zero): a. $x_1(0) = 1$, $x_2(0) = -1$ b. $x_1(0) = 1$, $x_2(0) = 1$ Explain why both of these sets of initial conditions yield simple harmonic motion and why one set results in a higher frequency of oscillation than the other. # Writing your lab report Write a lab report using $\LaTeX$ that adheres to the guidelines in the [E12 Lab Report Template](E12_Lab_Report_Template.zip). In addition to the guidelines in the template, below are guidelines specific to some sections of the report for this lab. ## Theory * Present and discuss the equations for the Runge-Kutta algorithms you used for this lab. * Discuss the relationship between Simulink, Runge-Kutta, and analytical solutions to ODEs. ## Methods Even though you did not take experimental data from a physical system, you should still discuss the setup of your experiments in simulation, including your parameter sweep for Runge-Kutta and the models you built in Simulink. To export your Simunlink models as high-quality images, make sure the model you want to export is open and run the following line of code from the command line. Replace `MyModelName` with your model name. ~~~ MATLAB print -sMyModelName -dpng -r300 MyModelImage.png ~~~ ## Results * Show your graph comparing the analytical solution to the Runge-Kutta algorithm with different step sizes. * Show your graph comparing the analytical solution to the Runge-Kutta algorithn of different orders. * Show your graphs comparing the analytical solution to the Simulink output. * Show your graphs for the additional initial conditions for the coupled oscillator. * Present your RMSE results in one or more tables. ## Submission Within two weeks after your lab meeting, please submit the following to Moodle: * Your lab report in PDF format. * Any code you wrote as .m file(s). * Any Simulink models you made as .slx files.