Challenge

Week 3 - Solving second order ODEs

In this project we attempt to write a program to solve a Second order differential Equation using MATLAB. We, also write a program to represent the pendulum for the given ODE.

The parameters are already given in the question so, we use the same parameters to solve the ode.

The following program is written as follows :

clear all
close all
clc

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

% initial conditions 
theta_a = [0;3];

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

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

output = results(:,1);

%contour_command
ct = 1;
for i = 1:length(output)
    disp = output(i);
    
    % start position
    x0 = 0;
    y0 = 0;
    
    % final position
    
    x1 = l * sin(disp);
    y1 = -l * cos(disp);
    
    % plot
    
    plot([x0 x1], [y0 y1], x1,y1, '.','markersize',20,'linewidth',4)
    axis([-1.3 1.3 -1.3 1.3])
    hold off
    grid on
    M(ct) = getframe(gcf);
    ct = ct+1;
end

% animation
movie(M)
videofile = VideoWriter('ode_pendulum');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Now we write a code to produce the damping graph as follows :

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

The given link is the YouTube video https://youtu.be/W4bQE_I1YBk

Ode_func is a command written to solve various differential equations :

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

As shown above we have successfully solved the Second Order Diffrential Equation by the given parameters.

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.