Project

ODE plot and animation for simple pendulum

https://youtu.be/V4Xi9t39hao

close all
clear all
clc

b = 0.5;     %damping coefficient
g = 9.81;
l = 1;        %length
m = 1;

%initial conditions

theta_A = [0;3];   % initial displacement is zero and initial velocity is 5 m/sec.

%time points

t_span = linspace(0,20,500);  %it goes from 0 to 10 sec with 500 values in between

% solve ODE, in below equation ode45 is inbuilt matlab command ahich means
% this particular ODE function of t and theta and then we are defining the variables for that. 

[t, results] = ode45(@(t, theta) ode_fun(t, theta, b, g, l, m),t_span, theta_A);




figure(1);
te = 1.5;   % to  be used as temporary variable
O=[0 0.8];
axis(gca,\'equal\'); % used for returning current axis and represents aspect ratio of the plot
axis([-te te -te te]);    % limits of the plot
grid on;

ct = 1;
for i=1:length(t)
    P=l*[ sin(results(i,1)) -cos(results(i,1))];  % represents location of masspoint at every interval of time 
    %i.e assigning value at every interval
    O_circle = viscircles(O,0.01);
    pend =line ([O(1) P(1)],[O(2) P(2)]); 
    ball =viscircles(P,0.05); % viscircles draw circles of accurate radii and with specified centres
  
    pause(0.001);
    M(ct) = getframe(gcf);
    ct = ct+1;
    
   
    if i<length(t)
        delete(pend);
        delete(O_circle);
        delete(ball);
    end
        
    
end

movie(M)
videofile = VideoWriter(\'ode_plot.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)

figure(2)
plot(t,results(:,1))
grid on;

hold on
plot(t,results(:,2))
grid on;
ylabel(\'plot\')
xlabel(\'time\')


        

\"\"

\"\"

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.