Challenge

Week 3 - Solving second order ODEs

%write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping

%Write a program in Matlab\Octave that will simulate the pendulum motion, just like the %one shown in the start of this challenge.

%When you do this you will get the position of the pendulum as a function of time. Use %this information to animate the motion of the pendulum



clear all
close all
clc
% inputs 
b=0.05       %b=damping coefficient
g=9.81;     %g = gravity in m/s2
l=1;         %l = length of the pendulum in m,
m=1;          %m = mass of the ball in kg,

%initial condition 
theta_0=[0,3];     %the initial displacement is 0 and initial velocity is 3.

%time point
t_span=linspace(0,20,500);   % time interval of 0 to 20 secs

%solve ode
[t,results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
% plotting the result of displacement and velocity with respect to time. 
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('plot')
ylabel('velocity')

% to plot the pendulum motion. 
for function is used plot the motion of the pendulum with respect to the displacement and time.the initial point of the pendulum is 0,0 and the x1 and y1 are the point where the mass is added.

ct=1;
for a=1:length(results(:,1))
x0=0;
y0=0;
x1=-1*sin(results(a,1));
y1=-1*cos(results(a,1));
% ploting the pendulum
figure(2)
plot([x0,x1],[y0,y1],'color','r','linewidth',3)
hold on
plot([-1 1],[0,0],'color','g','linewidth',3)
axis([-1 1 -1 1])
plot(x1,y1,'o','MarkerSize',20)
hold off
pause(0.0001)

%to create a movie. After the motion of pendulum is plotted we are saving the plot frame by frame and saving into M.
M(ct)=getframe(gcf);
ct=ct+1;
end

movie(M)
videofile = VideoWriter('pendulam_motion.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

% the video file is writen and saved in the format of avi.

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.