Challenge

Week 4.1 - Solving second order ODEs

AIM:

To solve an ODE and use the calculated information to animate the motion of the pendulum via matlab

THEORY

Pendulum have many applications and were utilized often before the digital age. They are used in clocks and metronomes due to regularity of their period. A simple pendulum consists of a point mass suspended on a string or wirethat has negligible mass. if the pendulum weight or bob is pulled to a relatively small angle from the vertical and let go.it will swing back and fourth at a regular period and frequency

An ordinary differential equation (ODE) is differential equation containing one or more functions of one independent variable and the derivatives of those functions. The ODE equation and the calculations

Code Explanation

%The function program

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

% solving second order ODE of pendulum

clear all
close all
clc

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

% initial condition
theta_0 = [0;3];

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

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

plot(t,results(:,1))
hold on 
plot(t,results(:,2))
ylabel('Plot')
xlabel('time')

%iteration using for loop to create animation of simple pendulum 
ct=1;
theta_1 = results(:,1);
for  i=1:length(results(:,1))

    %initial co-ordinates
    x_start= 0;
    y_start= 0;
    theta_2 = theta_1(i);

    %final co-ordinates
    x_end= 1*sin(theta_2);
    y_end= -1*cos(theta_2);

    %ploting for animation
    figure(2)
    %base od pendulum 
    plot([-1,1],[0,0], 'linewidth',4,'color','black')
    hold on 
    %string of pendulum
    line([x_start x_end],[y_start y_end],'linewidth',6,'color','b')
    hold on
    %bob of pendulum
    plot(x_end,y_end,'marker','o','markersize',28,'markerfacecolor','r')
    axis([-2 2 -2 2])
    pause(0.03)
    hold off
    M(ct)=getframe(gcf);
    ct=ct+1;
end

%creating movie
movie(M)
videofile = VideoWriter('simple_pendulum.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
  • Before writing the code , it's important to start with clear all, close all, clc commands
  • Next step is to define the inputs and variables. The damping coefficient, gravity, length of the spring and mass of the bob have been defined.
  • Next step is to define the initial conditions and the angular displacement and angular velocity has been defined. Timespan has also been defined.
  • Next step is to solve ODE , Ode45 was used, it is a function in matlab thet solves ordinary differntial equation
  • After calculating the results a graph has to be plotted the graph contained two plots. One for angular displacement with respect to time and one for angular velocity with respect to time
  • The final step of the code is to create a movie of the animation of the simple pendulum, so the movie command was used

Conclusion

The x_start y_start , x_end y_end values

x_start = 0

y_start = 0

x_end = -0.7351

y_end = -0.6779

Animation Video:

https://youtu.be/j8PcxEfudZg

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.