Week 3 - Solving second order ODEs
AIM:
To solve the second order ode using MATLAB program.
OBJECTIVES:
- To write a code to solve the following ode and create an animation of its motion.
- To find the transient behaviour of simple pendulum.
DERIVATION:
In 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
- θ= angle made by the string with the fixed part.
PROGRAM:
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
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m =0.1;
% Initial conditions
theta_0 = [0;3];
% Time points
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);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2),'color','r')
ylabel('Plot')
xlabel('Time')
legend('displacement','vel')
ct = 1
for i= 1:length(results(:,1))
Theta = results(i,1);
%initial points
x0 = 0;
y0 = 0;
%movement of pendulum
x1 = l*sin(Theta);
y1 = -l*cos(Theta);
figure(2)
plot([-1,1],[0,0],'linewidth',3,'color','g');
line([x0 x1],[y0 y1],'linewidth',3,'color','r');
hold on
plot(x1,y1,'o','markers',20,'markerfacecolor','b');
hold on
pause(0.01);
axis([-1.5 1.5 -2 1]);
pause(0.03);
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('simple_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
EXPLANATION:
Ode solver ode45 is used in this matlab program. The results are stored in an array of row atrix of two coloumns, for the values of displacement and velocity at different time periods.
Ode_func is the function made to solve the given equation . where the asumptions are made in the from of code .
Here the graph is plotted to get the wave form of the displacement and the velocity from the initial condtions.
Here we use the concept of the for loop . as the the bob moves with a velocity ,and displaces for the different time period. And recorded it and animated the path of the oscillations
ERROR FACED
OUTPUT: