Challenge

Week 3 - Solving second order ODEs

AIM- To solve the second order given ODE

INTRODUCTION AND EQUATIONS- A simple pendulum A weight of mass m is attached to the end of a weightless rod (or string) of length L that rotates on a frictionless axle. The mass could move in a large circular arc:

2nd Order ODEs are the equations that have a function with two derivatives. Here the given ODE is `(d^2theta)/(dt^2)+(b/m)(d theta)/dt+g/Lsintheta=0`

Coversion of 2nd order ODE to 1st order-`

Let `theta=theta_1`

`(d theta)/dt=(d theta_1)/dt=theta_2`

`(d^2 theta)/dt^2=(d^2 theta_1)/dt^2=d/dt((d theta_1)/dt)=d/dt(theta_2)`

`(d^2 theta)/dt^2+b/m(d theta)/dt+g/Lsintheta=0`

`(d^2 theta_1)/dt^2+b/m (d theta_1)/dt+g/L sintheta_1`

`(d theta_2)/dt+b/m theta_2+g/L sintheta_1=0`

now here we have, `(d theta_1)/dt=theta_2` and `(d theta_2)/dt=-b/m theta_2 -g/L sintheta_1`

OBJECTIVE- To solve the given 2nd order ODE and to plot and simulate the pendulum motion.

BODY OF THE CONTENT- 1. SAMPLE CODE-

clear all
close all
clc
%To input the values
l=1
m=1;
d=0.05
g=9.81
t=0
theta=0

%Initial conditions of pendulum [displacement;velocity]
theta_0 =[0;3]

%To integrate ODE to find out the solution
%So the value of time is to be defined
t_span=linspace(0,20,500)

%To slove the ODE function
[t,results]=ode45(@(t,theta)ode_function(t,theta,d,g,l,m),t_span,theta_0)

%Plotting the results
figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
xlabel('TIME')
ylabel('PLOT')

%To animate the pendulum motion
ct=1
for i=1:length(results(:,1))
    
    %Coordinates
    x0=0
    y0=0
    x1=l*sin(results(i,1));
    y1=-l*cos(results(i,1));
    figure(2)
    
    %To plot the coordinates
    plot([-1 1],[0 0],'color','r','linewidth',3)
    hold on
    plot([x0 x1],[y0 y1],'color','b')
    hold on
    
    %To plot the pendulum bob
    plot(x1,y1,'o','markers',20,'markerfacecolor','r')
    
    %To define the axis
    axis([-1.5 1.5 -1.5 1.5])
    pause(0.01)
    
    %To make an animation
    M(ct)=getframe(gcf);
    hold off
    ct=ct+1
end
%To make the movie of the animation
movie(M)
videofile=VideoWriter('pendulum animation.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

ODE_function that has been called in the above code-

function [dtheta_by_dt]=ode_function(t,theta,d,g,l,m)
 theta1=theta(1)
 theta2=theta(2)
 dtheta1_by_dt=theta2
 dtheta2_by_dt=-(d/m)*theta2 -(g/l)*sin(theta1)
 dtheta_by_dt=[dtheta1_by_dt;dtheta2_by_dt]
end

OUTPUT-

ERRORS FACED WHILE PROGRAMMING-

To overcome the above mentioned error '@' symbol is to be used after ode45(@(...

To overcome the above mentioned problem the alphabet for mass and for movie should be different.

PENDULUM ANIMATION LINK- https://drive.google.com/file/d/1Mqot6zjLTJ_Blp0dtB7rnDOCQGNrPpIL/view?usp=sharing

Youtube link- https://youtu.be/L1cZGsGIjbU

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.