Challenge

Week 3 - Solving second order ODEs

Solving second ODE for Simple Pendulum

Aim:

To solve the second ODE for a simple pendulum and to create the simple pendulum motion by using Matlab.

Code:

%Solving the ode equation
clear all
close all
clc

%inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;

%intial parameters

theta_0 = [0;3];

%time points

t_span = linspace(1,10,500);

%function calling and ode solving

[t,results] = ode45(@(t,theta) ode_sub(t,theta,b,g,l,m),t_span,theta_0);

%ploting
figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
xlabel('time')
ylabel('velocity displacement')
legends('displacement','velocity')
%for loop
for i = 1 : length(results)
    
    result = results(i,1);
    
    %coordinates
    
    x1 = 0;
    y1 = 0;
    
    x2 = -(l*sin(result));
    y2 = -(l*cos(result));
    
    %ploting
    figure(2)
    plot ([x1 x2] , [y1 y2], 'linewidth',3)
    hold on
    plot(x2,y2,'o','Markersize',20,'MarkerFacecolor','r')
    hold off
    axis([-1.25 1.25 -1.25 0.2])
    
    M(i) = getframe(gcf);
end
    
%movie
movie (M)
videofile = VideoWriter('ode_main.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Explanation:

The simple pendulum is used as an example to solve the Second order differential equation.

Resolving the Second ODE to First ODE

  • The given second order differential equation is, `d^2theta//dt^2 + b/m d theta//dt +g/l sintheta =0`.
    function [dtheta_dt] = ode_sub(t,theta,b,g,l,m)
    
    %intial assumption
    theta1 = theta(1);
    theta2 = theta(2);
    
    %calculation
    
    dtheta1_dt = theta2;
    dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(theta1);
    [dtheta_dt] = [dtheta1_dt ; dtheta2_dt];
    
    end
  • In the above mentioned code, the second ODE is simplified to First ODE.
  • The theta in the above equation is assigned to theta1(`theta = theta_1`).
  • The first ODE in the above equation is assigned to Theta2(`d theta_1//dt = theta_2`).
  • The second ODE in the given equation is resolved to First ODE as follow,

`d^2theta_1//dt^2 = d/dt (d theta_1//dt) = d theta_2//dt`

  • Therefore the above equation is now resolved to First ODE equation,

`d theta_2//dt + b/m theta_2 + g/l sintheta_1 = 0`.

`d theta_2//dt = -(b/m theta_2) - (g/l sintheta_1)`.

  • The given Second order differential equation is resolved using the above equation.

Solving the Second ODE for simple pendulum:

  • The input parameters that are given are taken,

    l=1 m,

    m=1 kg,

    b=0.05,

    g=9.81 m/s2.

  • The function 'ODE45' is used to solve the equation.
  • The syntax for ODE45, [t ,y] = ode45(odefun, t_span,y0).
  • The t,y in the above syntax are the out parameters.
  • The odefun is the function created to resolve the Second ODE to First ODE.
  • The T_span is the number of times that the ODE45 function should repeat.
  • The y0 is the initial condition that is given to the ODE45.
  • The Initial condition that given is taken(d = 0, v= 3rad/s).
  • The t_span is taken as required.
  • The Second ODE is solved using the ode45 function with given inputs.
  • The output that has arrived is plotted.
  • The x-axis and y-axis are named time and velocity-displacement respectively.
  • The legend is used to show the values of curves.

For loop:

  • The for loop is used here to change the angular value to cartesian values.
  • The for loop Syntax, for index = starting value : ending value

statement 1;

end

  • The starting value for the for loop is taken as 1 and the ending value is taken as a total number of values in the results using the length command(length(results)).
  • The index is taken as 'i'.
  • The new variable is assigned to have the for loop values, result = results(i,1)
  • The Cartesian values have derived.
  • The x1 and y1 are assigned as origin points(0,0).
  • The x2 and y2 have derived from the length of its string to its angle(x2 = -(l*sin(result)),y2 = -(l*cos(resut))).
  • The plotting had been done with arrived values.
  • The maker command is used to create the blob in the pendulum.
  • Then the makersize and makercolor commands are used to increase the size and color of the blob.
  • The axis has assigned to align the position of the plot.

Animation:

  • The getframe command is used to get a frame for creating the movie and stored in the variable 'M'.
  • The movie is created using the movie command (movie(M)).
  • The videowriter is used to write a new file and to save it in the predefined location it is assigned to the variable 'videofile'.
  • The Writevideo command is to Write the movie that has created in the created new file.
  • Thus Animation has been created.

Animation of simple pendulum

Conclusion:

  • The second order differential equation is solved using Matlab.
  • The animation created for the Simple pendulum using the Second Order Differential Equation.
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.