Embedded MATLAB function : Block Error

Mojo Jojo

I made a design in simulink to implement PID using embedded matlab function. My function is :

function [u,integral,previous_error]   = fcn(Kp,Td,Ti,error,previous_error1,integral1)

dt = 1;
Ki= Kp/Ti;
Kd=Kp*Td;


    integral     = integral1 + error*dt;     % integral term 
    derivative = (error-previous_error1)/dt; % derivative term
    u = Kp*error+Ki*integral+Kd*derivative; % action of control

    previous_error=error;
    %integral=integral;
end

This is how my model looks:(a part of the entire model) enter image description here

I am getting the following error :

Simulink cannot solve the algebraic loop containing 'pid_block1/MATLAB Function' at time 2.2250754336053813E-8 using the TrustRegion-based algorithm due to one of the following reasons: the model is ill-defined i.e., the system equations do not have a solution; or the nonlinear equation solver failed to converge due to numerical issues.
To rule out solver convergence as the cause of this error, either
a) switch to LineSearch-based algorithm using
set_param('pid_block1','AlgebraicLoopSolver','LineSearch')
b) reducing the ode45 solver RelTol parameter so that the solver takes smaller time steps.
If the error persists in spite of the above changes, then the model is likely ill-defined and requires modification.

Any idea, why am I getting it? Should i use global variables for integral and previous_error here? Thanks in advance.

PatchJob

Erm.. Unless there is a specific reason why you need it in this form, I would strongly recommend replacing your MATLAB Function block with Simulink blocks such as:

  1. Gain blocks for Kp, KI, and KD
  2. Sum blocks for all addition and subtraction
  3. Derivative block for derivative
  4. Integrator block for integration

etc....

I've found that Algebraic loop problems are really hard to get rid of and are usually just best to avoid. The method I suggest above can be used for most any controller type and has worked quite nicely for me in the past.

If the issue is neatness, you can always create your own "PID controller" subsystem or library part.

Let me know if you need some more detail or a diagram on how you might do this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Simulink: Matlab Embedded Function Code Error

From Dev

Simulink: Matlab Embedded Function Code Error

From Dev

Compiling error due to variable size Matrix in simulink (matlab function block)

From Dev

Structure as input for a Matlab function Block

From Dev

Error in MATLAB.Undefined function 'det' for input arguments of type 'embedded.fi'

From Dev

OOP Matlab inheritance issue with Matlab coder (Simulink MATLAB Function block)

From Dev

Nested function error in matlab

From Dev

Error in matlab function definition

From Dev

Geometric transformation using MATLAB Function block

From Dev

Editing the Code of a "MATLAB Function" Block in Simulink Programmatically

From Dev

How to use syms in Simulink Matlab function block

From Dev

block iterator, embedded ruby giving me a syntax error

From Dev

How to apply a running window to a simulink signal using embedded matlab function?

From Dev

Matlab: Error obtained with rectangle function

From Dev

MATLAB parfor error: function or variable?

From Dev

undefined function or variable error in MATLAB

From Dev

Error using impshowpair function in MATLAB

From Dev

undefined function or variable error in MATLAB

From Dev

Memory error with recursive function in MATLAB

From Dev

function '<-'/2 undefined Error in receive block Elixir

From Dev

function '<-'/2 undefined Error in receive block Elixir

From Dev

In Swift optional function in block raises error

From Dev

How can I create multiple inputs for a matlab function block?

From Dev

View class properties while debugging matlab function block in simulink

From Dev

Generating random numbers in Simulink with MATLAB function-block

From Dev

Plotting inside Matlab Function Block for real time signals in Simulink

From Dev

compiler for Matlab function block R2011b

From Dev

Generating random numbers in Simulink with MATLAB function-block

From Dev

Define Initial condition for matlab function blocks as an Initial condition in sfunction block

Related Related

  1. 1

    Simulink: Matlab Embedded Function Code Error

  2. 2

    Simulink: Matlab Embedded Function Code Error

  3. 3

    Compiling error due to variable size Matrix in simulink (matlab function block)

  4. 4

    Structure as input for a Matlab function Block

  5. 5

    Error in MATLAB.Undefined function 'det' for input arguments of type 'embedded.fi'

  6. 6

    OOP Matlab inheritance issue with Matlab coder (Simulink MATLAB Function block)

  7. 7

    Nested function error in matlab

  8. 8

    Error in matlab function definition

  9. 9

    Geometric transformation using MATLAB Function block

  10. 10

    Editing the Code of a "MATLAB Function" Block in Simulink Programmatically

  11. 11

    How to use syms in Simulink Matlab function block

  12. 12

    block iterator, embedded ruby giving me a syntax error

  13. 13

    How to apply a running window to a simulink signal using embedded matlab function?

  14. 14

    Matlab: Error obtained with rectangle function

  15. 15

    MATLAB parfor error: function or variable?

  16. 16

    undefined function or variable error in MATLAB

  17. 17

    Error using impshowpair function in MATLAB

  18. 18

    undefined function or variable error in MATLAB

  19. 19

    Memory error with recursive function in MATLAB

  20. 20

    function '<-'/2 undefined Error in receive block Elixir

  21. 21

    function '<-'/2 undefined Error in receive block Elixir

  22. 22

    In Swift optional function in block raises error

  23. 23

    How can I create multiple inputs for a matlab function block?

  24. 24

    View class properties while debugging matlab function block in simulink

  25. 25

    Generating random numbers in Simulink with MATLAB function-block

  26. 26

    Plotting inside Matlab Function Block for real time signals in Simulink

  27. 27

    compiler for Matlab function block R2011b

  28. 28

    Generating random numbers in Simulink with MATLAB function-block

  29. 29

    Define Initial condition for matlab function blocks as an Initial condition in sfunction block

HotTag

Archive