Challenge

Week 3 - Solving second order ODEs

% link to the animation video in google drive https://drive.google.com/file/d/1yO3tEHmbfyUSIylVrEt8pTYImSVkWNmE/view?usp=sharing

%Please find the two .m files which also has the function Ode_func that has to be used in order to execute this script. I have attached a .docx file for a detailed explanation. Thank you

%Code to the program.

clear all;
close all;
clc;

%the following are the inputs; g = gravity in m/s2, L = length of the pendulum in m, m = mass of the ball in kg,b=damping coefficient.

b = 0.05;%damping coefficient
g = 9.81;
l =1;% length
m = 0.1;

% initial condition
% initial displacement is 0 and initial velocity is 5m/s
theta_0 = [0;3];

% time_span is a time array which ranges from 0 to 20 seconds with 500 values

t_span = linspace(0, 20, 500);


% solve ODE

[t, results] = ode45(@(t,theta) ode_func1(t, theta, b,g,l,m), t_span, theta_0);
figure(1)
plot (t, results(:,1))
hold on
plot (t, results(:,2))
ylabel('plot')
xlabel('time')

ct=1; %counter ct is used to increment the frames

for i = 1: length(results)%since the position of pendulum is the function of time
% origin or the fixed coordinates that hold one end of the pendulum
x0=0;
y0=0;
% the other end of the pendulum that oscillates with respect to change in angular displacement. Explained further in the doc file attached.
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
figure(2)
plot ([-0.8 0.8],[0 0],'linewidth',3)% plotting the horizontal line where the fixed point is located.
hold on
plot([x0,x1],[y0,y1],'linewidth',3)
axis equal
plot(x1, y1, 'O', 'markersize', 20, 'markerfacecolor','r');
grid on;
hold off;

pause(0.03)
%storing individual frames in an array. We name it here as M.
M(ct)= getframe(gcf);
ct=ct+1;

end
movie(M)% with this command Matlab generates a movie with all those frames
%Open the videoWriter object, write the movie & close the file.
myvideofile= VideoWriter('Pendulum Animation.avi', 'Uncompressed AVI');
open(myvideofile)
writeVideo(myvideofile,M);
close(myvideofile);

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.