Rational-in-the-State Representation of 
Affine Nonlinear Systems 
by Toshiyuki Ohtsuka,  Sept. '99

Definition of Functions

Differentiation by a Vector

Dv[f_, x_] := 
      Module[{j, ans}, 
        ans = Table[
            (∂_(x[[j]]))f, {
              j, Length[x]}]; 
        If[Length[Dimensions[ans]] ≠ 1, ans = Transpose[ans]]; ans]

Picking Up Variables That Depend on x

VariablesFunc[f_, x_] := 
      Module[{i, j, n = Length[x], v = Variables[f], u, test, ans = {}}, 
        Do[u = Level[{v[[i]]}, 
              Depth[v[[i]]]]; 
          test = Table[
              MemberQ[u, x[[j]]], {j, 
                n}]; If[MemberQ[test, True] == True, 
            AppendTo[ans, v[[i]]]], {i, 
            Length[v]}]; ans]

Finding a Member That Equals x

MPosition[f_, x_] := 
      Module[{i, ans = {}}, 
        Do[If[f[[i]] == x, 
            AppendTo[ans, i]], {i, Length[f]}]; Return[ans]; ]

Picking Up Monomials That Depend on x

Monomials[f_, x_] := 
      Module[{i, j, k, nm, a1, a2, var, monoi, g = Expand[Flatten[f]], 
          temp = {}, ans = {}}, Off[Power::"argr"]; 
        Do[temp = {}; 
          g[[k]] = 
            g[[k]] + a1 + a2; 
          nm = Length[g[[k]]]; 
          Do[AppendTo[temp, 
                g[[k
                    ]][[i
                  ]]], {i, nm}] 
            Do[var = 
                VariablesFunc[temp[[i]], 
                  x]; If[var ≠ {}, monoi = 1; 
                Do[monoi = 
                    monoi var[[j
                          ]]^Exponent[
                          temp[[i]], 
                          var[[j]]], {j, 
                    Length[var]}]; ans = ans ⋃ {monoi}], {i, 
                Length[temp]}], {k, Length[g]}]; On[Power::"argr"]; ans]

Definition of State Equation and Output Equation
   dx/dt = f(x) + B(x)u
   y = h(x) + D(x)u

\(\(xv = Array[x, 4]; \ \  (*\ State\ Vector\ *) 
uv = Array[u, 1]; 
    \ \  (*\ Input\ Vector\ *) 

    fx = {x[3], x[4], 
        \(-\(\(2\ x[3]\ x[4]\)\/x[2]\)\) - 3\ Sin[x[1]]\ Cos[x[1]] - 
          \(2\ x[4]\)\/x[2], 
        x[2]\ x[3]\^2 + 2\ x[2]\ x[3] + 3\ x[2]\ Cos[x[1]]\^2}; 
    \  (*\ Vector\ f \((x)\)\ *) 
TrBx = {{0, 0, 0, \(-1\)}}; 
    \ \  (*\ Transpose\ of\ the\ Matrix\ B \((x)\)\ *) \ 
hx = {Log[x[2]]}; 
    \  (*\ Vector\ h \((x)\)\ *) 
TrDx = {{0}}; 
    \ \ \  (*\ Transpose\ of\ the\ Matrix\ D \((x)\)\ *) \)\)

Transformation to Rational Nonlinear System

\(dimx = Length[xv]; 
fBx = Transpose[Join[{fx}, TrBx]]; 

    hDx = Transpose[Join[{hx}, TrDx]]; 
afB = hDx; 
s1v = {}; 
fBs1 = {}; 
    
While[\((alpha = Complement[VariablesFunc[afB, xv], s1v])\) 
        ≠ {}, s1v = Join[s1v, alpha]; 
      If[Length[alpha] == 1, 
        afB = Dv[alpha[[1]], xv] . fBx; 
        fBs1 = Append[fBs1, afB], afB = Dv[alpha, xv] . fBx; 
        fBs1 = Join[fBs1, afB]]]; 
\(dimz = Length[s1v]; \)\)zv = Array[z, dimz]; fBz = 
      fBs1 /. Table[
          s1v[[i]] -> z[i], {i, 
            dimz}]; fBz = ExpandAll[fBz]; fz = First[Transpose[fBz]]; \(TrBz = Rest[Transpose[fBz]]; 

    hDz\  = \ hDx\  /. \ 
        Table[s1v[[i]] -> z[i], {i, 
            dimz}]; 
hz = First[Transpose[hDz]]; 

    TrDz = Rest[Transpose[hDz]]; 

Print["dim z =  ", dimz]; 

    Print["z = alpha(x) =  ", s1v]; 
Print["f(z) =  ", fz]; 

    \(Print["Tr[B(z)] =  ", TrBz]; \)\)Print["h(z) =  ", hz]; Print["Tr[D(z)] =  ", TrDz];