Challenge

Week 3 - Solving second order ODEs

Objective:

- To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.

The code is provided below:

clear all
close all
clc

%Inputs:
%Damping coefficient
b= 0.05;
% length of string
l= 1;
%mass of bob
m= 1;
% acc due to gravity
g= 9.81;


% Initial condition at t=0:
%angular velocity= 3;
%angular displacement = 0;
theta0= [0;3];


%time:
tspan= linspace(0,20,200);

%Solving ODE using ode45:

[t,r]= ode45 (@(t,theta) ode_function_pendulum(t,theta,b,g,l,m),tspan,theta0);

%plotting
%angular displacement:

plot(t,r(:,1))
hold on

%angular velocity

plot(t,r(:,2))
xlabel('time')
ylabel('plot')
legend({'displacement', ' velocity'})

% Animation

x0=0;
y0=0;
plot(x0,y0,'Marker','o')

%To extract the first column of a result and assign it to'y'
y= r(:,1);

% to create images for each value of time and store it in an array 'm'
ct= 1;

for i= 1:length(tspan)
    x1= l*sin(y(i));
    y1= -l*cos(y(i));

    plot ([x0 x1],[y0 y1], 'LineWidth',3,'Marker','o')
    hold on
    plot (x1,y1,'marker','o','Color','r','MarkerSize',35, 'MarkerFaceColor','b')
    hold off
    title('motion of simple pendulum with damping','Color','r')
    axis([-1 1 -2 0])
    pause(0.04)
    xlabel('Time')
    ylabel('Angular Displacement and Angular Velocity')

    % convert figures into frames and store them an array 'M'

    M(ct)= getframe(gcf);
    ct= ct+1;
end

% to create a movie:

movie(M);
videofile = VideoWriter('2nd_oreder_ode','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);

*function defined by the user:

function [dtheta_dt] = ode_function_pendulum(t,theta,b,g,l,m)

theta1= theta(1); %dispacement rep as an array
theta2= theta(2); %velocity rep as an array
dtheta1_dt= theta2;
dtheta2_dt= (-b/m)* theta2- (g/l)*sin(theta1);
dtheta_dt= [dtheta1_dt;dtheta2_dt];

end

In order to create a program that solves ODE, first I have defined the inputs which are: Damping coefficient, gravity acceleration, the mass of bob and the length of the string.

In the next step, I have defined the initial condition of the pendulum which was given by taking the angular displacement (theta0)= 0 and the angualr velocity = 3 at time = 0.

for the time intervals, I have generated an array from 0 to 20 sec with generating 200 values by using linspace function. After that, I have created a function that solves the second order equation using ode45 which is inbuilt the ode solver in matlab.

After the variables are defined , I have insert a plot command called (figure 1) to show how the pendulum getting damped.

the next plot which is figure 2 shows the animation of the pendulum using for loop to create an image for each time value and store it in an array

Finally, I have created a movie to profile and format my animation.

Note: Youtube link is provided below which shows the motion of simple pendulum with damping. In additionm plot is provided in the attached file

https://youtu.be/OexTC2twyiY

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.