Week 3 - Solving second order ODEs
INTRODUCTION:-
The Order differential equation (ODE) is used to measure the transient behaviour any respective system in this case we are considering motion of a simple pendulum as an example. motion generally works on NEWTONS second law , which usually have second order differential equation which is given below -
from the above equation
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
PROCEDURE TO CODE:-
- we have to give the inputs accordingly to the base , mass , gravity and length.
- code the initial condition when `theta=0`
- initiate the time points from 0 to 10.
- by forming the ODE function we need to solve the ODE with respect to time and results.
- Then we have to plot the results with respect to time.
- Using FOR loop iteration we need create an animation .
- In that FOR loop we have to give the initial and final co-ordinates to plot .
- by giving the dimensions if base , string and bob to create a figure .
- finally we need to create a movie by using movie command which is availabe in MATLAB.
MAIN CODE:-
clear all
close all
clc
%inputs
b=0.05;
g=9.81;
l=1;
m=0.1;
%initial conditions
theta_0=[0;5];
%time points
t_span=linspace(0,10,500);
%sloving ODE
[t, results]=ode45(@(t,theta) ode_fun(t,theta,b,g,l,m), t_span,theta_0);
%plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('plot')
%for loop Iteration method is used to create animation on pendulum
ct=1;
theta_1 = results(:,1);
for i= 1:length(results(:,1))
% initial co-ordinates
x_start= 0;
y_start= 0;
theta_2 = theta_1(i);
% final co-ordinates
x_end = 1*sin(theta_2);
y_end = -1*cos(theta_2);
% ploting for pendulum animation
figure(2)
% base
plot([-1,1],[0,0], 'linewidth',4,'color','black')
hold on
% string
line([x_start x_end],[y_start y_end],'linewidth',6,'color','b')
hold on
% bob
plot(x_end,y_end,'marker','o','markersize', 28,'markerfacecolor','r')
axis([-3 3 -3 3])
pause(0.02)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
% creating movie
movie(M)
videofile = VideoWriter('ode_problem.avi','uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
FUNCTION CALLING CODE:-
function [dtheta_dt]= ode_fun(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
OUTPUT:-
In this problem we are having two outputs.
1. The output obtained is velocity and displacement curves with respect to time .
2.Animated video of damped motion on simple pendulum with respect to time.
ERRORS FACED WHILE CODING:-
we need use the correct and case sensitive spellings while coding
so we need rectify once before executing the code.