Challenge

Solving second order ODEs

AIM : Solving second order ODEs

EQUATION :

B = Damping coefficient

g = Gravity (m/s2)

l = Length of pendulum (m)

m = Mass of the ball (kg)

From above diagram we under stud that we at any angel the value of x-axis & y-axis are different because of changing value of “ theta ” thus X = L*sinθ , Y=L*cosθ

clear all
close all
clc

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

%Initial condition
theta_0 = [0;5];


%time point
t_span = linspace(0,20,500);

%solve ODE
[t,results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span,theta_0);

%plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('theta_0')
xlabel('time')

ct=1;
for i=1: length(results(:,1))
    
    x0 = 0;
    y0 = 0;
    x1 = l*sin(results(i,1));
    y1 = -l*cos(results(i,1));
    figure(2)
    plot([-1 1],[0 0], 'linewidth',2,'color','b');
    axis([-1.5 1.5 -1.5 1.5]);
    line([x0 x1],[y0 y1],'linewidth',2,'color','black');
    hold on;
    plot(x1,y1,'o','markersize',20,'markerfacecolor','black','markeredgecolor','black');
    hold off;
    M(ct) = getframe(gcf);
    ct=ct+1;
end
movie(M)
Videofile=VideoWriter('pendulum.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)

CODDING OF FUNCTION :

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

At the starting of main coding we write ' clear all ' , clc, due this before value of coding are erase in command. After that we have assign input values such as Damping coefficient, Gravity, Length of pendulum, Mass of the ball. The oscillation of the pendulum is depend on mass of the ball, damping coefficient, Gravity. Then we give displacement range from 0 to 5,which is on y-axis of the figure 1. Now we give the value of the time from 0 to 20 sec and is given by linspace command and between those range we take 500 value for plotting of the graph. linspace(0,20,500)

Now to solve the ODI we call the function as “ode_func(t, theta, b,g,l,m)” since we have pass the value via function thus calculation are done by the formula

And the we start plotting between time and result that we have get via function by passing the command of plot . to understand the graph we give label to it by passing command xlabel & ylabel.

Now for different value of time and angel we wanted different position of the pendulum thus we have to use for loop. By initialising ct value as 1.

Next step is to create a movie, So in MATLAB it uses videowriter command for creating the movie. For creating movie we have to start with array M(ct)and get an individual frame for each position. Here (ct) represent the individual frame. Now we have to use “getframe” command to get the current figure. And the code will be written in the form of “M(ct)= getframe(gcf)” Movie(M) indicate the movie file and we have to give video format that in which format we want output movie file. For this particular animation, I am going to use.avi file with movie name “ode_func”

OUTPUT :

ERROR :

1). Since angel required in radian , I written “ sind ” instead of “ sin ”.

2). Before end of for loop I have not return “ hold off ” due to this kind of output

youtube link : https://youtu.be/atcQ-5KDFqs

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.