Challenge

Week 3 - Solving second order ODEs

clear all
close all
clc

b = 0.15;
g = 9.81;
l = 0.5;
m = 0.15;
ct=1;
L=1;
% initial condition
theta_0 = [0;4]

% time points
t_span = linspace(0,8,400)

% solve ode
[t, results] =ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0)

plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

% animation

X0=0;
Y0=0;
    
for i=1:length(t)
     X1= L*sin(x(:,1));
     Y1 =-L*cos(x(:,1));
    figure(2)
    plot([-1 1],[0 0],'linewidth',3,'Color',r);
    axis([-2 2 -2 2])
    line([X0,X1],[Y0 Y1],'linewidth',2,'Color',b)

    plot(X0,Y0,'O',markersize,3,'markerfacecolor','g')
    plot(X0,Y1,'O',markersize,20,'markerfacecolor','y')
    title('DAMPING OF SIMPLE PENDULUM');
    legend('Support rod''length of pendulum''fixed point''bob of pendulum')
    grid on
    hold off
    M(ct)=getframe(gcf);
    ct=ct+1;
end
 
movie(M)
videofile=videowriter('simple_pendulum','Uncompressed AVI');
openVideo(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.