Challenge

Week 3 - Solving second order ODEs

AIM:

To write a program for simple pendulum using matlab.

THEORY:

The simple pendulum is another mechanical system that moves in an oscillatory motion. It consists of a point mass ‘m’ suspended by means of light inextensible string of length L from a fixed support. The motion occurs in a vertical plane and is driven by a gravitational force. The forces which are acting on the mass. The tangential component of the gravitational force, mg sin θ, always acts towards the mean position θ = 0 opposite to the displacement, restoring force acting tangent to the arc.

PROGRAM:

clear all
close all
clc

%initial condition of pendulum
l=1;
m=0.1;
b=0.02;
g=9.81;

%initial condition
theta_0=[0;3];

%time points
t_span=linspace(0,20,500);

%solve ode
[t,results]=ode45(@(t,theta)ode_fun(t,theta,b,g,l,m ),t_span,theta_0);

%plotting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
title('SIMPLE PENDULUM')
xlabel('TIME')
ylabel('ANGULAR VELOCITY')

%pendulum animation
amplitude=results(:,1);
p=1;
for i=1:length(amplitude)
    angle=amplitude(i);
    figure(2)
    title('SIMPLE PENDULUM')

    %fix point of pendulum
    x0=0;
    y0=0;
    %another end of pendulum
    x1=1*sin(angle);
    y1=-1*cos(angle);

    %plotting
    plot([-1.1 1.1],[0 0],'linewidth',2);
    hold on
    plot([x0 x1],[y0 y1],'color','g','linewidth',2);
    plot(x1,y1,'o','Markers',20,'markerfacecolor',[1 1 0]);
    hold off
    grid on
    axis([-1.5 1.5 -1.5 0.5])
    pause(0.001)
    M(p)=getframe(gcf);
    p=p+1;
end

movie(M)
videofile=VideoWriter('SIMPLE PENDULUM.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

function [dtheta_dt]=ode_fun(t,theta,b,g,l,m)
theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt]
end

ANIMATION:

https://www.youtube.com/watch?v=2u9PKPvZass.

REFERENCE:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.