MATLAB: How to create a function with symbolic variable tools

linearisationsymbolic linearisation matlab-function

Hello Please How can I create a function with symbolic variabls, I linearised my nonlinear system using symbolic tools in matlab and I need to extract the ABCD matrices in another script to obtain a transfer function because som of my parameters change and I want the transfer function to recognise the changes. I already did the linearization but I need it as a function rather than a script.
Thanks

Best Answer

  • Regular functions created in .m files, and classes created in .m files, can accept symbolic expressions and symbolic variables and process them however they want, with whatever control logic they want.
    Anonymous functions can also accept symbolic expressions and symbolic variables, but anonymous functions do not have much ability to express control logic. Some control logic can be expressed by using the symbolic operator piecewise() . Note that MATLAB is able to reason about nested piecewise.
    Symbolic functions can be created with symfun(), or equivalently by using the syntax
    NAME(list_of_variables) = expression
    However, this is also quite restricted in control logic, and again piecewise() may help.
    Note that in MATLAB, every symbolic variable you used is assumed to be scalar, so
    syms A B
    isAlways(A*B == B*A)
    would be true. For scalar A and B, commutative operations apply and this is true. In numeric operations if you had numeric arrays A and B then A*B and B*A give different outputs because * is algebraic matrix multiplication, so unless you know that A and B are scalar, you cannot rewrite A*B as B*A ... but the symbolic engine does because it defines all symbolic variables as being scalar. If you do
    syms A B
    F(A,B) = A*B
    F([1 2;3 4], [5 6;7 8])
    then because it knows that values are to be treated like extended scalars, it will do element-by-element multiplication, not algebraic matrix multiplication.
    This aspect of the Symbolic Toolbox is difficult to escape from, and it is probably going to mess up the mathematics of linearization.
    In the Symbolic Toolbox, you can create fixed-size arrays of symbols, such as
    syms A [2 2]
    syms B [2 2]
    and A*B would be algebraic matrix multiplication, giving
    [ A1_1*B1_1 + A1_2*B2_1, A1_1*B1_2 + A1_2*B2_2]
    [ A2_1*B1_1 + A2_2*B2_1, A2_1*B1_2 + A2_2*B2_2]
    but this only works for fixed size arrays. You cannot create a Symbolic Function that accepts arrays A, B of previously-unknown sizes and do A*B and have it come out as matrix multiplication -- you can only configure the function for one particular size of array.
    There are some other things that work strangely in Symbolic Functions. For example,
    F(A) = mod(3*A+7, 5)
    will give back 3*A+2 and F(2) would give 8 instead of the expected 3. Telling MATLAB to hold off on the evaluation of a symbolic expression for later until an actual argument is passed in, is fairly tricky (and if you manage to do it, you cannot get MATLAB to display the expression properly without breaking the symbolic engine.)
    For these reasons, it is best not to create symbolic functions that try to do anything complicated: create real functions instead that happen to accept symbolic arguments as required.