Challenge

Week 3 - Solving second order ODEs

Aim: To Create a program in Matlab that will simulate the pendulum motion, using ODE45 Solver.

%Pendulam
b = 0.05;
g = 9.81;
l = 1;
m = 0.1;

%initial Conditions
theta_0 = [0;3];

%Time Points
t_span = linspace(0,20,500)

%Function
[t, results] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);

%looping
ct=1;
for i=1:length(results(:,1))
    x0 = 0;
    y0 = 0;
    x1 = l*sin(results(i,1));
    y1 = -l*cos(results(i,1));

    plot([-1 1],[0 0],'linewidth',5)
    hold on
    plot([x0 x1],[y0 y1],'linewidth',3)
    plot(x1,y1,'O','MarkerSize',20)
    plot(x1,y1,'O','MarkerSize',5)
    hold off
    title('Animation of Pendulam')
    axis([-1.5 1.5 -1.5 1])
    M(ct) = getframe(gcf);
    ct=ct+1;
end

%Animation 
movie(M)
videofile = VideoWriter('Pendulam.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
function [dtheta_dt] = ode_function(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

 Coding Explanation:

1.In this Programm an animation simple pendulum with damping is created.
2.Initial datas are provided like g = gravity in m/s2,L = length of the pendulum in m,m = mass of the ball in kg,b=damping       coefficient.
3.The motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
4.Function is created by second order ODE and its breakdown into two first order ODE to calculate the oscillation of pendulam.
5.Looping is created with incrementing counter.
6.The initial point of pendulam is fixed and the end ball of swinging bob is fixed with swing using marker cmd.
7.Axis is fixed with limited frame, and an Array is created to collect and get the individual forms using gcf - get current frame.
9.Animation video is created by using command movie for the array we created.
10.Using cmd videowriter, the file is writed with required format and file is opened and the frames are inserted by adding the array created then the file is closed.
11.Finally the video of swinging Pendulam is created.

Conclusion: Thus the program in Matlab that will simulate the pendulum motion using ODE45 Solver is ccompleted and the video is also produced

https://youtu.be/y1kFGtcAzVo

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.