Challenge

Week 3 - Solving second order ODEs

AIM : To slove the second order equation using MATALB.

INTRODUCTION :

An ordinary differential equation is a differntial equation containing one or more functions of one independent variable and teh derivatives of these functions.

OBJECTIVES:

1. To slove the second order ODE with graph of velocity v/s displacement.

2. To create the animation of the simple pendulum.

GIVEN DATA:

simulate teh motion between = 0 - 20

for angular displacement =0

angular velocity = 3 rad/sec

time =0

L= 1m

g= 9.81m/s^2

b = 0.05

m=1kg

MATLAB PROGRAM :

main program:

clear all
close all
clc

% intial variables for  the second order ode of simple pendulam

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

% intial condition

theta_0 = [0,3]


% time

t_span = linspace(0,20,260)

%slove 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))


% animation of simple pendulum

figure(2)

ct = 1

for i = 1 : length(results(:,1))

    x0 = 0 ;
    y0 = 0 ;

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

    plot ([0,0] , [-1.5 0.5])
    hold on

    plot ([x0 x1],[y0 y1],'linewidth',1,'color','b')
    axis ([-1.5 1.5  -1.5 0.5])
    plot (x1 , y1 , 'o' , 'Markersize',20,'Markerfacecolor','r','color','r')
    pause (0.0001)
    M(ct) = getframe(gcf)
    hold off

    ct = ct + 1

end

%video file

movie(M)
videofile = videowriter( 'simple_pendulum.avi')
open("videofile")
writevideo(videofile ,M)
close (videofile)

ode function program :

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

CODE EXPLANATION :

  1. Firstly, the given details are assigned as variables in the code.
  2. Then time span of teh simple pendulum is set by using linspace command.then theta value is given as a column matrix.
  3. after this use ode45 function and 'ode_func".
  4. use plot command to plot displacement v/s velocity graph.
  5. use for loop function commad to to simulate the simple pendulum.
  6. x0 y0 is fixed coordinates and x1 y1 are the orther end.
  7. Marker command is used for making pendulum bob and Markerfacecolor is used to colour the bob.
  8. videowriter is used to create teh video file.

RESULTS:

graph of displacement v/s velocity:

Animation of simple pendulum:

ERRORS:

CONCLUSION:

Second order ODE is sloved using matlab and the displacement v/s velocity graph is plotted . Animation of the simple pendulum is created using matlab.

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.