Search code examples
matlabsimulink

Implementing a complex function in Simulink


In Matlab's command window I obtained several equations which look like:

Tc1 = (- 2*J2*cos(t3)*sin(t1)*sin(t3)*n^2 + 2*J2*w3*sin(t3)*n)/cos(t2) - d2 + cos(t2)*(J2*cos(t3)*sin(t1)*sin(t3)*n^2 + J2*v3*sin(t1)) + J1*w1*w3 + J2*w1*w3 - J3*w1*w3 + J2*v2*cos(t1) + J2*n^2*cos(t1)*tan(t2) - J2*w2^2*cos(t1)*tan(t2) + 2*J2*w3^2*cos(t1)*tan(t2) + J2*w2^2*cos(t1)^3*tan(t2) - J2*w3^2*cos(t1)^3*tan(t2) - 2*J2*n*w2*cos(t3)*tan(t2) + 2*J2*w2*w3*sin(t1)*tan(t2) - J2*n^2*cos(t1)*cos(t3)^2*tan(t2) + 2*J2*n*w2*cos(t1)^2*cos(t3)*tan(t2) - 2*J2*w2*w3*cos(t1)^2*sin(t1)*tan(t2) - 2*J2*n*w3*cos(t1)*cos(t3)*sin(t1)*tan(t2)         

In Simulink I use From and Goto blocks for all the parameters (Tc1, J1, J2, J3, t1, t2, t3 and so on).

Now I am wondering what the easiest way is to implement these long formulas in Simulink for a simulation. Normally I would just rebuild the equations using all kind of blocks in Simulink, but the equations I ended up with this time are much longer. Any ideas?


Solution

    1. the first way is using Fcn block from user-defined function and just write your function in one line and use inputs like u(1),u(2),....

    The Fcn block applies the specified mathematical expression to its input. The expression can include one or more of these components:

    • u — The input to the block. If u is a vector, u(i) represents the i-th element of the vector; u(1) or u alone represents the first element.
    • Numeric constants.
    • Arithmetic operators (+ - * / ^).
    • Relational operators (== != > < >= <=) — The expression returns 1 if the relation is true; otherwise, it returns 0.
    • Logical operators (&& || !) — The expression returns 1 if the relation is true; otherwise, it returns 0.
    • Parentheses.
    • Mathematical functions — abs, acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, hypot, ln, log, log10, pow, power, rem, sgn, sin, sinh, sqrt, tan, and tanh.

    check this link


    1. The second way is using Matlab Function from user-defined function and write all the functions you have in an embedded m-file. and then connect all inputs and outputs to it.

    check this link