Challenge

MATLAB Challenge - Solving second order ODEs

Motion/Animation is in drive link attached at last.

Second-order ODE Project

Aim - write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.

Solution:

In this project, we will write the code for a simple pendulum with a spring, mass, and damper system by a second-order ODE as

The Simple Pendulum

We will solve for the value of theta(Ɵ) w.r.t time(t), where

b= damping coefficient

m= mass of the ball in kg

L=length of the pendulum in m

g= gravity

Concept:

We will break down this second-order ODE to lower-order i.e., first order.

CODE

clear all 
close all
clc

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

%initial condition
theta_0=[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_0);
ct=1;
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
axis([0 25 -4 4])
ylabel('plot')
xlabel('time')

for i=1:length(t)
x=L*sin(results(i,1));
y=-L*cos(results(i,1));
figure(2)
plot([-1,1],[0,0],'color','r','linewidth',3)
hold on
plot([0,x],[0,y],'color','g','linewidth',2)
plot(x,y,'o','markers',15,'markerfacecolor','r')
axis([-2 2 -2 2])
hold off
pause(0.05)
M(ct)=getframe(gcf);
ct=ct+1;
end

%Animation
movie(M)
videofile=VideoWriter('Second order pendulum');
open(videofile)
writeVideo(videofile,M)
close(videofile)

ode 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:

Givens

  • b= 0.05
  • m= 1
  • L=1
  • Time interval = 0-20
  • Angular displacement=0
  • Angular velocity=3

Calculating

  • For solving second order ODE, initial conditions should be given which are initial displacement of pendulum=0 and initial velocity=3m/s
  • In the interval of 20s, we have taken the 100 values by using the command ‘linspace’.
  • We will now use command ode45 which is an inbuilt command for solving a differential equations.
  • Then we will solve the second-order differential equation and encode it using the function ode_func.
  • Then, we will use the for loop, and contor command is used to change loop irritation after every motion, using marker command we can create the bob of the pendulum.
  • By using videowriter command we will create the animation by capturing each frame by getframe(gcf).

Errors faced while

Got some problems in for loop then contacted for a one-to-one session then it get resolved and some typo error.

Result:

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

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.