Challenge

Week 3 - Solving second order ODEs

AIM:

To solve the given second order ODE function using the MATLAB/OCTAVE.

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

ODE45:

ode45 is a function in matlab that solves ordinary or partial differential equations using a numerical methods.

EQUATIONS:

`theta= thetaunderset1`

`d theta/dt=underset(1)(d theta)/(dt)=thetaunderset2`

`d^2 theta/(dt^2)=underset(1)(d^2theta)=d/dt(underset(1)(d theta)/dt)=d/dtthetaunderset2`

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

`d^2underset(1)theta/(dt^2)+b/m(d underset(1)theta /dt)+g/lsin underset(1)theta=0`

`dunderset2theta/(dt^2)+b/m underset2theta+g/lsin underset1theta=0`

`dunderset1theta/(dt)=underset2theta`

`dunderset2theta/(dt^2)=-b/m underset2theta-g/lsin underset1theta`

`d/dt``((underset1theta),(underset2theta))=((underset2theta),(-b/m underset2theta-g/l sin theta))`

CODIND AND EXPLANATION:

MAIN PROGRAM:

clear all
close all
clc

%input parameters
%b=damping co-efficient
%g=gravity
%l=length
%m=mass
b=0.05;
g=9.81;
l=1;
m=1;

%initial conditions we have two 1st order ODE 
%the velocity(5) and displacement(0)
theta_0=[0;5];

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

%ODE is an inbuilt solution in MATLAB
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

ct=1;

figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
title('simple damper pendulum')
xlabel('time')
ylabel('plot')
legend('displacement','velocity')

for i=1:length(results(:,1))
    ref=results(i);
    %co-ordinates
    x0=0;
    y0=0;
    x1=l*sin(ref)
    y1=l*(-cos(ref))
    
    figure(2)
    
    plot([x0 x1],[y0 y1],'linewidth',3);
    axis([-1.5 1.5 -1.5 0.5]);
    pause(0.01)
    
    %get individual frame to create a animated one
    %M is the frame
    M(ct)=getframe(gcf);
    ct=ct+1;
end
%conversion of plot into video
movie(M)
videofile=VideoWriter('ODE_MAIN.avi,uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

FUNCTION:

function[dtheta_dt]=ode_func(t,theta,b,g,l,m)

%solution array
theta1=theta(1);
theta2=theta(2);
%1st ODE
dtheta1_dt=theta2;
%2nd ODE
dtheta2_dt=(-b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end

ERRORS:

  • The symbols in the equations are written wrong, the space between the ode_func and the (t,theta) is been done.

  • The y0 is been written as yo in the plotting.

OUTPUT:

  • The velocity and displacement values are been ranging to zero from their initial point of 0 and 5 respectively.

VIDEO LINK of pendulum motion:https://youtu.be/nm5HQwlcN80

RESULT:

Thus the given second order ODE is been solved, coded and explained.

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.