Challenge

Week 3 - Solving second order ODEs

First i started my code by writing the values for mass, damping coefficient, length and gravitational acceleration.

In line 10 and 11 i assigned constant values to the cofactor for simplification, in line 13 i wrote the initial condition (0 rad) angular displacement,(3 rad/s) angular velocity, in line 15 i wrote the first order defferential equations then in line 17 to Simulate the motion between 0-20 sec, i made an array from 0 to 20 with 100 elements equally spaced.

in line 19 i used the matlab solver ode 45 to solve the first order defferential equations, in line 20 i insert ct = 1 and i am going to use the variable ct later to create movie, then i used for loop to make an animation,

in lines 24,25 i inserted xi , yi to make a fix end for the pendulum at the origin (0,0), in line 27 i have used these two equations array to plot the coordinates moving end of the pendulum for each time value by substituting angular displacement at that instant , in line 31 i used the plotting command , in line 34 i used getframe command to make a frame for each figure to create movie, in line 37-41 i used these commands to create a movie from the frames.

ERRORS:

To get the coordinates of the moving end of pendulum i was using the two columns (angular displacement, angular velocity) by inserting the colon symbol.

I overcame this error by inserting 1 instead of colon symbol so i will use only the angular displacement values.

YOUTUBE LINK :

https://youtu.be/P-_FZ3eWqV8

CODE:

clear all 
close all 
clc

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

c = g/l
k = b/(m*l)
% initial condition
x0 = [0 ; 3];
% first order defferential equations
f = @(t,x) [x(2) ; -k*x(2)-c*sin(x(1))]
% time points
t_span = linspace(0,20,100);
% solve ode
[t,x] = ode45(f,t_span,x0)
ct=1;

for i = 1:length(t_span)
       
xi = 0;
yi = 0;

p = l*[sin(x(i,:)) -cos(x(i,:)];

% Plotting

plot([xi p(1)],[yi p(2)],'-o','linewidth',3)
axis([-2 2 -2 2])
pause(0.001)
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile = VideoWriter('pendulum_1.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
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.