Project

ODE of pendulum

clear all
close all
clc

l = 1;
m = 0.1;
g = 9.81;

%initial conditions
theta_0 = [0;5];

%time points
t_span = linspace(0,10,100);

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



%origin
o = [0 0];
axis(gca,'equal');
axis([-1.2 1.2 -1.2 0.7])
grid on;

ct=1;
for i=l:length(t)
    p = l*[sin(results(i,l)) -cos(results(i,l))];
    o_circ=viscircles(o,0.01);
    pend=plot([o(1) p(1)],[o(2) p(2)], 'linewidth',4);
    ball = viscircles(p,0.05);
    pause(0.03)
    M(ct) = getframe(gcf);
    ct=ct+1;
end
movie(M)
videofile=VideoWriter('pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)


FUNCTION CODE:


function [dtheta_dt] = ode_funct(t , theta,g,l,m)

theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -(g/(l*m))^2*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
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.