Fuzzy Logic Tools v1.0
matlab_utilities/fuzjac.cpp

MEX file that calculates the Jacobian matrix of the closed loop fuzzy system.

MATLAB help:

% FUZJAC Calculates the Jacobian matrix of the closed loop fuzzy system.
%
%   J = fuzjac(X, Plant, Controller)
%
% Arguments:
%
%   X -> State vector where the jacobian matrix is calculated.
%
%   Plant -> Fuzzy model of the Plant.
%
%   Controlador -> Fuzzy model of the Controller.
%
%   J -> Jacobian matrix of closed loop system.
%
%                                  (  df1          df1  )
%     dx1                          | -----   ...  ----- |
%    ----- = f1(x)                 |  dx1          dxn  |
%     dt                           |                    |
%     ...                      J = |  ...    ...   ...  |
%     dxn                          |                    |
%    ----- = fn(x)                 |  dfn          dfn  |
%     dt                           | -----   ...  ----- |
%                                  (  dx1          dxn  )
%    x = [ x1 ; x2 ; ... ; xn]
%
%   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, aproxlinear, conseq2mat, fis2txt,
%         fuz2mat, fuzcomb, fuzeval, fuzlinear, fuzprint, mat2antec, mat2conseq,
%         mat2fuz, subsystem, txt2fis

Source code:

/*  Copyright (C) 2004-2011
  ANTONIO JAVIER BARRAGAN, antonio.barragan@diesia.uhu.es
  http://uhu.es/antonio.barragan

  Collaborators:
  JOSE MANUEL ANDUJAR, andujar@diesia.uhu.es
  MARIANO J. AZNAR, marianojose.aznar@alu.uhu.es

  DPTO. DE ING. ELECTRONICA, DE SISTEMAS INFORMATICOS Y AUTOMATICA
  ETSI, UNIVERSITY OF HUELVA (SPAIN)

  For more information, please contact with authors.
  
  This software is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This software is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "aux_matlab.hpp"
#include <flt/derivatives.hpp>

using namespace FLT;
using namespace TNT;

void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  size_t i,q,n;
  int model;
  double *X,*J,*d;
  System S[2];
  char Sist[MAX_FILE_NAME];
  char error[MAX_FILE_NAME];
  
  if(nrhs!=3)
    ERRORMSG(E_NumberArgIn)
  if (nlhs>1)
    ERRORMSG(E_NumberArgOut)
  for (i=0;i<nrhs;i++)
    if (mxIsEmpty(prhs[i]) || mxIsNaN(*mxGetPr(prhs[i])))
      ERRORMSG(E_ArgNoValid)
      
  X = mxGetPr(prhs[0]);
  if (!X)
    ERRORMSG(E_NumberArgIn)
  
  n = mxGetM(prhs[0]);
  if (mxGetN(prhs[0])!=1)
    ERRORMSG(E_Column)
  
  for (model=1;model<3;model++)
  {
    if (readModel(prhs[model],S[model-1]))
    {
      sprintf(error,"%s %d.\n",E_Model, model);
      ERRORMSG(error)
    }
  }
  
  if ((nrhs==3) && n!=S[0].outputs())
    ERRORMSG(E_PointCoherent)
  if ((nrhs==2) && n!=S[0].inputs())
    ERRORMSG(E_PointCoherent)
  
  Array2D<double> Jac = jacobian(S[0], S[1], X);
  if (!Jac.dim1())
  {
    mxDestroyArray(plhs[0]);
    ERRORMSG(E_NoCoherent)
  }
  
  plhs[0] = mxCreateDoubleMatrix(n,n,mxREAL);
  J = mxGetPr(plhs[0]);
  if (!J)
  {
    mxDestroyArray(plhs[0]);
    ERRORMSG(E_NumberArgOut)
  }
  for (i=0;i<n;i++)
  {
    for (q=0;q<n;q++)
    {
      *J = Jac[q][i];
      J++;
    }
  }
}
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines