Project

Solving Second Order ODE - pendulum_undamped

%solving ODE for the undamped pendulum motion
clear all
close all
clc

g=9.81;
l=0.5;
m=0.5;

theta_0 = [0;5];

t_span = linspace(0,8,500);

[t,results] = ode45(@(t,theta) ode_func(t, theta, g,l,m),t_span,theta_0);

figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('plot')
ylabel('time')
legend({'Displacement','Velocity'});
%%Animating the simple pendulum

figure (2)

angle = results(:,1);

Xa1=1.5;
Ya1=2;

Xa2=-1.5;
Ya2=2;

%Creating loop

ct=1;

for i=1:length(t)
    
    x0 = 0;
    y0 = 2;
    
    x1 = l*sin(angle(i));
    y1= -l*cos(angle(i));
    
    r = 0.15;
    xc = x1;
    yc = y1;

    theta_c = linspace(0,2*pi);
    x_c = r*cos(theta_c) + xc;
    y_c = r*sin(theta_c) + yc;
    
     plot([x0 x1],[y0 y1],[Xa1 Xa2],[Ya1 Ya2],x_c,y_c,'linewidth',2)
   
    
   
    % plot([Xa1 Xa2],[Ya1 Ya2])
    
    axis([-2 2 -2 3])
    pause(0.03)
    M(ct)= getframe(gcf);
    ct=ct+1;
    
end

Movie(M)
videofile= VideoWriter('pendulum_undamping.avi','uncompressed AVI ');
open(videofile)
writeVideo(videofile,M)
close(videofile)








%ODE Solver Code
function [dtheta_dt] = ode_func(t, theta ,g,l,m)

  theta1 = theta(1);
  theta2 = theta(2);
  dtheta1_dt = theta2;
  dtheta2_dt = -((g/(l*m))^2) *sind(theta1);
  dtheta_dt = [dtheta1_dt; dtheta2_dt];
  
end

https://youtu.be/3vUk8PWzy08

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.