Week 9 Lecture 1: Higher Order ODEs and Dynamical Systems
Rahman Notes:
Lets's do the examples we did in the theory lecture.
Consider the simple pendulum, which we showed in the theory lecture is modeled as
If we were to use forward Euler, we would get the following scheme:
theta = theta + dt*omega;
omega = omega + dt*(-sin(theta));
theta
theta =
-0.997033921796586
Now lets try it with ode45
[T,Y] = ode45(@(t, y) SimplePendulum(t,y),[0 10],[theta omega]);
We can use this to plot our phase plane. We first we need a bunch of initial points, and we let them run.
theta = [-3*pi:6*pi/5:3*pi];
[T,Y] = ode45(@(t, y) SimplePendulum(t,y),[0 10],[theta(j) omega(i)]);
We can even simulate it as a video. Try it for different initial points.
[T,Y] = ode45(@(t, y) SimplePendulum(t,y),[0 10],[theta omega]);
h = plot([0 x(t)],[0 y(t)],'k',x(t),y(t),'.',0,0,'.',x(1:t),y(1:t));
set(h(2),'MarkerSize',50);
set(h(3),'MarkerSize',20);
axis([-1.5 1.5 -1.5 1.5])
function dy = SimplePendulum(t,y) %%% This usually goes at the end of the code.
I had to delete the old notes for the snippets of code to run, but I'll include it as a separate file.