Solving second order ODEs
A MATLAB code was coded for a 2D O.D.E equation in which a damping equation gives the animation of the oscillation.This coding can also be done for automobile suspensions and to view its damping condition.
is the link for animation
close all
clc
b=0.05;
m=1;
g=9.81;
l=1;
%initial condition
theta_0=[0;5];
%total time
t_span=linspace(0,20,700);
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,m,g,l),t_span, theta_0);
subplot(5,4,[13,14,17,18]);
plot(t,results(:,1),'color','b');
hold on;
plot(t,results(:,2),'color','r');
hold off;
subplot(5,4,[15,16,19,20]);
plot(results(:,1),results(:,2));
xlabel('positive-radian');
ylabel('ang-velocity');
ct=1;
for i=1:length(results(:,1))
x0=0;
y0=0;
x1=l*sin(results(i,l));
y1=-l*cos(results(i,l));
subplot(5,4,[1,2,3,4,5,6,7,8,9,10,11,12]);
plot([-1,1],[0,0],'linewidth',4,'color','k');
hold on;
line([x0 x1],[y0 y1],'linewidth',2,'color','b');
hold on;
plot(x1,y1,'-o','markers',20,'markerfacecolor','r');
axis([-1 1 -2 1])
pause(0.001);
hold off;
end
function [dtheta_dt]=ode_func(t,theta,b,m,g,l)
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
The command is expalined here,
Aim: to acquire a graph for a pendulum using the damping equation between time intervals and to solve the 2D differential equation
close all
clear all
clc
These are the input arguments for the equation
b=0.05;
m=1;
g=9.81;
l=1;
%initial condition
These represent the initial condition f zero displacement and initial velocity 5m/s
theta_0=[0;5];
%total time
The total time interval of the oscillation of pendullum
t_span=linspace(0,20,300);
[t,results]=ode45(@(t,theta)
This directs us to another code where this function is determined for theta 1 & theta 2
ode_func(t,theta,b,m,g,l),t_span, theta_0);
now subplots for the displacement ,velocity & time range are given for 2 separate graphs. They are for theta 1 & theta 2
subplot(5,4,[13,14,17,18]);
plot(t,results(:,1),'color','b');
hold on;
plot(t,results(:,2),'color','r');
hold off;
subplot(5,4,[15,16,19,20]);
plot(results(:,1),results(:,2));
xlabel('positive-radian');
ylabel('ang-velocity');
a for loop is created to represent each graph at different time levels
for i=1:length(results(:,1))
x0=0;
y0=0;
x1=l*sin(results(i,l));
y1=-l*cos(results(i,l));
subplot(5,4,[1,2,3,4,5,6,7,8,9,10,11,12]);
plot([-1,1],[0,0],'linewidth',4,'color','k');
hold on;
line([x0 x1],[y0 y1],'linewidth',2,'color','b');
hold on;
plot(x1,y1,'-o','markers',20,'markerfacecolor','r');
pause(0.001);
hold off;
end