Project

Second order ODE

As per the given inputs, following is the code for second order ODE which will be clearly explained with the example of simple pendulum.

clear all
close all
clc

% Inputs
 b = 0.5;  % b is damping coefficient
 g = 9.81; %g is gravity in m/s^2
 m = 1;    % m is mass of the ball in kg
 l = 1;    %l is length of the pendulum in m
 
 % initial condition
 initialtita = [0;3];
 
 % time span
 timeinterval = linspace(0,20,200);
 
 % solution of ODE
 [t,results] = ode45(@(t,tita) odefunction(t,tita,b,g,l,m),timeinterval,initialtita);
 
 
 hold on
 grid on
 xlabel(\'time\')
 ylabel(\'plot\')
 plot(t,results(:,1), \'linewidth\',1.0,\'color\',\'r\')
 plot(t,results(:,2),\'linewidth\',1.0,\'color\',\'b\')
 legend(\'displacement\',\'velocity\')
 
 % movement of pendulum

o = [0 1];
 
ct = 1;
 for i=1:length(results(:,1))
     
     tita = results(i,l);
     x2=l*sin(tita);
     y2=-l*cos(tita); 
     figure(2)
     
     plot([o(1) x2], [o(2) y2],\'-\',\'linewidth\',1.5)
     hold on
     rectangle (\'position\',[-0.5 1 1 0.25],\'FaceColor\',\'g\');
     plot (x2,y2,\'r.\',\'MarkerSize\',30) 
     hold off    
     axis equal
     axis ([-1.5 1.5 -1.5 1.5])     
     M(ct) = getframe(gcf);
     pause (0.5);
     ct = ct+1;           
 end

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

the graph for velocity and displacement is given below

\"\"

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.