Challenge

Week 4.1 - Solving second order ODEs

clear 
close
clc
%Write a program in Matlab that will simulate the pendulum motion.
%Input variables
L = 1;        %Length of the pendulum in m.
m = 1;        %Mass of the ball in kg.
b = 0.05;     %Damping coefficient.
g = 9.81;     %Gravity in m/s2.
%Initial Condition of angular displacement and angular velocity at time t=0.
theta0 = [0;3];
%Time Input
tspan = linspace(0,20,1000);
%Function Call
[t,y] = ode45(@(t,theta) ode_function(t,theta,b,g,L,m),tspan,theta0);
figure(1)
plot(t,y(:,1))
hold on
plot(t,y(:,2))
hold off
xlabel('Time in seconds');
ylabel('angular displacement/angular velocity' )
%Initial value of counter variable
ct = 1; 
%Using for loop for the simulation of the pendulum.
for i=1:length(t);
  
    x1 = L*sin(y(i,1));
    y1 = -L*cos(y(i,1));
    %Reference Line
    figure(2)
    plot([-0.5,0.5],[0,0],'LineWidth',3,'color','b');
    hold on
    %Pendulum Ball or Bob
    plot(x1,y1,'Marker','O','MarkerEdgeColor','g','MarkerSize',10,'LineWidth',8)
    %Rope
    plot([0 x1],[0,y1],'linewidth',1.5)
    %Joint between Rope and Reference Line
    plot(0,0,'-o','MarkerFaceColor','m','MarkerSize',5)
    xlabel('X axis')
    ylabel('Y axis')
    legend('Reference Plane','Ball','Rope','Joint')
    title('Simulation of Pendulum')
    axis([-1.5 1.5 -1.5 1.5]);
    pause(0.01)
    M(ct) = getframe(gcf)
    ct = ct+1;
    hold off
end
movie(M);
videofile = VideoWriter('Simulation_of_Pendulum.avi', 'Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);

FUNCTION OF THE CODE

function [dtheta_dt] = ode_function(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

The error occured in the function as well as in code. I first tried to find the error by checking the command window but only few of them were resolved but rest of them were resolved by appling the step by step method.

PSEUDOCODE FOR FUNCTION

BEGIN
     FUNCTION create a function to reduce the complication and length of the code
              Give the name of the function and save it by using the same name
              First discretisation of the ODE
              theta1 = theta(1)
              theta2 = theta(2)
               dtheta1_dt = theta2;
               dtheta2_dt = -(b/m)*theta2-(g/L)*sin(theta1)
               dtheta_dt = [dtheta1_dt; dtheta2_dt]
     ENDFUNCTION    
 END
Step-wise explanation of code

BEGIN
     INITIALIZE input the initial known variables.
                input initial Condition of angular displacement and angular velocity for the given time. 
                Create an array which represents the time period.
                Call the function ode_function
           PLOT plot the grapg between time and angular displacement/angular velocity
                Initialize the value of counter variable            
           FOR  Using for loop for the simulation of the pendulum
                enter x and y coordinates for moving end of the rope
            PLOT plot graph for reference line,Pendulum Ball or Bob,Rope and Joint between Rope and Reference Line.
            Use getframe () command to capture the frame. 
           ENDFOR
            Use movie() command to create the video,
             Make the file by using Videowriter command,
             open the file by using open() command,
             use writevideo() command to write the video file,
             use close() command to close and save the video file.
END

https://youtu.be/DBkKqwOZ_kc

link of the working video of pendulum

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.