Challenge

Week 3 - Solving second order ODEs

AIM:

To solve the second-order ode function using matlab which depicts the motion of the pendulum and to create an animation of the same.

OBJECTIVE:

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

In the above equation of second order differential equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

FIXED PARAMETERS:

Length (l)=1 metre,

Mass (m)=1 kg,

Damping coefficient (b)=0.05.

Acceleration due to gravity (g)=9.81 m/s2.

Angular displacement=0, Angular velocity=3 rad/sec at time t=0.

INTRODCTION:

A second order differential equation is defined as a differential equation that includes a function and its second-order derivative and no other higher-order derivative of the function can appear in the equation. It can be of different types depending upon the power of the derivative and the functions involved. These differential equations can be solved using the auxiliary equation.

A pendulum is an object, hung from a fixed point, that swings freely back and forth under the action of gravity. A playground swing is an example of a pendulum. The swing is supported by chains that are attached to fixed points at the top of the swing set. When the swing is raised and released, it will move freely back and forth. These back and forth movements are called oscillations. The swing is moving due to the force of gravity on the swing. The swing continues moving back and forth until friction (between the air and the swing, and between the chains and the attachment points) slows it down and eventually stops it.

(These playground swings are examples of pendulums.)

FUNCTION CODE EXPLAINATION:

1. In the function program, the secon-order ODE is simplified to the first-order ODE by introduction the term theta1 & theta2.

2. Then theta1 & theta2 is differentiated and the dtheta_dt gives a column matrix.

FUNCTION CODE:(ode_func)

% ODE function
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

MAIN PROGRAM EXPLAINATION:

In the above program, the fixed parameters are length, mass, damping coefficient, and acceleration due to gravity.

Time and angle are given as the initial condition.

In the t_span command, 200 values are taken in between 0 to 20 times period.

Using the ode 45 command ODE function is introduced.

Using the plot command velocity and displacement is plotted in the y-axis againts time in the x-axis.

Using the for loop command the mass at various ponts and various times are marked in the graph and are plotted.

Using the movie command the plot is saved as an uncompressed avi file.

MAIN CODE:

clear all
close all
clc
% damping co efficiant
b=0.05;
%gravity
g=9.81;
%length
l=1;
%mass
m=1;
%initial condition
theta_0=[0;3];
%time points
tspan=linspace(0,20,200);
%solve ode
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),tspan,theta_0);
% plotting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('velocity (red)& displacement (blue)')
xlabel('time')
% animation the pendulam oscillation
ct=1;
for i=1:length(results(:,1))
% coordinates
x0=0;
y0=0;
x1=l*sin(results(i,1)+pi);
y1=l*cos(results(i,1)+pi);
% pendulum oscillation
figure(2)
plot([x0 x1],[y0 y1],'-o','MarkerSize',10,'MarkerFaceColor','r','MarkerEdgeColor','r','LineWidth',4);
axis([-2 2 -2 2])
hold on
plot([-1 1],[0 0],'LineWidth',8,'Color','[0 0 0.5]')
title('pendulam oscillation')
pause(0.03)
M(ct)=getframe(gcf);
hold off
ct=ct+1;
end
% movie creating
movie(M)
videofile=VideoWriter('pendulum oscillation','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

ERROR RECTIFCATION:

Error occurs while run the main program that error is "must retur a column vector" checking main program and function code.

Seeing function code error are occur line 8 mistake typos replacing ", to ;" after correction the execute the program succesfully.

dtheta_dt=[dtheta1_dt,dtheta2_dt];==>dtheta_dt=[dtheta1_dt;dtheta2_dt];

After error correction in function

RESULT:

Result on ODE program.

OUTPUT:

Plottings of time and velocity displacement .

Pendulum ocillation:

YOUTUBE LINK:https://youtu.be/BordnSGpsm0

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.