Project

Pendulum Display in MATLAB

clear all
close all
clc
g=9.81; %acceleration due to gravity
l=1; %length in m
m= 0.2; %mass in kg
r=1;
w=g/(l*m);
%initial condition
theta_0=[ 0; 50];

%time variation
t_var=linspace(0,5,100);

%integration
[t , results]=ode45(@(t,theta) ode_func(t,theta,w), t_var , theta_0);

%plotting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel(\'Time\')
ylabel(\'Plot\')

%animation
ct=1;
for i=1:length(t_var)
    thet=results(i,1);
    x=-l*sind(thet);
    y=-l*cosd(thet);
    
figure(2)
    plot([0 x],[0 y],\'linewidth\',3)
    axis([ -1.5 1.5 -1.5 1.5]);
    
    p=[x y];
    circ=viscircles(p,0.1)

    %circle1(x,y,2)
    pause(0.01)
    M(ct)=getframe(gcf);
       ct=ct+1;
end
movie(M)
videofile=VideoWriter(\'Pendulum_matlab.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)


Function ode_func

function [dtheta_dt]=ode_func(t,theta,w)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt= -(w^2)*sind(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt]
end

Screenshots

https://youtu.be/k1HgsUQ1mfU

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.