Project

Second order ODE

Video of moving pendulum

https://youtu.be/kDixJr7sMAU

The code for solving the second order ODE,

clear all
close all
clc

%inputs
b=0.5;
g=9.81;
l=1;
m=1;

%initial conditions
theta_0=[0;3];

%time plots
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);

%initial conditions
x0=0;
y0=0;
res = results(:,1);
ct=1;
x2=l*sin(results);
y2=l*cos(results);

%animating the pendulum motion
for i=1:length(t_span)
    angle = res(i);
    
    x1=l*sin(angle);
    y1=-l*cos(angle);
    
    plot([x0,x1],[y0,y1],'linewidth',3,'color','b');
    %plotting bob
    pos = [x1-0.1 y1-0.1 0.2 0.2]; 
    rectangle('Position',pos,'Curvature',[1 1],'facecolor','r');
    %viscircle([x1 y1], 0.2)
    %plotting support
    rectangle('position',[-0.4 0 0.8 0.2],'facecolor','c');
    axis([-1 1 -1.4 0.2]);
    pause (0.03);
    M(ct) = getframe(gcf);
    ct= ct+1;
end

%Movie
movie(gcf,M);  
videofile = VideoWriter('pendulum_animation.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);

%plotting 
figure(2)
plot (t_span,results(:,1));
hold on;
plot (t_span,results(:,2));
ylabel('Plot');
xlabel('Time (in secs)');


The resulting plot generated,

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.