Project

SOLVING SECOND ORDER ODE FOR A SIMPLE PENDULUM AND ALSO ITS MOTION

AIM

To write a matlab program and to stimulate the pendulum motion for the second order ODE.

%inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;
 
%initial conditions
theta_0 = [0,3];
 
%time points
time_span = linspace(0,20,500);
 
%solve ODE
[t , results] = ode45(@(t,theta) ode_func(t , theta,b,g,l,m),time_span,theta_0);
 
plot(t,[results(:,1) results(:,2)])
xlabel(\'time\')
ylabel(\'plot\')
 
%pendulum_animation
theta = results(:,1);
c = 1;
 
for i =1:length(theta)
    Theta = theta(i);
    
    
    %initial points
    
     x0 = 0;
     y0 = 0;
    
    %pendulum motion
    x1 = 1*sin(Theta);
    y1 = -1*cos(Theta);
    
    figure(2)
    
    
    plot([-1 1],[0 0],\'linewidth\',5,\'color\',\'k\')
   hold on
    plot([x0 x1],[y0 y1],\'color\',\'b\')
    hold on
    
    plot(x1,y1,\'markersize\',30,\'markerfacecolor\',\'r\',\'marker\',\'O\')
     
    %plot(x1,y1,\'markersize\',30,\'markerfacecolor\',\'r\',\'marker,\'O\')
    hold off
   
    
      
    axis([-2 2 -2 2]);
    pause(0.005);
     M(c) = getframe(gcf);
    c =c+1;
end
 
%movie creating
movie(M);
Videofile = VideoWriter(\'oscillation.avi\',\'uncompressed AVI\');
open(Videofile)
WriteVideo(Videofile,M)
close(Videofile)



function[dtheta_dt] = ode_func(t,theta,b,g,l,m)
theta1 = theta(1)
theta2 = theta(2)
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
[dtheta_dt] = [dtheta1_dt;dtheta2_dt];
end





STEPWISE EXPLANATION OF CODE

  1. Provide the necessary inputs for the pendulum.
  2. Then,apply initial conditions for angular displacement and the angular velocity.
  3. Make time span for upto 20 cycles.
  4. Now solve ODE,apply the matlab command ode45 and apply all the limits.
  5. Then,plot the results and make x-axis as time and y-axis as plot.
  6. Now create a for loop command for the pendulum motion by giving necessary theta values.
  7. Then,plot the string and pendulum bob values with markersize and marker facecolor.
  8. Give the axis value and pause at certain intervals for a smooth motion of the animation.
  9. Use getframe command and then create a movie file for the animation.
  10. Also create the alternate function value for the pendulum and apply this function in the program.

ERROR FACED AND STEPS TO OVERCOME THE ERROR

The error i got while programming is that the variable \'ct\' is not defined inside the program for creating the movie so the command which is not called caused an error.

The step i have taken to overcome the error is that providing the necessary \'c\' value which i have called inside the program instead of the variable \'ct\'.

OUTPUT FOR THE PLOT

IMAGE SHOWING PLOT FOR TIME VS PLOTTED VALUE

YOUTUBE LINK FOR THE ANIMATION

https://youtu.be/oSbj8bBrJHU

RESULT

Thus the given program is used to stimulate the pendulum motion using a simple matlab program.

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.