Week 3 - Solving second order ODEs
PROJECT:- SOLVE ODE EQUQTION
AIM:- Write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.
SOFTWARE USED:- MATLAB R2020a
THEORY:- An ordinary differential equation (ODE) is a differential equation containing one or more functions of one independent variable and the derivatives of those functions.The term ordinary is used in contrast with the term partial differential equation which may be with respect to more than one independent variable.
In the damped simple harmonic motion, the energy of the oscillator dissipates continuously. But for a small damping, the oscillations remain approximately periodic.
The equation of damped system is :
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Inputs for simple pendulum:
L=1 meter, m=1 kg, b=0.05. g=9.81 m/s2.
Timespan 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
OBJECTIVE:- To create animation of simple pendulum with damper.
ODE45 in MATLAB:- ODE45 is an inbuilt function in MATLAB to solve the ordinary differential equations. This function implements a Runge-Kutta method with a variable time step for. efficient computation.
CODES WRITTEN:-
%solving 2nd order ode's using matlab
clear all % all data cleared
close all % all running work closes
clc % command window cleared
%inputs for solving ode
b=0.05; %damping coeff.
g=9.81; % gravity (m/s^2)
l=1; %length of pendulum (m)
m=1; %mass of ball (Kg)
% initial conditions for pendulum motion
theta_0 = [0;3];
% time for pendulum
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(t,results (:,1))
hold on
plot(t,results(:,2))
ylabel('angular velocity and amplitude')
xlabel('time')
fig=1;
%using for loop for simulation of pendulum
for i = 1:length(t)
%initial position of the pendulum
x0 = 0;
y0 = 0;
%final position of pendulum
x1 = l*sin(results(i,1));
y1 = -1*cos(results(i,1));
%plotting
figure(2)
%base line
plot([-1,1],[0,0],'linewidth',4,'color','g');
% pendulum line
line([x0 x1],[y0 y1],'linewidth',2,'color','r');
hold on;
%pendulum ball
plot(x1,y1,'o','markers',20,'markerfacecolor','k');
grid on
axis([-2 2 -2 1]);
pause(0.03);
hold off;
M(fig) = getframe(gcf);
fig= fig+1;
end
% for genarating and storing the video file in avi format
videofile = VideoWriter('simple_pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
calling function:-
%function for solving the ode
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
CODE EXPLANATION:-
- Write basic command for previously worked cleared.
- Write calling function and function and file name should be same.
- Input variables, which are in equations , m, b,l,g . and give initial condition for pendulum timespan.
- ODE45 command for solving 2nd order ode.
- Use plot command for making plot.
- Use for loop for simulation of pendulum and initial and final position of pendulum.
- Use marker to make bob of pendulum.
- Give axes for plot and pause for simulation of pendulum.
- Generate video/animation using videoWriter command.
OUTPUT:-
ERROR:-
Step to overcome error :
Putting hold off command the error was fixed.
PROJECT ANIMATION LINK:-
https://drive.google.com/drive/folders/1OYbH4YDYOFpOOBv-0M3dNSW2scOnI2ih
CONCLUSION:-
- Learned to solve ode using MATLAB.
- Learned use of ODE45.
- Learn use of marker command