Challenge

Week 3 - Solving second order ODEs

PROGRAME CODE:

clear all
close all
clc

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

% initial condition
theta_0 = [0;5];

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

% solve 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))
ylabel('plot')
xlabel('time')
title('Differential equation')

%to create pivot points
x0=0
y0=0
plot(x0,y0,'marker','o')

% To extract the first coloumn of a result & assign it to y ;
y=results(:,1);

ct=1
for i=1:length(t_span)
    x1=l*sin(y(i));
    y1=-l*cos(y(i));
    plot([x0 x1],[y0 y1],'linewidth',3,'marker','o','Color','r')
    hold on
    plot(x1,y1,'marker','o','Color','g','markersize',35,'MarkerFaceColor','g')
    hold off
    axis([-1 1 -2 0])
    pause(0.03)

    M(ct)=getframe(gcf);
    ct=ct+1;
end

movie(M)
videofile=VideoWriter('murali.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)



FUCTION CODE:

	
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

VIDEO FILE LINK :

https://youtu.be/aGN1KAeL8Zs

EXPLANATION :

SECOND ORDER ODEs

OBJECTIVE:

In this challenge I have to write a program code to solve the following one-dimensional equation (ODE). This ODE represents the equation of motion of a simple pendulum with damping. So, the main objective of this challenge is I have to write a code for create a pendulum animation.

THEORY:

Second order differential equation is a specific type of differential equation and a differential equation is consisting of a function and its derivative. The differential equation must include the function and second order derivative. A differential equation is an equation with a function and one or more derivatives are there the the differential equation is given below

y + dy/dx = 5x

this is the example of an equation with the function y and its derivative dy/dx

Order: now we are going to see the differential equation in this order it is having the highest derivative so let we the first order derivative equation

dy/dx + y^2 = 5x

it has only the first derivative dy/dx so it is called “First Order” so now we see the second order differential equation

d^2y/dx^2 + xy = sin(x)

it has a second derivative d^2y/dx^2 so it is called “Second Order” and now we see the third derivative

d^3y/dx^2 + x(dy/dx) + y = e^x

it is called third derivative because it has d^3y/dx^3 the outranks of the dy/dx.

Second order differential equations:

We can solve a second order differential equation of the type

d^2y/dx^2 + P(x)dy/dx + Q(x)y = f(x)

Above the equation is called second order one dimensional equation.

Now let we see in this challenge we have to use this below equation and we get the result of this challenge.

d^2 theta/dt^2 + b/m(d theta/dt) + g/L(sin theta) = 0

In the above equation is,

g = gravity in m/s^2

l = length of the pendulum in m

m = mass of the ball in kg

b = damping coefficient

according to this equation value to create a code and we will get a pendulum of second order ODE function. In matlab we have use the marker command to create a sphere and if we have to use the movie command to create an animation.

CONCLUSION:

Finally, I wrote a program code for second order ODE and then we got a output like pendulum animation.

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.