Challenge

Week 3 - Solving second order ODEs

1. Aim :

  • To write a program that solves the following ODE `(d^2∅)/(d^2 t)+b/m (d∅)/dt+g/l sin⁡∅= 0`
  • To write a Matlab program that will simulate the pendulum motion

2. Formulas used :

`∅= ∅_1`

`(d∅)/dt= (d∅_1)/dt=∅_2`

`(d∅_2)/dt+b/m ∅_2+g/l sin ∅_1⁡= 0`

`(d∅_2)/dt⁡= -b/m ∅_2-g/l sin ∅_1`

`d/dt [■(∅_1@∅_2 )]= [■(∅_2 &@-b/m ∅_2-g/l sin ∅_1&)]`

3. Objective of the project:

  • Animate the pendulum motion
  • To show the graph how angular displacement and angular velocity changes with respect to time

4. Body of content:

  • Matlab is used to write the program
  • Stating the inputs as given in the Question
  • Stating the initial contions as given
  • Used the ODE 45 is used to solve the differential equation with the help of the function
  • function ode_func is used to solve the given qiven equation using the formulas
    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
    ​
  • plotting the graph of angular displacement and angular velocity changes with respect to time
  • Using the for loop to excute the pendulum motion with the time
  • plotting the pendulum motion

create the animation with videoWriter as folows

movie(M)
videofile = VideoWriter('pendulum motion.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
the whole program is in below
clear all
close all
clc

% inputs
b = 0.05;
m = 1;
l = 1;
g = 9.81;

% Intial conditions
theta_0 = [0,3];
t_span = linspace(0,20,700);

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

%Plotting displacement and velocity varying w.r.t time
figure(1)
hold on
plot (t,results (:,1))
plot (t,results (:,2))
xlabel ('Time')
ylabel ('Displacement & velocity')

%For loop excution
theta1 = results (:,1);
a=1;
for i = 1:length(theta1)
    THETA = theta1(i);
    x0 = 0;
    y0 = 0;
    x1 = l*sin(THETA);
    y1 = -l*cos(THETA);
    % Plotting for the swinging pendulum
    figure (2)
    plot ([0.5 -0.5],[0 0],'linewidth',6)
    hold on
    plot (x1,y1,'o',MarkerSize=20,MarkerFaceColor='r')
    plot ([x0 x1],[y0 y1],'color','r')
    hold off
    axis ([-1 1 -1.5 0.5]);
    pause(0.005)
    M(a) = getframe(gcf);
    a=a+1;
end
%creating the animation
movie(M)
videofile = VideoWriter('pendulum motion.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

5. Results :

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.