Week 3 - Solving second order ODEs
clear all
close all
clc
%b=damping coefficient
b=0.5;
%gravity
g=9.81;
%l=length of the pendulam
l=1;
%m=mass
m=0.1;
%we need to define initial conditions
theta_0=[0;5];
%define time span
t_span=linspace(0,10,500);
%to solve the ode
%ode is the function of time(t) and theta
[t,results]=ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Plot')
%to create fucntion in program
function[dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sind(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end