Challenge

Week 3 - Solving second order ODEs

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
clear all
close all 
clc
b=0.05;
g=9.81;
l=1;
m=1;
%Initial condition
theta_0=[0;3];
%time points
t_span =linspace(0,20,500);
%solve ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting

plot (t,results (:,1))
hold on
plot (t,results (:,2))
ylabel('plot')
xlabel('time')
hold off;
%Animation
X0=0;
Y0=0;
ct=1;
for i=1:length(t);
    X1=l*sin(results(i,1));
    Y1=-l*cos(results(i,1));
    figure(2)
    plot([-1,1],[0,0],'linewidth',6,'Color','g');
    axis([-2 2 -2 2]);
    line([X0,X1],[Y0,Y1],'linewidth',3,'Color','r');
    hold on;
    plot(X0,Y0,'O','markersize',8,'markerfacecolor','b');
    plot(X1,Y1,'O','markersize',20,'markerfacecolor','y');
    title('Damping of simple Pendulum');
    legend('Support rod','length of the pendulum','fixed point','Bob of the pendulum');
    hold off;
    pause(0.1)
    M(ct)=getframe(gcf);
    
    ct=ct+1;
end
movie(M);
videofile=VideoWriter('simple_pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
clear all
close all 
clc
b=0.05;
g=9.81;
l=1;
m=1;
%Initial condition
theta_0=[0;3];
%time points
t_span =linspace(0,20,500);
%solve ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting

plot (t,results (:,1))
hold on
plot (t,results (:,2))
ylabel('plot')
xlabel('time')
hold off;
%Animation
X0=0;
Y0=0;
ct=1;
for i=1:length(t);
    X1=l*sin(results(i,1));
    Y1=-l*cos(results(i,1));
    figure(2)
    plot([-1,1],[0,0],'linewidth',6,'Color','g');
    axis([-2 2 -2 2]);
    line([X0,X1],[Y0,Y1],'linewidth',3,'Color','r');
    hold on;
    plot(X0,Y0,'O','markersize',8,'markerfacecolor','b');
    plot(X1,Y1,'O','markersize',20,'markerfacecolor','y');
    title('Damping of simple Pendulum');
    legend('Support rod','length of the pendulum','fixed point','Bob of the pendulum');
    hold off;
    pause(0.1)
    M(ct)=getframe(gcf);
    
    ct=ct+1;
end
movie(M);
videofile=VideoWriter('simple_pendulum.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.