Challenge

Week 3 - Solving second order ODEs

sloving the problem of second order ordinary differential equation using Matlab and plotting the graph and creating animation video of pendulum.

clear all %this command clear all the data stored in the previous workspace.
close all % this command closes the previous fig and plots
clc% this command clears the command window

b = 0.05; % damping
g = 9.81; % gravity
l = 1; % length
m = 1; %mass


%initial condition

angular displacement = 0

angular velocity =3 rad/sec, at time =0
theta = [0;3];
%time points

motion is simulated between 0-20 sec with 300 values
t_span = linspace(0,20,300);

%slove the ODE

% solving the program using this command ode solver ode45. the results are stored in two row and cloumn matrix


[t,result] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta);

%plotting
figure(1)
plot(t,result(:,1))
hold on
plot(t,result(:,2))
xlabel('time')
ylabel('plot')

%for loop
% before start the foe loop, a counter value was defined

ct = 1
for i = 1:length(result(:,1))
x1 = 0
y1 = 0
x2 = 1*sin(result(i,1)+pi);
y2 = 1*cos(result(i,1)+pi);

%plotting the animation
figure(2)

plot([x1 x2],[y1 y2],'-o','Markersize',15,'markerfacecolor','b','MarkerEdgeColor','r','LineWidth',3)
axis([-1.2 1.2 -1 1])
hold on
plot([2 -2],[0 0],'linewidth',5,'color','[0 0.2 0]')
M(ct) = getframe(gcf)
hold off
ct = ct+1;
end

%animation of the pendulum
movie(M)
videofile = VideoWriter('Pendulumanimation.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Function program

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.