MEX file that calculates the linealization of a Fuzzy Model in a point usign finite differences.
MATLAB help:
% APROXLINEAR Linealizes a Fuzzy Model usign finite differences.
%
% A = aproxlinear(Fuzzy_Model, X, U)
%
% A = aproxlinear(Fuzzy_Model, X, U, h)
%
% [A,B] = aproxlinear(Fuzzy_Model, X, U, h)
%
% [A,B,F] = aproxlinear(Fuzzy_Model, X, U, h)
%
% Solve the matrices the linear representation of the 'Fuzzy_Model' model
% usign finite differences in the point X with signal control U.
% F is the value of the function at the point (X,U).
%
% Y = F + Ax + Bu
%
% Arguments:
%
% Fuzzy_Model -> Input Fuzzy model.
%
% X -> State vector.
%
% U -> Control vector.
%
% h -> Increment. Default value is 0.001;
%
% Fuzzy_Model -> This fuzzy model could be a '.txt' file, a '.fis' file,
% or a 'FIS' variable from MATLAB Workspace.
%
% See also activation, antec2mat, aproxjac, conseq2mat, fis2txt, fuz2mat,
% fuzcomb, fuzeval, fuzjac, fuzlinear, fuzprint, mat2antec, mat2conseq,
% mat2fuz, fuzsubsystem, txt2fis
Source code:
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
size_t i,q,j,n,m=0;
size_t countA,countB;
double *X,*U=NULL,*M_A,*M_B,*M_F,h;
if(nrhs<2 || nrhs>4)
if (nlhs>3)
for (i=0;i<nrhs;i++)
{
if ((i!=2 && mxIsEmpty(prhs[i])) || mxIsNaN(*mxGetPr(prhs[i])))
}
X = mxGetPr(prhs[1]);
if (!X)
if (mxGetN(prhs[1])!=1)
n = mxGetM(prhs[1]);
if (nrhs>=3)
{
U = mxGetPr(prhs[2]);
if (!U)
if (mxGetN(prhs[2])!=1)
m = mxGetM(prhs[2]);
}
else if(m!=0)
{
U = new double[m];
for (i=0;i<m;i++)
U[i]=0;
}
else
U = NULL;
plhs[0] = mxCreateDoubleMatrix(n,n,mxREAL);
M_A = mxGetPr(plhs[0]);
if (!plhs[0] || !M_A)
{
if (nrhs==2 && m!=0)
delete []U;
mxDestroyArray(plhs[0]);
}
if (nlhs>1)
{
plhs[1] = mxCreateDoubleMatrix(n,m,mxREAL);
M_B = mxGetPr(plhs[1]);
if (!plhs[1] || !M_B)
{
if (nrhs==2 && m!=0)
delete []U;
mxDestroyArray(plhs[0]);
mxDestroyArray(plhs[1]);
}
}
if (nlhs==3)
{
plhs[2] = mxCreateDoubleMatrix(n,1,mxREAL);
M_F = mxGetPr(plhs[2]);
if (!plhs[2] || !M_F)
{
if (nrhs==2 && m!=0)
delete []U;
mxDestroyArray(plhs[0]);
mxDestroyArray(plhs[1]);
mxDestroyArray(plhs[2]);
}
}
Array2D<double> A(n,n), B(n,m);
Array1D<double> F(n);
if (nrhs==4)
{
h=mxGetScalar(prhs[3]);
if (h<=0)
}
else
if(!A.dim1())
{
if (nrhs==2 && m!=0)
delete []U;
mxDestroyArray(plhs[0]);
if (nlhs>1)
mxDestroyArray(plhs[1]);
if (nlhs==3)
mxDestroyArray(plhs[2]);
}
for (q=0,countA=0;q<n;q++)
{
for (i=0;i<n;i++,countA++)
*(M_A + countA) = A[i][q];
}
if (nlhs>1)
{
for (j=0,countB=0;j<m;j++)
for (i=0;i<n;i++,countB++)
*(M_B + countB) = B[i][j];
}
if (nlhs==3)
for (q=0;q<n;q++)
M_F[q] = F[q];
}