MATLAB - P2prod/Filter-Modeling-Simulation GitHub Wiki
MATLAB is a scripting language for numerical computation, like SCILAB and OCTAVE. The name MATLAB is based on the English MATrix LABoratory. MATLAB also has a graphical environment, SIMULINK, which lets you draw diagrams using functional blocks from dedicated libraries, linked together to create complex functions and systems that can be simulated and whose results can be represented in the form of curves. In addition, a number of add-on ToolBoxes are available to enhance calculations and simulations. It is even possible to perform symbolic calculations using the dedicated toolbox.
If we compare MATLAB to MATHCAD, which can also handle symbolic operations, we can say that MATHCAD is more user-friendly because it doesn't use scripts but worksheets on which you write equations and formulas as if they were on a blackboard, in other words in handwritten-like form. This is a major advantage of MTAHCAD. But MTAHCAD has no equivalent of SIMULINK.
1st example with the transfer function
% Lead-lag filter step response variante with Transfer function
clear all;
T1 = 5;
T2 = 10;
num = [T1 1];
den = [T2 1];
sys = tf(num,den)
step(sys);
2nd example with the recurrence equations
% Lead Lag Filter with recurrence equations
% Warning : all mathematical sequences start at n = 1 in MATLAB
clear x;
clear y;
clf();
N = 4000 ;
x = ones(N,1);
y = zeros(N,1);
% Parameters
Te = 0.01;
T1 = 10;
T2 = 5;
% Initial conditions
x(1) = 1;
y(1) = T1/T2;
% Coefficients
a = 1 - (Te/T2);
b = T1/T2;
c = (Te - T1)/T2;
% Recurrence equation
for n = 2:N
y(n) = a*y(n-1) + b*x(n) + c*x(n-1);
end
plot(y);
4th example with state space approach
% Lead-lag filter state space model
% Parameters
T1 = 10;
T2 = 5;
% Matrices
A=-1/T2;
B = 1/T2;
C = (1-T1/T2);
D = T1/T2;
LLF = ss(A, B, C, D);% Building the model
% Plots
impulse(LLF);
figure;
step(LLF);
% Finding the transfer function (it's a plus)
[b,a] = ss2tf(A, B, C, D);