Challenge

Week 3 - Solving second order ODEs

Aim :-

Write a program to solve second order differential equation.

Objective :-

  • Write a program to solve second order differential equation.
  • Make animation of pendulum from the given information.

Governing Equations :-

`(d^2 theta)/dt^2 + b/m * (d theta)/dt + g/l sin theta = 0`

Where,

  1. b = damping coefficient
  2. m= mass of ball
  3. g= gravity
  4. l = length of string

Theory :-

We are solving the second order ode include simple pendulum. We firstly draw a rough diagram of pendulum having angle of theta. So we are solving the equation for the angle theta and also the rate of theta value is changing with respect to the time. The rate of change of theta of pendulum with respect to time is called velocity. This pendulum also describes motion of a spring, pendulum and damper system. And the same ode is going to use the to describe how the mass is going to move. It have lot of application in vehicle dynamics where the suspension system can be developed.

By assuming some parameters given equation of second order ode is break down into the series of first order ode. Writing a program according to given equation to solve ode and make animation of pendulum. We get time and results of velocity and displacement. after that by calling ode function It gives the value of velocity and displacement of pendulum after solving the ode.

Code explanation :-

Code explanation for animation of pendulum :-

  1. clear all, close all, clc command is used to cut the connection of other programs present in same file.
  2. Write input parameters.
  3. Providing initial condition for displacement and velocity.
  4. Getting time points array using linspace command.
  5. Ode45 is used which is inbuilt software in matlab.
  6. Here ode is a function of t and theta.
  7. After that Call theode function in main program.
  8. Write figure(1) command then Write plot command for results and giving labels to x and y axis.
  9. Use forloop for repetation of results for given inputs to make animation of pendulum in figure(2).
  10. For making bob ‘o’ command is used and ‘markersize’ command is used to increase the size of bob
  11. M(ct) = Getframe(gcf) command is used to get frames.
  12. Defining ct = 1
  13. And giving ct = ct+1
  14. After that movie and videowriter command is used to make animation.
  15. Open and closed video file command is used.

youtube link for animation :-

https://youtu.be/W4zxto5alfM

https://www.youtube.com/watch?v=W4zxto5alf

Result :-

Writing program for second order ode from the given formula we find the values for velocity and displacement of the bob of pendulum. From using videowriter command we made animation of the pendulum.

Conclusion:-

We gave find the values of velocity and the displacement of the pendulum from the given differential equation and made the animation of pendulum. We can use these method in vehicle dynamics for the suspension of automobile.

% Program to solve differential equation and to make animation of pendulum
 
 
clear all
close all
clc
 
b = 0.05;
g = 9.81;
l = 1;
m = 0.1;
 
% initial conditions
theta_0 = [0;5];
 
% time points
t_span = linspace(0,10,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))
xlabel('plot')
ylabel('time')
ct=1;
for i=1:length(t)
 
    
    x=l*sin(results(i,1));
    y=-l*cos(results(i,1));
 
    figure(2)
   
   
    plot([0 x],[0 y],'linewidth',3);
     hold on
     plot([-2 2],[0 0],'linewidth',5,'color','r');
    plot(x,y,'o','markersize',20,'markerfacecolor','g');
    axis([-2 2 -1.5 1.5])
    grid on
    pause(0.03)
     M(ct)=getframe(gcf);
  hold off
  ct=ct+1;
 
       
end
movie(M)
 
videofile = VideoWriter('ode_animation.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
    
 




% function command for ode




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


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.