Challenge

Week 3 - Solving second order ODEs

Aim: -Write a program in MATLAB to solve the ode function and simulate the pendulum motion

Objective: - To solve the given ode function using ode45 MATLAB solver and simulate the ode function using a simple pendulum.

Introduction to ode45: -

Ode45 is a standard MATLAB solver for ordinary differential equation. This function implements a Runge-Kutta method with a variable time step for

efficient computation. The second order ode can be solved by breaking the higher order ode into several single order ode.

The following equation is been solved and reduced into single order differential equation

This differential equation is further solved and differentiated into several single order ode

Inputs: -

g=9.81 m/s^2 gravity in m/s^

l=1 m length of the pendulum in m

m= 1 kg mass of the ball in kg

b= 0.05 damping coefficient

clear all
close all
clc

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

%initial conditions
theta_0=[0;3];

%time plot
t_span=linspace(0,20,200);

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

%plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
title('Pendulum plot')

ct=1;
for i=1:length(results(:,1))
    
    figure(2)
   %initial position
   x0=0;
   y0=0;
   
   x1=l*sin(results(i,1));
   y1=-l*cos(results(i,1));
   
   plot([x0 x1], [y0 y1],'linewidth',3)
   hold on
   plot([-1 1],[0 0],'linewidth',3,'color','r')
   plot(x1,y1,'o','markersize',15,'markerfacecolor','g')  %To get the location of the bob
   axis([-2 2 -1.5 1.5])
   pause(0.03)
   hold off
   grid on 
   M(ct)=getframe(gcf);
   ct=ct+1;  %to capture each frame
  
end
movie(M)
videofile=VideoWriter('simple_pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Function Code

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

Code Explanation

  1. The above inputs are provided in the MATLAB script
  2. The theta value is given to the Matlab in the form of a matrix. The matrix contains displacement and velocity value respectively. Displacement=0 m and velocity = 3 m/s
  3. The time input is provided. The time input is converted into equal spaces using linspace command.
  4. Ode45 solver is used to solving the above differential equation. Ode45 solver function is programed and initiated.
  5. The ode45 solver is then used to solve the equation with the above inputs.
  6. Using iterations, we simulate the pendulum motion
  7. We then define the pivot, base, the bob location for the pendulum
  8. We then use the counter command to obtain a separate frame for each motion
  9. After the animation is simulated, the movie command is used to stitch frames and create the animation.
  10. The created file is saved into .avi format file

Pendulum plot

Errors

Basic typing error for writeVideo command.

Using cosd and sind instead of sin and cos resulting in incorrect animation.

animationhttps://drive.google.com/file/d/1gpFt_Ap8S3_xKwOhZCVrc4qd0guLPYn4/view?usp=sharing

hold off command missing resulting in incorrect

Animation for pendulum

https://youtu.be/R1NNEw7-2T8

The Ode45 solver was studied and programmed accordingly for solving the above differential equation.

The pendulum animation was created by using the ode45 solver

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.