Challenge

Week 3.2 - Simulating a 2nd order ODE system of a simple pendulum

Week 3.2 - Simulating a 2nd order ODE system of a simple pendulum

Aim : Illustrating the motion of a pendulum, by a predefined 2nd order ODE.

The program starts by giving inputs (damping constant, length, etc.), thereafter the initial condition is defined as 0 displacement and initial 3 rad/s angular velocity. In order to simulate the movement of the pendulum, linspace command defines 0-20 seconds with intervals.

In order to solve the differential equation, or for calling the function, a prebuilt 'ode45' command is used, which is primarily dependent on main inputs(time and angle), and also it incorporates 'ode_func' as a funtion of several variables. Here, the 1st and 2nd ODEs are defined as arrays, so when they give out result'([t, results])', 2 coloumns are obtained ((:,1)) for displacement positions and (:,2) for corresponding velocities). Secondly to plot the time vs velocity graph for the pendulum, plot command is used which assigns the value of previous two coloumns obtained in a graph.

For visualing the motion of the motion, the (result (:,1) coloumn) 'displacements' are defined to a new variable 'thetaP'. After inserting the counter, a 'for loop' is introduced, starting from i=1 to all values of theta_P, and its fixed to a 'THETA_P'(which holds all recorded values of thetaP). In the next section, the initial points of a string are defined as (x0,y0 as fixed) and endpoints (x1,y1, both as a funtion of THETA_P, as it varies). The animation plot is done using 'plot command'(including x and y coordinates as arrays with given linestyle). For animating the string, 'hold on' command is not used as its value is instantaneous for all 20 seconds and need not to be captured for each second, however to represent a ball hanging to the end of string, 'marker' command is used showing a circle, with 'hold on', as it is attached to the string for given duration. Then the 'for loop' is ended with prior commands('axis' command for limiting both X and Y axes so as to eliminate their fluctuations, defining a pause interval, command for storing the frames and closing the counter with a plus one increment).

At the end, the file name is given by 'videofile', qnd follows up by commands for opening, writing and closing the video file.

Drive link for animation:

https://drive.google.com/file/d/1uFE_-XzOupB57aEiOxJjT17wAoD7frD6/view?usp=sharing

%Inputs
b=0.05;
g=9.81;
l=1;
m=1;

%initial condition
theta_0=[0;3];

%Timepoints
t_span = linspace(0,20,500);

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

%plot
figure(1)
hold on
plot(t, results (:,1))
plot(t, results (:,2))
xlabel('time')
ylabel('velocity')

%pendulum motion
thetaP = results(:,1);

ct=1;
for i=1:length(thetaP)
    THETA_P=thetaP(i);
    
    x0=0;
    y0=0;
  
    x1=l*sin(THETA_P);
    y1=-l*cos(THETA_P);

    %animation plot
    figure(2) 
    plot([x0 x1],[y0 y1],'linewidth',4,'color','r');
    hold on
    plot(x1,y1, 'o','markers', 18,'markerfacecolor','g');
    hold off
    axis([-1.5 1.5 -2 2]);
    pause(0.05)
    M(ct) = getframe(gcf);
    ct = ct+1;
end
movie(M)
videofile = VideoWriter('Pendulum_motion.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
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.