Challenge

Week 3 - Solving second order ODEs

AIM

To Create a pendulum animation by using inbuilt function of ode45 in MATLAB by given the following equation

d2theta/dt^2+b/m(dtheta/dt)+g/l(sin(theta))

MODIFYING THE EQUATION

The given equation is second order partial differential equation (ODE), simplifying the equation second order into first order in order to use in ode45 function

d/dt[ theta1;theta2]=[theta2;-b/m(theta2)-g/l(sin(theta1)

ODE45

Syntax of ode45 :[t,y]=ode45(odefun,tspan,y0)

Where tspan=[to tf],integrates the systems of differential equation from t0 to tf with initial condition y0

MATLAB PROGRAM

clear all                             %used to clear all function and variables in workspace and system memory
close all                             %used to close all plots 
clc                                   %used to clear all text in command window


%INPUTS

%Damping Coefficient 
b=0.05;

%Gravity in (m/s^2)
g=9.81;

%Mass of the ball in (kg)
m=1;

%Length of the pendulum
l=1;

%initial condition
theta_0=[0;3];

%limits
t_span=linspace(0,20,300);

%USING ODE FUNCTION TO SOLVE MOTION OF THE PENDULUM
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,m,l),t_span,theta_0);

%PLOTTING 

%Plotting Time vs Angular Displacement
figure(1)
plot(t,results(:,1))                                      %Plot command use to plot the 2D line graph
hold on                                                   %Used to plot the graph one over another
%Plotting Time Vs Angular Velocity
plot(t,results(:,2))                                      
hold off                                                  
legend('Angular Displacement','Angular Veclocity')        %Used to differtiate the lines in plots
grid on                                                   %Used to turn on the grid lines in plots
title("TIME VS ANGULAR DISPLACEMENT & ANGULAR VELOCITY")  %Used to name 2Dline-plot
xlabel('Time')                                            %Used to denote x-axis


%PIVOT POINT
x=0;
y=0;

%VALUES OF ANGULAR DISPLACEMENT OBTAINED FROM ODE45
angular_displacement=results(:,1);

for i =(1:300)                                                    %Used for looping
    %BALL POINT
    x1=l*sin(angular_displacement(i));
    y1=-l*cos(angular_displacement(i));
    
    figure(2)
    axis([-2 2 -2 0.5])                                           %Used to set the axis                               
    hold on
    img=imread('deskmain.png');                                   %Used to insert the image
    image('CData',img,'XData',[-1.8 1.65],'YData',[-1.9 0.3]);    %Used to set the image in required position
    plot([0 x1],[0 y1],'color','[0.5 0.5 0.5]','linewidth',2)
    plot(x1,y1,'marker','o','MarkerSize',20,'MarkerFaceColor','[0 0 0]')
    hold off
    pause(0.03)
    
    %RECORDING THE FRAMES ANIMATION
    record(i)=getframe;                     %Each frames of 2D line plot is recording
                                    
end

%MAKING ANIMATION VIDEO

videofile=VideoWriter('Motion of pendulum.avi','Uncompressed AVI');                 %used to choose the video format
open(videofile)                                                                     %used to choosing a file to write a data
writeVideo(videofile,record)                                                        %used to write a video on file
close(videofile)                                                                    %close the file after writing the data
%In order to perform ode 45 ,need a function to perform.

function f = ode_func(t,theta,b,g,m,l)

theta_a=theta(1);
theta_b=theta(2);
dtheta1=theta_b;
dtheta2=-(b/m)*theta_b-(g/l)*sin(theta_a);
f=[dtheta1;dtheta2];

end

ERROR FACED WHILE PROGRAMMING:

Error: Faced lot of problems on plotting the motion of pendulum

Steps taken to overcome: By Understanding the concept of behind Pendulum motion on looking in google, MATLAB Website and from technical team Skill-Lync

EXPLANATION OF OUTPUT:

The Time vs angular displacement & angular velocity has plotted by given equation. At initial condition the angular displacement reach peaks because of max of angular velocity. As well as the velocity decreases the angular displacement also decreases

PENDULUM ANIMATION:

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.