Solving second order ODEs in matlab
AIM:
write a program that solves the following ODE.
OBJECTIVE:
-To animate the motion of the pendulum.
-To make the graph of angular velocity vs time & angular displacement vs time.
INPUTS:
This ODE represents the equation of motion of a simple pendulum with damping:

In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Write a program in MatlabOctave that will simulate the pendulum motion, just like the one shown in the start of this challenge.
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
CODE:
clear all
close all
clc
l=1;ct=1;
ma=1;
b=0.05;
g=9.81;
theta0=[0;3];
t_span=linspace(0,20,100);
[t,result]=ode45(@(t,theta) vd(theta,l,ma,b,g),t_span,theta0)
figure(1)
hold on
plot(t,result(:,1))
plot(t,result(:,2))
hold off
figure(2)
for i=1:length(t_span)
x0=0;
y0=0;
x1=l*sin(result(i,1));
y1=-l*cos(result(i,1));
plot([x0, x1],[y0, y1],'linewidth',4);
hold on
plot(x1,y1,'Marker','.','MarkerSize',40);
plot(x0,y0,'Marker','.','MarkerSize',20);
plot([-2 2],[0 0],'linewidth',2);
axis([-2 2 -2 2]);
hold off
pause(0.1)
M(ct)=getframe(gcf)
ct=ct+1
end
movie(M)
videofiles=VideoWriter('2ndorder')
open(videofiles)
writeVideo(videofiles,M)
close(videofiles)
FUNCTION CODE:
function results= vd(theta0,l,ma,b,g)
theta1=theta0(1)
theta2=theta0(2)
dtheta1_dt=theta2;
dtheta2_dt=((-(b/ma)*theta2)-((g/l)*sin(theta1)));
results=[dtheta1_dt;dtheta2_dt]
end
OUTPUT:


GOOGLEDRIVE LINK:
https://drive.google.com/file/d/1ML3I3Mw0gSd8RhZBteU0WdVvAlS-1l9Y/view?usp=sharing
SIMPLE HARMONIC MOTION:
Simple harmonic motion is defined as a periodic motion of a point along a straight line, such that its acceleration is always towards a fixed point in that line and is proportional to its distance from that point.
FORMULA:

EXPLANATION:
-As usual we are giving the clear all , close all and clc for removing the variables ,commands and plots.
-We are giving inputs of damping, mass, length, gravity, time span, theta0.
-As per newtons sencond law ,equation is in 2nd order differential. we are changing the 2nd order to 1st order.
- we can caculate the velocity and the displacement.(mentioned the derivation above for reference) using the function.
-After calculating the velocity and displacement,we plot time vs velocity, time vs displacement.
-For motion of pendulum , we need find the 2 co-ordinates x0,x1,y0,y1 and with help of the for loop changing the instance and we can do the plot.
-Using hold on , we can retain the existing plot when new plot is added.
-Finally , we can make the movie with the matlab default code.
Mention the errors faced while programming & overcome those errors:

CONCLUSION:
As per the newtons second law of equation ,we calulated the velocity and displacement for different instance of time span and also we done motion of pendulum in mat lab .