Challenge

Week 3 - Solving second order ODEs

Aim: - Write a program in Matlab that will simulate the pendulum motion, just like the one shown in the question of this challenge.

Object: - Write a program that solves the 2degree ODE. This ODE represents the equation of motion of a simple pendulum with damping.

Procedure: - First we understand the ODS and convert 2 order derivative into single order derivative and create the equation for this problem in single order and put all values in that equation and calculate and this all put in the for loop for continuous theta angle and get the animation of pendulum.

Formula: -

ODE45

x1= Length*sin(THETA1);
y1=-Length*cos(THETA1);

dtheta1_dt= theta(2);
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt= [dtheta1_dt; dtheta2_dt];

Explanation: - In this problem we convert 2 order derivative in to 1 (single) order derivative and create it in to single order equation and get the

Input: -

  1. L=1 metre, Length
  2. m=1 kg, Mass
  3. b=0.05. Damping
  4. g=9.81 m/s2. Gravity

after getting input we write the initial condition for angular displacement=0, angular velocity=3 rad/sec at initial time t=0. We get the time span of 0 to 20 of 500 values , Then we used the ODE function for this program this ODE is an inbuilt solver which help to solved the 1 order derivate so we convert the 2 order derivate in to single form by using ode function and then this we put in main code and performed operation we have two input variable one independent and other dependent so here is the Time t is independent and result is dependent se we get the result in with help of ode and function with respect time spam, angular displacement, angular velocity, hence we get the result.

note:-(here we create function comand which help us to one order equation and hep to calculate foe multiple values with help of for loop)

This result is nothing but Theta1 store in entire 1st column this and we have to create for loop from 0 to length of t_span and this all values of result is used to plot of, and get the x1y1 coordinates with the help on this formulas x1= Length*sin(THETA1);
y1=-Length*cos(THETA1); we used angle in radian angle because we get angular velocity=3 rad/sec in this problem so after that we moved to create plot using marker command ‘o’ and also used MarkerSize of 35 , markerfacecolor, blue and create movie for this project hence as this we create the pendulum motion.

Main Code:-

clear all
close all
clc
%Input Values given 
b=0.05;
g=9.81;
l=1;
m=1;

%initial conditons 
theta_0=[0;3];          %angular displacement = 0, angular velocity = 3 in form of two colom

%time period 
t_span = linspace(0,20,500); %we get the in 0-20 sec of 500 values stored in T_span

%solve ode
[t, result] = ode45(@(t,theta) ode_function(t, theta, b , g, l, m),t_span,theta_0); %ode45 is inbuld (solver)command used to solved equation in single order thats why have to convert this in to one order derivative and @(t,theta) is used for undefined variable and create function for derivative calculation  
Theta1=result(:,1);  %this store the result in colom no 1

ct=1
for i=1:length(t_span)  %used of for loop for repeteating value until t_span and put this values in equation and get the value of theta 
    THETA1=Theta1(i);
    
    x=0;                %Initial position or fixed position of pendulum
    y=0;
    
    x1=l*sin(THETA1);   %get the values for different angles of theta in between 0- 20 sec of 500 in form of angular displacement or 0 colom this all didderent values put into this equation and get the cordinates 
    y1=-l*cos(THETA1);  %----||-----
    
    
    plot(x1,y1,'marker','o','MarkerSize',35,'markerfacecolor','b') %plot of bob on x1 y1 markerSize is used to adjust size of bob markerfacecolor is used to fill the bob color
    hold on 
    plot([x x1],[y y1],'linewidth',2) %used to plot on graph
    axis([-1.5 1.5 -1.5 0.5])
    pause(0.001)
    M(ct) = getframe(gcf);   %store all the capture frame in one array 
    ct=ct+1;
    hold off
    
end
%movie command
movie(M)
videofile = VideoWriter('pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Function Code:-

function [dtheta_dt]= ode_function(t,theta,b,g,l,m)%creating function for ode
theta1= theta(1);
theta2= theta(2);
dtheta1_dt= theta(2);  %create in to first order derivative 
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1); %create in to first order derivative 
dtheta_dt= [dtheta1_dt; dtheta2_dt]; %storing this equation  values in form of two column
end

this both must have to store in same location. And function name and file name must be same.

Amination:- Get the animation of pendulm. https://drive.google.com/file/d/1ua8Il6KgYQk8k4JoCCoBCjnXTXweyc0Y/view?usp=sharing

Link:- https://drive.google.com/file/d/1ua8Il6KgYQk8k4JoCCoBCjnXTXweyc0Y/view?usp=sharing

Result:- Calcuate all coordinates of all points of all posible angle of theta or describes the position of the bob with respect with time and get the animation of pendulm.

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.