Challenge

Week 3 - Solving second order ODEs

This program is used to simulate the pendulum motion.

Given parameters :

  • 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.

  • The code is used to simulate the motion of a pendulum using the given parameters and a ode function.
  • It gives us a 2 column vector .
  • First column having the values of displacement of the pendulum and the second column gives us the values of the velocity of the pendulum.
  • The position of the pendlum is found by a the pythogoras therom .
  • Wherein the values of displacement are taken as x coordinates and the value of lenght of the pendulum given is taken as the hypotanuse to find the values of y coordinated of the pendulum ball.
clear 
close all
clc

%A program in Matlab that will simulate the pendulum motion,using ODE.

%Given parameters 
l = 1 ;
m = 1 ;
b = 0.05 ;
g = 9.81 ;

%initial conditions 
theta_0 = [0,3];

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

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


%animation of motion of the pendulum
ct = 1;     

    for i = 1: length(t_span)
             len = results(:,1);
             
             %to clear the preveous drawing
             clf

             %positionof fixed point
             x0 = 0;
             y0 = 0;
             x  = -1 : 1;
             plot(x0,y0,'o','MarkerSize',15,'MarkerEdgeColor','cyan','MarkerFaceColor','black')
             hold on
             plot(x.',y0,'_','MarkerSize',100,'MarkerEdgeColor','black')
             hold on

             %position of ball
             x1 = len(i);
             y1 = -sqrt(l^2-len(i).^2);
             
             plot([x0 x1.'],[y0 y1.'] ,'linewidth',2,Color='red')
             hold on
             grid on

             plot(x1.',y1.','o','MarkerSize',25,'MarkerEdgeColor','blue','MarkerFaceColor','magenta')
            
             pause(0.008)
             axis([-2 2 -1.5 0.5])
             
             M(ct) =getframe(gcf);
             ct =ct+1;

    end
   
movie (M)
videofile =VideoWriter('program_for_ode','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

the following function is used in the above code:

function [dtheta_dt] = ode_func(t,theta,l,m,b,g)

    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

the link for the youtube video:

https://youtu.be/RrfzSuFAGEE

Errors :

  • No majors errors were made.
  • Little difficulty was faced to make adjustment of the axes.
  • Also to find a function to animate the video.
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.