Challenge

Week 3 - Solving second order ODEs

YOUTUBE LINK :-

https://youtu.be/iD9owFov5Js

GOOGLE DRIVE LINK :-

https://drive.google.com/drive/folders/1JybKWW_MgbcbZldZRV-TBqvLK7COi6iY?usp=sharing

AIM:-

To solve the second order differential equation using MATLAB which mathematical of the motion of the pendulum and to create an animation for that equation.

OBJECTIVE:-

To write a program that solves the following second order differential equation.This second order differential equation represents the equation of motion of a simple pendulum with damping.

GOVERNING EQUATIONS:-

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

FIXED PARAMETERS:-

Length(L) = 1 meter

Mass(m) = 1kg

Damping coefficient(b) = 0.05

Acceleration due to gravity(g) = 9.81 m/s2

Time period = 0-20 sec

MAIN CODE:-

% input
% b = damping coefficient
 b = .05;
 
 % l = length of the pendulam in m
 l = 1;
 
 % g = gravity in m/s2
 g = 9.81;
 
 % m = mass of the ball in kg
 m = 1;
 
 % intial condition
 theta_0 = [0 5];
 
 % time point
 t_span = linspace(0,20,100);
 
 % solve ode
 [t, result] = ode45(@(t,theta) ODE_func(t,theta,b,g,l,m),t_span,theta_0);
 figure(1);
 plot(t,[result(:,1) result(:,2)]);
 grid on
 ylabel('plot')
 xlabel('plot')
 legend('Displacement (m)','velocity (m/s^2)');
 saveas(figure(1),'SHM.jpg');
 theta = result(:,1);
 i = 1;
 
 for j = 1:length(theta)
     Theta = theta(j);
     
     % intial points 
     x0 = 0;
     y0 = 0;
     
     % movement of the pendulum
     
     x1 = l*sin(Theta);
     y1 = -l*cos(Theta);
     
     figure (2);
     
     % ploting string
     
     plot([x0 x1],[y0 y1],'color','g');
     hold on
     
     % ploting the pendulum bob
     
     plot(x1,y1,'o','markersize',10,'color','r','markerfacecolor','r');
     hold off
     
     axis([-1.2 1.2 -1.2 1.2]);
     pause(0.001);
     M(i) = getframe(gcf);
     i = i+1;
 end
 
 movie(M);
 videofile = VideoWriter('pendulum1.avi','Uncompressed avi');
 open(videofile)
 writeVideo(videofile,M)
 close(videofile);
 




  • In the above program,the fixed parameters are length, mass,damping coefficient and acceleration due to gravity.
  • Tine and angle are given as the initial condition.
  • In the t_span command, 100 values are taken in between 0 to 20 time period.
  • Using the ode 45 command ODE function is introduced.
  • Using the plot commond velocity and displacement are plotted in the y-axis against time on the x-axis.
  • Using the for loop commamd the mass at various points and various times are marked in the graph and are poltted.
  • Using the movie command the plot is saved as an uncompressed .avi file.

FUNCTIONCODE :- ODE_FUNC

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
  • In this function program the second-order ODE is simplified to the first-order ODE by introducing the term theta1 & theta2
  • Then theta1 & theta@ are differentiated and the dtheta_dt gives a column matrix.

ERRORS:-

RESULTS:-

The program executed and animation generated successfully.

ANIMATION:-

https://youtu.be/iD9owFov5Js

PLOT:-

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.