Challenge

Week 3 - Solving second order ODEs

Aim: To write a program that solves the ODE that represents the equation of motion of simple pendulum

Introduction: A second order differential equation is an equation involving the unknown function y, its derivatives y' and y'', and the variable x.

In this project we will use a simple pendulum example to explain 2nd order ODE. A simple pendulum consists of mass ‘m’ hanging from a string of length ‘l’. With displacement to an angle Ө.

The Equation used to solve 2nd Order ODE is

Let’s say

Values taken in the problem

l= 1m

m=1kg

b=0.05

g=9.8m/s^2

Initial conditions are

Ang velocity = 3 rad/sec at t=0

Ang displacement =0

% Code for the following ODE

clear all
close all
clc

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

%initial condition
theta_0=[0;3];

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

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

%Animating the transient motion of the 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','g')
    axis([-1.5 1.5 -1.5 0.5])
    plot(x1, y1,'o','MarkerSize',20,'Color','r')
    pause(0.001)
    M(ct)=getframe(gcf)
    hold off
    ct=ct+1

end
%making video file

movie(M)
videofile=VideoWriter('simple_pendulum.avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)

% Results

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

% Errors

Results: With this code we can solve ODE function

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.