Week 4.1 - Solving second order ODEs
%Inputs
l=1;
m=1;
b=0.05;
g=9.8;
%initial condition
theta_0 = [0;3];
%time points
t_span = linspace(0,20,200);
%slove 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('plot')
xlabel('time')
x0 = 0;
y0 = 0;
disp1 = results(:,1);
ct = 1;
figure(2)
for i = 1:length(disp1)
x1 = l*sin(disp1(i,1));
y1 = -l*cos(disp1(i,1));
plot([x0 x1],[y0 y1])
hold on
plot(x1,y1,'.','Markersize',50)
plot([-1 1],[0 0])
axis([-2 2 -2 2])
hold off
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter("secondorder_ode.avi");
open(videofile)
writeVideo(videofile,M)
close(videofile)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]; EXPLANATION:
step 1
In step one to enter the second order equaction values
there are
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
STEP 2
In step 2 to give the initia condition of the ode function (i.e)
%initial condition
theta_0 = [0;3];
and also to given the time
%time points
t_span = linspace(0,20,200);
STEP 3
In step 3 we can slove the second order ode (i.e)
%slove ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
ode_func(t, theta, b,g,l,m) in this command to take from function loop like
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];
STEP 4
i can draw thplot between plot and time
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
after this commands we can given the animated plot of simple pendulum and also to create the movie file
movie(M)
videofile = VideoWriter("secondorder_ode.avi");
open(videofile)
writeVideo(videofile,M)
close(videofile)
https://drive.google.com/file/d/1NkubkGB55GxR-vFVAu7bsojr8F8TZA9U/view?usp=sharing
ERRORS:
I had struggled with one error (i.e) we can not given the x and y values