Search code examples
matlabsymbolic-mathstate-space

Using equationsToMatrix with functions (dependent variables)


I'm trying to find state space model of inverted (one piece) pendulum using the equationsToMatrix function. I'm using the following code:

%Declaration of Variables
syms x(t) t M m ddx(t) l th(t) ddth(t) dth(t) b1 b2 dx(t) F(t) I

%Nonlinear Equations
eqn1=eq((I+m*l^2)*ddth+m*l*cos(th)*ddx-m*g*l*sin(th)+b2*dth,0)
eqn2=eq((M+m)*ddx+m*l*cos(th)*ddth-m*l*sin(th)*(dth)^2+b1*dx,F)

%Linear Equations
eqn1L=subs (eqn1,[cos(th),sin(th(t)),dth(t)^2],[1,th(t),0])
eqn2L=subs (eqn2,[cos(th),sin(th(t)),dth(t)^2],[1,th(t),0])

%Finding State Space Model
[A,B]=equationsToMatrix([eqn2L,eqn1L],[x(t),dx(t),th(t),dth(t)])
C=[1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1];
D=[0;0;0;0];

sys = ss(A,B,C,D)

MATLAB throws the following error:

Error using sym.getEqnsVars>checkVariables (line 92)
The second argument must be a vector of symbolic variables.

Error in sym.getEqnsVars (line 54)
checkVariables(vars);

Error in sym/equationsToMatrix (line 55)
[eqns,vars] = sym.getEqnsVars(argv{:});

Error in Linearization_Test (line 10)
[A,B]=equationsToMatrix([eqn2L,eqn1L],[x(t),dx(t),th(t),dth(t)])

How to resolve this error?


Solution

  • You should substitute the variables with ones that has no time dependency:

    syms x_ dx_ th_ dth_
    X = [x(t),dx(t),th(t),dth(t)];
    X_ = [x_,dx_,th_,dth_];
    [A,B]=equationsToMatrix(subs([eqn2L,eqn1L], X, X_),X_)