Challenge

Solving second order ODEs using matlab

Program to simulate the pendulum motion:

%program to stimulate the simple pendulum
clear all
close all
clc
%inputs
%Damping
b = 0.05;

%Length of the sting (m)
l = 1;
%Gravitational constant (m/s^2)
g = 9.81;
%Mass of the ball (kg)
m = 1;

%initial condition
theta_0 = [0;3];
 %Time points
 t_span = linspace(0,20,500);
 
 %Solving ODE
 [t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
 
 
 ct=1;
for i=1:length(results(:,1))
   
   x0=0;
   y0=0;
   x1=l*sin(results(i,1));
   y1=-l*cos(results(i,1));
   % line where the fixed end of the string is placed
   plot([-1,1],[0 0],'linewidth',4,'color','k');
   hold on
   % creating the string, determining its colour and width
   line([x0 x1],[y0 y1],'linewidth',2,'color','b');
   hold on;
   % Creating the bob
   plot(x1,y1,'-o','markers',10,'markerfacecolor','r');
   grid on;
   %Determining the x and y limits
   axis([-1.5 1.5 -1.5 0.5])
   % pausing the program 
   pause(0.0003);
   hold off
   %Saving the figure created each while for loop is called with a different name
   
   Text = sprintf("pendulum%d.png",ct);
   saveas(1,Text)
   ct=ct+1;
end

calling function program:

%program to convert the 2nd order equation into first order equation
function[dtheta_dt] = ode_func(t,theta,b,g,l,m)
    theta1 = theta(1) 
    theta2 = theta(2)
    dtheta1_dt = theta(2);
    dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
    dtheta_dt = [dtheta1_dt;dtheta2_dt];
end

output:

REPORT:

1.AIM:

To write a Program in Mat lab/Octave to stimulate the motion of the simple pendulum by converting the 2nd orderr differential equation into first order equation.

2.OBJECTIVES OF THE PROGRAM:

The main objective is to solve the 2nd order differential equation. Here the 2nd order equation which represents the motion of the simple pendululm with respect to time is given.

The given 2nd order equation is as follow:

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

where,

  • b is the damping b=0.05 (given)
  • m is the mass of the bob m=1kg (given)
  • l is the length if the string l=1m (given)
  • g is the gravitational constant g = 9.81 (constant)

3. BODY OF THE CONTENT:

In order to solve the 2nd order equation it must need to be convert into 1st order euqation to solve the problem easily. Here this process is occur in the called function program. The called program on converting the equation is explained below.

Line 1: Description of purpose of the program.

Line 2:function is called, name of the function is ode_func, input parameters required are t, theta, b, l, g, m all these values must need to be assumed to these particular variable as before the function is called in the calling main program.

Line 3: here breaking of 2nd order into 1st order equation begins. assume theta1 = theta(1) i.e,`theta = theta1`

Line 4: i.e,`(d(theta))/dt = (d(theta1))/dt = theta2` intermediate variables are assigned

line 5: i.e,`(d^2theta)/dt^2 = (d^2(theta1))/dt^2 = d/dt((d(theta1))/dt) = d/dt(theta2)`

line 6: `d((d(theta2))/dt) = - b/m ((d(theta))/dt)-g/l sintheta`

line 7: end the funtction.

The explanation of the calling program is as follow:

Line 1: Purpose of the program is explained.

Line 2-4: Previous variable values are cleared and the commond window is also gets cleared.

Line 5: Inputs required for the program is assigning.

Line 6-7: damping is assigned to the new variable b, where b = 0.05.

Line 9-10:length of the string is assigned to the new variable l = 1 which is in meterrs.

Line 11-12: Gravitational constat value is assigned to the vaiable g which is 9.81 m/s^2.

Line 13-14: Mass of the bob is assigned to the variable m = 1 which is in kg.

Line 16-17: Initial condition for the veloctiy lies between 0 to 3

Line 18-19: Time span is assigned between 0 to 20 secs

Line 21: Solving the ode function

Line 22: Ode 45 cmd is used for calling the function with the required minimum input parameters with respect the time of 20 secs and velocity 30 rad/s

Line 25 : ct value is assigned as 1

Line 26: for loop is begin with i values ranges from 1 to lenght of results values.

Line 28-31: values for (x0,y0) and (x1,y1) are assigned.

Line 32-33: Creating a line which acts as fixed plane in which the fixed end of the string is placed

Line 34: holding the figure

Line 35-36: Creating the string which is printed in the figure crated as before due to hold on cmd

Line 38-40: Creating the bob

Line 41-42: Determining the x and y axxis limits

Line 44: Pausing the program

Line 45: hold off the figure

Line 48: The figure crated each time when the for loop called is saved with the different name.

Line 50: adding 1 to the ct

Line 51: ending tthe program

4. ERRORS AND ITS CORRECTION:

Error occured:

error occured during creating the string and the bob.

Interefrence:

Movment of bob or the string is found to be wrong and in some cases string is failed to create,

Error correction:

By changing the values , altering the code the error is rectified step by step.

some of the errrors occured:

During the above situation of error the string tends to moves vertically while the bob oscillates in another direction.

Like various error has occured.

Conclusion:

The 2nd order euqation is converted into 1st order equation and the assigned input values are called using the ode45 cmd with respect to the time and velcotiy. After correcting the various error ocuured and the final desired output is obtained.

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.