Challenge

Week 3 - Solving second order ODEs

-:SIMPLE PENDULUM:-

AIM:-

solving a second order ordinary differential equation of simple pendulum and showing the effect of damping by an animation

Objective:-

1. To convert second order ordinary differential equation to 1st order ordinary differential equation

2. to create the function using MATLAB in built fuction for solving ordinary differenial equation

3. To plot the displaccement and velocity damping through the time stipulated time of simulation

4. To make the animation of simple pendulum

Code explanation:-

  • The code starts with the inputs which are provided in the problems. then we make time domain for which the whole code is going to run
  • It was done by 'linespace' command from 0 to 20 seconds with 100 valus in between them
  • after that we provide the initial condition for the angular displacement and velocity which is colum vector from because we need to calulate the both value from the equation of motion for plotting purpose
  • The code comprises of function which solves the second order differential equation which is taken inside the main code the main code has another function which calulates the array of data for time and displacement
  • after caluculation plotting is done in which subplot command is used in used in the first plot frist column is plotted and in second subplot second column is plotted with respect to tim

THE CODE IS GIVEN AS:-

clear all
close all
clc

%input
b = 0.02; %damping coefficient

g = 9.81; %gravity

l=1; %length 


m=0.1; %mass of ball

%initial condition for velocity and displacement
theta_0 = [0;3]; %angular displacement = 0; %angular velocity =2;

%time interval
t_span = linspace(0,20,300); %generate values from 0 to 20 for 100

%differential equation
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);%ode solver for one dgree equation

%plotting

figure(1)
plot(t,results(:,1))%angular displacement
hold on

plot(t,results(:,2)) %angular velocity
ylabel('angular displacement and angular velocity')
xlabel('time(second)')
title('Damped Harmonic motion of a pendulam')
length({'displace','velocity'})
grid on

%Creating Animation of simple pendulam

ct = 1;

for i = 1:length(t_span)
    THETA = results(i,1);
    x0 = 0;
    y0 = 0;

    x1 = 1*sin(THETA);
    y1 = -1*cos(THETA);


    figure(2)
    plot([-1,1],[0 0],'linewidth',5,'color','b')
    hold on
    plot([x0 x1],[y0 y1],'linewidth',3,'color','r');
    hold on
    plot(x1,y1,'o','MarkerSize',25,'MarkerFaceColor','g');
    
    grid on
    title('simple pendulum')
    axis([-1.5 1.5 -1.5 0.5]);
    pause(0.03);
    M(ct)=getframe(gcf);
    ct = ct+1;
    hold off
end

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

FUNCTION CODE:-

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)*sin(theta1);
 dtheta_dt = [dtheta1_dt; dtheta2_dt];

end

OUTPUTS OF WORKSPACE:-

DAMPED HARMONIC MOTION OF A PENDULUM:-

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.