Challenge

Week 3 - Solving second order ODEs

% 2nd order differtial equation is used to solve problem like pendulam &
% spring loaded mass
% for displacement & acceleration graph
b=0.05
%damping coefficient
g=9.8
% gravity coefficient
l=1
% length of pendulam
m=0.1
% mass of pendulam

% initial condition
theta_0=[0;5]
% initially pendulam is vetical

% time points
t_span=linspace(0,10,500);
%time is divded 500 parts between 0 10 sconds to get image frame at each
%parts

%solve ode
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%ode_func is function solve the differential equation of pendulam problem.
plot(t,results(:,1))
hold on
plot(t,results(:,1))
ylabel('plot')
xlabel('time')
function[dtheta_dt]=ode_func(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
https://drive.google.com/file/d/1G_tCjpUIyZPHtkLHHmepd5c62neL7WI5/view?usp=sharing
% 2nd order differtial equation is used to solve problem like pendulam &
% spring loaded mass(pendulam animation)
b=0.05
%damping coefficient
g=9.8
% gravity coefficient
l=1
% length of pendulam
m=0.1
% mass of pendulam

% initial condition
theta_0=[0;5]
% initially pendulam is vetical

% time points
t_span=linspace(0,10,500);
%time is divded 500 parts between 0 10 sconds to get image frame at each
%parts

%solve ode
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%ode_func is function solve the differential equation of pendulam problem.


ct=1
for i=1:length(t_span);
 % for loop is to get image frames of pendulam starting from 1st frame.       
x0=0;
y0=0;
% fixed point at the end of pendulam.
x1=-l*sin(results(i,1));
y1=-l*cos(results(i,1));

% moving point of pendulam.

% plotting

plot([x0,x1],[y0,y1],'linewidth',3)
hold on
plot(x1,y1,'o','MarkerFaceColor','red');
hold off
% hold on & hold off is used for animat% 2nd order differtial equation is used to solve problem like pendulam &
% spring loaded mass
b=0.05
%damping coefficient
g=9.8
% gravity coefficient
l=1
% length of pendulam
m=0.1
% mass of penduion to start and end between two
% position
axis([-1 1 -1.5 0])
pause(0.1)
M(ct)=getframe(gcf)
ct=ct+1


end

movie(M)
videofile=VideoWriter('pendulam_motion.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.