Challenge

Week 3 - Solving second order ODEs

AIM:

To solve the SECOND ORDER ODE, by classifying into smaller parts and deriving the equations using MATLAB software.

PROCEDURE:

* First of all, we should understand the given data in what equation is shown in the requirement.

* From the given equation in the requirement,

*g = gravity in m/s

*L = length of the string in m

*m = Mass of the bob

*b = damping coefficient

*theta = angle made by string with the fixed part.

* To start the program, give clear all close all clc

* From the given data, declare the values of

b = 0.05
g = 9.81
l = 1
m = 1

* The velocity, displacement are to be declared so far, and so it is stored in an array called theta_0

theta_0 = [0;3];

* Give t_span = linspace(0,20,500); where t_span denotes the time array.

* Give %solving ode, Give [t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);

* The above values are said to be ode_func that is declared and the syntax for the function is given.

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

* Hence, this ode_func accepts the values of t,theta,b,g,l,m

* And so, ode_45 is a default solver, and it accepts the value of t,theta as inputs.

* plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('plot')
grid on
fig=1

This commands are used to plot the t, result values in the graph and hold on is used to generate the second plot in the same window.

* Generate FOR loop and declare i = 1:length(t)

* Declare % Initial position of the Pendulum

x = 0;

y = 0;

* Declare % Final position of the Pendulum

x1 = 1*sin(results(i,1));
y1 = -1*cos(results(i,1));

* Plot the base line of the pendulum

plot([-1,1],[0,0],'linewidth',4,'color','g');

* Plot the pendulum line

line([x0 x1],[y0 y1],'linewidth',3,'color','r');
hold on;

* Plot the pendulum ball

plot(x1,y1,'o','markers',30,'markerfacecolor','b');

generates the pendulum ball in blue colour.

* Plot according to the axis ([-2 2 2 1])

* pause (0.01) pauses the animation.

* M(fig) = getframe(gcf) captures the frame.

* fig = fig+1 is the loop counter for multiple generation of plots in above programme.

* end used for ending the FOR loop.

* %creating a movie
movie(M);
videofile = VideoWriter('pendulum.avi', 'Uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

* This is used for creating the video of this animation, in the format of .avi

PROGRAM DONE:

clear all
close all
clc

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

%initial condition
theta_0 = [0;3];

%time points
t_span = linspace(0,10,100);

%solving 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))
xlabel('time')
ylabel('plot')
grid on
fig=1

for i = 1:length(t)
  
    
    %initial position of pendulum
    x0 = 0;
    y0 = 0;
    
    %final position of pendulum
    x1 = 1*sin(results(i,1));
    y1 = -1*cos(results(i,1));
    
    %plotting
    figure(2)
    %base line
    plot([-1,1],[0,0],'linewidth',4,'color','g');
    %pendulum line
    line([x0 x1],[y0 y1],'linewidth',3,'color','r');
    hold on;
    %pendulum ball
    plot(x1,y1,'o','markers',30,'markerfacecolor','b');
    grid on
    
    axis([-2 2 -2 1]);
    pause(0.01);
    hold off;
    M(fig) = getframe(gcf);
    fig = fig+1;
end

    
%creating a movie
movie(M);
videofile = VideoWriter('pendulum.avi', 'Uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Conclusion:

Thus, the solving of second order ODE is done and executed by using MATLAB software.

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.