# lolimotDemo3 Demo 3: Static process with 2 inputs and 2 outputs

    LoLiMoT - Nonlinear System Identification Toolbox
    Torsten Fischer, 20-October-2011
    Institute of Mechanics & Automatic Control, University of Siegen, Germany
    Copyright (c) 2012 by Prof. Dr.-Ing. Oliver Nelles

    % Training
    LMN = lolimot;                   % initialize lolimot object

    % Generate training data
    [u1g u2g] = meshgrid(linspace(0,1,25), linspace(0,1,25));
    u1 = u1g(:);
    u2 = u2g(:);
    y1 = 1./(0.1+u1) + (2*u2).^2;
    y2 = u1.*u2;
    y2 = y2 + 0.2*randn(size(y2))*(max(y2)-min(y2));
    LMN.input = [u1 u2];
    LMN.output = [y1, y2];

    LMN.maxNumberOfLM = 15;           % Termination criterion for maximal number of LLMs (default: inf)
    LMN.minError = 0.05;              % Termination criterion for minimal error (default: 0)
    LMN.kStepPrediction = 0;          % Static model (default: 0)
    LMN.history.displayMode = true;   % display information (default: true)
    LMN.xRegressorDegree = 1;         % use 1st order polynoms for local models (default: 1)

    % Train net object
    LMN = LMN.train;

    % Generalization
    [u1G u2G] = meshgrid(linspace(0,1,30), linspace(0,1,30));
    u1G = u1G(:);
    u2G = u2G(:);
    y1G = 1./(0.1+u1G) + (2*u2G).^2;
    y2G = u1G.*u2G;

    % Simulate net
    yGModel = calculateModelOutput(LMN, [u1G u2G], [y1G y2G]);

    % Calculate loss function value
    JG = calcGlobalLossFunction(LMN, [y1G y2G], yGModel);

    % plot model output for first output
    figure
    LMN.plotModel([1 2],1)

    % plot model output for second output
    figure
    LMN.plotModel([1 2],2)

    % plot partition of the input space
    figure
    LMN.plotPartition

    xInputDelay is empty, defaults are used: xInputDelay(1:p) = {0}
    zInputDelay is empty, defaults are used: zInputDelay(1:p) = {0}
    xOutputDelay is empty, defaults are used: xOutputDelay(1:p) = {[]}
    zOutputDelay is empty, defaults are used: zOutputDelay(1:p) = {[]}

    Initial net has 1 local linear model(s): J = 0.626324.

    1. Iteration. Number of local linear models = 1. Checking for split of model 1 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.505950.
       Testing split in dimension 2 with ratio 0.50: J = 0.586840.
    -> Splitting in dimension 1 with ratio 0.50: J = 0.505950 and penalty = 0.510598.

    2. Iteration. Number of local linear models = 2. Checking for split of model 2 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.435335.
       Testing split in dimension 2 with ratio 0.50: J = 0.501448.
    -> Splitting in dimension 1 with ratio 0.50: J = 0.435335 and penalty = 0.440570.

    3. Iteration. Number of local linear models = 3. Checking for split of model 3 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.429830.
       Testing split in dimension 2 with ratio 0.50: J = 0.420472.
    -> Splitting in dimension 2 with ratio 0.50: J = 0.420472 and penalty = 0.426425.

    4. Iteration. Number of local linear models = 4. Checking for split of model 4 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.381789.
       Testing split in dimension 2 with ratio 0.50: J = 0.415637.
    -> Splitting in dimension 1 with ratio 0.50: J = 0.381789 and penalty = 0.388263.

    5. Iteration. Number of local linear models = 5. Checking for split of model 5 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.381080.
       Testing split in dimension 2 with ratio 0.50: J = 0.373932.
    -> Splitting in dimension 2 with ratio 0.50: J = 0.373932 and penalty = 0.380832.

    6. Iteration. Number of local linear models = 6. Checking for split of model 7 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.372287.
       Testing split in dimension 2 with ratio 0.50: J = 0.372504.
    -> Splitting in dimension 1 with ratio 0.50: J = 0.372287 and penalty = 0.380060.

    7. Iteration. Number of local linear models = 7. Checking for split of model 6 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.370983.
       Testing split in dimension 2 with ratio 0.50: J = 0.371039.
    -> Splitting in dimension 1 with ratio 0.50: J = 0.370983 and penalty = 0.379627.

    8. Iteration. Number of local linear models = 8. Checking for split of model 8 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.357113.
       Testing split in dimension 2 with ratio 0.50: J = 0.365156.
    -> Splitting in dimension 1 with ratio 0.50: J = 0.357113 and penalty = 0.366517.

    9. Iteration. Number of local linear models = 9. Checking for split of model 11 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.355982.
       Testing split in dimension 2 with ratio 0.50: J = 0.355506.
    -> Splitting in dimension 2 with ratio 0.50: J = 0.355506 and penalty = 0.365505.

    10. Iteration. Number of local linear models = 10. Checking for split of model 10 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.355108.
       Testing split in dimension 2 with ratio 0.50: J = 0.354057.
    -> Splitting in dimension 2 with ratio 0.50: J = 0.354057 and penalty = 0.364689.

    11. Iteration. Number of local linear models = 11. Checking for split of model 9 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.354009.
       Testing split in dimension 2 with ratio 0.50: J = 0.348044.
    -> Splitting in dimension 2 with ratio 0.50: J = 0.348044 and penalty = 0.359016.

    12. Iteration. Number of local linear models = 12. Checking for split of model 13 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.347516.
       Testing split in dimension 2 with ratio 0.50: J = 0.347311.
    -> Splitting in dimension 2 with ratio 0.50: J = 0.347311 and penalty = 0.359075.

    13. Iteration. Number of local linear models = 13. Checking for split of model 15 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.346897.
       Testing split in dimension 2 with ratio 0.50: J = 0.345686.
    -> Splitting in dimension 2 with ratio 0.50: J = 0.345686 and penalty = 0.358263.

    14. Iteration. Number of local linear models = 14. Checking for split of model 12 ...
       Testing split in dimension 1 with ratio 0.50: J = 0.345539.
       Testing split in dimension 2 with ratio 0.50: J = 0.344465.
    -> Splitting in dimension 2 with ratio 0.50: J = 0.344465 and penalty = 0.357589.

    Maximum number of local models reached.

    Final net has 15 local models and 90 parameters: J = 0.344465

    Net 15 with 15 LMs and 90 parameters is suggested as the model with the best complexity trade-off.
    Plot model for dimensions 1 and 2.
    Plot model for dimensions 1 and 2.

    ans =

      1×29 cell array

      Columns 1 through 13

        []    []    []    []    []    []    []    []    []    []    []    []    []

      Columns 14 through 19

        [1×1 Line]    []    [1×1 Line]    [1×1 Line]    [1×1 Line]    [1×1 Line]

      Columns 20 through 24

        [1×1 Line]    [1×1 Line]    [1×1 Line]    [1×1 Line]    [1×1 Line]

      Columns 25 through 29

        [1×1 Line]    [1×1 Line]    [1×1 Line]    [1×1 Line]    [1×1 Line]