GeeKee CeeBee

Welcome to GeeKee CeeBee's Page: House of Mechatronics Projects & Lessons.
Contact Email:
Follow me on Youtube

(Matlab) Differential Eq.

using Finite Difference Method in Discrete Time

Step-by-step video

In this video guide, you will learn how to solve differential equations using finite difference approximation method. Video covers the theory of how to use FDM to solve and how those equations work on paper. Then, video talks about the MATLAB code implementation in discrete time to simulate the solution to ODEs and plot the solution. In particular, the most well-known spring-mass-damper system example is used for this tutorial. Please watch the whole video for better understanding.

Matlab Code


% By GeeKee CeeBee
% Solve Differential Using Finite Difference Method in Discrete Time
% This example looks at Spring Mass Damper System

clear all; close all; clc
K=1000;                      % Spring Constant
M= 10;                       % Mass
C= 100;                      % Damping Coefficient
dt=0.001;                    % Time step
t=[0:dt:5];                  % Time range
y= 0.1 * heaviside(t-1);     % Step function
x(1)=0;                      % Initial Conditions

A1= (C/dt)+(M/dt^2)+K;
A2= (-2*M/dt^2)-(C/dt);
A3=(M/dt^2) ;
A4= (C/dt)+K;
A5= (-C/dt);
for k=3:length(t)
   x(k)= (1/A1)*(A4*y(k)+A5*y(k-1)-A2*x(k-1)-A3*x(k-2));
figure; plot(t,x,t,y,'linewidth',2);
xlabel('t (s)');
ylabel('X (m)');
legend('x(t)', 'y(t)');
title('Displacement vs Time');