Challenge

Week 4.1 - Solving second order ODEs

Aim- to solve the second order defferential equation.

Governing Equation-

Objective- write a program for solving the second order differential eqation and creat annimation for swing motion of pendulum

Theory-

Pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equalibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging back and forth. The time for one complete cycle, a left swing and a right swing, is called the period. The period depends on the length of the pendulum and also to a slight degree on the amplitude, the width of the pendulum's swing.

MATLAB CODE

FUNCTION

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

MAIN BODY OF CODE

clear all
close all
clc

%programme for solving the second order differential equation.

%given data
l=1   %length of pendulm in meter(m)
m=1   %mass of ball in killogram(kg)
b=0.05 %damping coefficient
g=9.81 %gravity meter per sec^2 (kg/m^2)

%time for simulation
t_span=linspace(0,20,300);

%condition for angular velocity and angular displacementc
theta_0=[0;3];


%solve ode
[t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
theta1=results(:,1)
%plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
title('damped harmonic motion of pendulum')
legend('Angular Displacement','Angular Velocity')
ylabel('VELOCITY')
xlabel('TIME')

ct=1;
for i=1:length(theta1)
   x_start=0;
   y_start=0;
   
   x_end=l*sin(results(i,1));
   y_end=-l*cos(results(i,1));
  
        %plotting
        figure(2)
        plot([-2 2],[0 0],'linewidth',3)
        line([x_start x_end],[y_start y_end],'linewidth',4)
        axis([-2 2 -2 2])
        hold on
        plot(x_end,y_end,'o','markersize',20)
        hold off
        
        grid on
        pause(0.03);
        
        M(ct)=getframe(gcf)
        ct=ct+1
end
Movie(M)
videofile=videowriter('second order ode.avi','uncompressed AVI');
open(videofile)
writevideo(videofile,M)
close(videofile)

GRAPH

CODE EXPLANATION

    • Programming language uses Matlab.
    • start the program by using comments.
    • write the input data.
    • define the time interval
    • write the value for angular velocity and angular displacement.(theta_0)
    • define the function for the given variable for differentiation.
    • plot the graph between the angular velocity and time span
    • use for loop for the motion of the pendulum
    • use plot command to plot the graph and simulation.
    • end the for a loop.
    • figure save in M file to make an animation.
    • at end save the movie in .avi format.

Animation

# CLICK FOR VIDEO---- animation video

https://drive.google.com/file/d/1WeojYRpjaoDsHAXeI8V5LdTJlicVgPPJ/view

Error

  • function calling error
  • spelling mistake
  • theta1 value error
  • for loop error

Result- Successfully create an animation video of the pendulum and Matlab code for the secon order differential equation.


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.