second oder ordinary diiferential equation
INTRODUCTION:
Among ordinary differential equations, linear differential equations play a prominent role for several reasons. Most elementry and special functions that are encountered in physics and applied mathematics are solutions of linear differential equations ( harmonic motion). When physical phenomena are modeled with non-linear equations, they are generally approximated by linear differential equations for an easier solution.
ODE for a simple pendulam:
m = mass at the swinging end of the pendulum (kg)
g = acceleration due to gravity (m/s2)
L = length from the swivel point to the center of mass (meters)
b=damping coefficient
θ= angle between the string position to the string position at rest
t = time (seconds)
CODE WITH STEPWISE EXPLANATION
1. Creating function:
function [dtheta_dt]=ode_function(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta_2
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1)
dtheta_dt=[dtheta1_dt;dtheta2_dt]
end
2.second order ODE for pendulam motion.
%Second order ODE for pendulam motion
%step1: close all previous work
clear all
close all
clc
% step2 :inputs
%Damping Coefficient
b = 0.05;
%Gravity m/s^2
g = 9.81;
%Mass of the Pendulum in kg
m = 1;
%Length of the string in meters
l = 1;
%step3 :initial conditions
theta_0 = [0;3]; %[angular displacement=0;angular velocity=3]
%step4 :time points
t_span = linspace(0,20,1000); %pendulum oscillates between 0-20 seconds
%step4: solving second order ODE by calling the function
[t,result] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);
%step5 :plotting the graph
figure(1)
%subplot to represent two curves in a single figure window
subplot(2,1,1)
p1 = plot (t,result(:,1),\'linewidth\',2,\'color\',\'b\')
xlabel(\'Time\')
ylabel(\'Theta\')
title(\'Angular Displacement v/s Time\')
legend([p1(1)],\'angular displacement\')
hold on
subplot(2,1,2)
p2 = plot(t,result(:,2),\'linewidth\',2,\'color\',\'r\')
xlabel(\'Time\')
ylabel(\'Velocity\')
title(\'Angular Velocity v/s Time\')
legend([p2(1)],\'angular velocity\')
%step6 :pendulum simulation using second order ode
theta = result(:,1) %it means theta is equal to values in cloumn 1 of result matrix
%counter to counts events in a loop
ct=1;
%step7 : for loop for oscillating the pendulum
for i=1:length(result)
THETA = result(i,1);
%step8 :initial boundary conditon
x0=0;
y0=0;
%step9: Defining x and y coordinates at different coordinates
x1= l*sin(THETA);
y1= -l*cos(THETA);
%step10: to represent motion of a simple pendulam
figure(2)
hold on
plot([x0 x1],[y0 y1],\'linewidth\',4);
plot(x1,y1,\'-o\',\'markersize\',40,\'markeredgecolor\',\'g\',\'markerfacecolor\',[0.8 0.8
0.8]);
plot(x0,y0,\'-*\',\'markersize\',5,\'markeredgecolor\',\'k\',\'markerfacecolor\',[0.8 0.8
0.8]);
axis([-1.5 2 -1.5 0]);
pause(0.003);
%defining frame
M(ct)=getframe(gcf)
%incrementing counter
ct=ct+1;
% clearing the current graphic image
clf()
%step12: creating the animation
movie(M);
videofile = VideoWriter(\'second_order_ode_pendulum.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
ERRORS FACED:
while coding in loop i put : intead of loop intiger i so i get error as shown in snapshot:
so to overcome it I put i instead of : and code works effectively.
OUTPUT CURVES:
ANIMATION LINK: