Challenge

Week 3 - Solving second order ODEs

What are ODE ?

An ordinary differential equation (ODE) is an equation that involves some ordinary derivatives of a function. Often, our goal is to solve an ODE, i.e., determine what function or functions satisfy the equation.

a simple ode - dx/dt=0

second order differential equation

where b is damping coefficient

m is mass, g is acceleration due to gravity, length of the string.

program for solving the second order ode:

function [dtheta_dt] = ode_function(t,theta,b,g,l,m)
    theta1 = theta(1)
    theta2 = theta(2)
    dtheta1_dt = theta2;
    dtheta2_dt = -(b/m)*theta2-(g/1)*sin(theta1);
    dtheta_dt = [dtheta1_dt; dtheta2_dt];
end

program for pendulum and animation:

clear all
close all
clc

% inputs for the program
% damping coefficient
b = 0.05; 
% acc due to gravity m/s^2
g = 9.81;
% mass of the bob in kg
m = 1;
% length of the string in m
l = 1;

% initial condition
disp_ang_vel = [0;5];

% timepoints
t_span = linspace(0,20,100);

% solving ode
[t,results] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span, disp_ang_vel);

% plotting
% first figure
plot(t,results(:,1),'linewidth',1)
hold on
plot(t,results(:,2),'linewidth',1)
axis([0 25 -3 3])
xlabel('time-sec')
ylabel('angular velocity & displacement')

figure(2)
plot(results(:,1),results(:,2))
xlabel('position-rad')
ylabel('angular velocity rad/sec')

figure(3)
theta2 = results(:,1);
ct = 1;
for i = 1:length(theta2)
    theta3 = theta2(i);
    x0 = 0;
    y0 = 0;
    
    x1 = 1*sind(theta3);
    y1 = -1*cosd(theta3);
    
    plot([-2 2],[0 0],'linewidth',3)
    hold on
    plot([x0 x1],[y0 y1],'linewidth',3)
    plot(x1, y1,'marker','.','markersize',40)
    hold off
    axis([-0.2 0.2 -1 1])
    M(ct) = getframe(gcf);
    ct = ct+1;
end

movie(M)
videofile = VideoWriter('pendulum.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

explanation of the code:

1. first we provide the static inputs for the program, damping coefficient, acceleration due to gravity, mass of the bob, length of the string.

2. initial conditions and timepoints are given

3. the ode function is called using [t, results] and calling ode_function from the programmed tab.

4. we have plotted three figures in this program

5. the first plot is angular velocity and displacement vs time

6. the second plot is angular velocity vs position in radians

7. plot 3 is the animation of the pendulum, it has 3 parts the axis which is stable, the pendulum string which changes values both vertically and horizontally with increment in angle theta which is changed by the for loop, to dictate a bob we have used marker.

8. the movie code captures the frame and videowriter writes the file name and dictates the quality of the file.

plots

second plot

errors faced

1. for the plotting of the third plot i missed 1: in the beginning of the for loop as a result of which the plot wasnt animated and showing me the final plot

2. since the ode function was programmed in a different editor window it wasnt named similar to the function that is why when calling the function the error was showing unrecognized function ode_function.

animation

https://drive.google.com/file/d/1K918j0ON7WLa53BlhxIMgEWXpVYHQ__c/view?usp=sharing

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.