Damped harmonic oscillations are described by a second-order differential equation that has the form: \frac{d^2x}{dt^2} + a\frac{dx}{dt} + x = 0. For simplicity, we assume that $a = 0.01$.
Part 1: Using the function dfdt = odefun(t, f) that you wrote for Problem 2 in HW#5, solve this ODE to find the value of x at $t = 50$. Assign the result to xf.
Use the following initial conditions at $t = 0$: $dx/dt = 1$ and $x = 10^{-3}$.
Part 2: Write an event function to determine the period of oscillations. To make things simple, let's define the end of the first period as an event when the oscillator returns to
$x = 0$ with a positive velocity. Use the following initial conditions at $t = 0$: $dx/dt = 1$ and $x = 10^{-3}$. Assign the result to T.
Script
1 a = 0.01;
2 tspan = linspace(0, 50, 50);
3
odefun = @(t, f) [f(2); -a*f(1) - f(2)];
5
6 event_func = @(t, f) f(1);
7
8 [t, f] = ode45(odefun, tspan, [1, 10^(-3)]);
9 xf = f(end, 1);
10
11 plot(t, f(:,1));
12 xlabel('Time');
13 ylabel('Position');
14 title('Damped Harmonic Oscillator');
15
16 options = odeset('Events', event_func);
17 [t, f, te, fe, ie] = ode45(odefun, tspan, [1, 10^(-3)], options);
18
19 te
20 fe
21