How can I use a function handle in Simulink "matlab function" block?

9 vues (au cours des 30 derniers jours)
Muhammad Zeeshan Babar
Muhammad Zeeshan Babar le 11 Août 2015
Hello,
I am trying to simulate this first order function
*function [f,h] = Model_First_order(u,deltat)
f=@(x)[x(1)+deltat*(-x(2)*x(1)+u(1)); x(2)]; h=@(x)x(1);*
using the function handle in Simulink Matlab function block. But as I try to simulate it, it gives me an error that
Anonymous functions are not supported for code generation. Function 'Model_first_order' (#23.313.314), line 21, column 3: "@" Launch diagnostic report. Component: MATLAB Function | Category: Coder error
Please help me to solve this problem.

Réponses (1)

Sebastian Castro
Sebastian Castro le 11 Août 2015
Modifié(e) : Sebastian Castro le 11 Août 2015
If you won't be generating code from your Simulink model (i.e., you just want a desktop simulation), you could do a few things:
  1. Place your entire code in a regular MATLAB function .m file, and call it from Simulink using an Interpreted MATLAB Function block instead.
  2. Place just the unsupported piece (the generation of the function handle) in a separate function and call it as interpreted using coder.extrinsic.
... or, my favorite approach:
3. Given that you seem to be modeling some kind of first-order set of equations, it would probably be MUCH easier (and numerically accurate) to implement the equations in Simulink with Integrator blocks!
- Sebastian
  2 commentaires
Muhammad Zeeshan Babar
Muhammad Zeeshan Babar le 11 Août 2015
Thankyou Sebastian. But the theme of writing the model in this form (as I mentioned in my question) is that I am using these function handle in other MATLAB function as well. e.g
r=0.1; s=[0,1]; z = h(s) + r*randn; s=f(s)+r*randn;
you can see that I am evaluating h and f here on some values of s. It is running in a loop where I called the estimation algorithm. That's why I need to use this interpretation. I am trying to make multiple MATLAB function blocks in order to solve a parameter estimation problem. This is only a portion of my problem where I just need my model to simulate and give me f and h as output. It is working in .m file but not in Simulink.
Vignesh Ramakrishnan
Vignesh Ramakrishnan le 12 Juin 2023
Modifié(e) : Vignesh Ramakrishnan le 12 Juin 2023
@Sebastian Castro, quick follow-up on this question: Say I have solved a first order ODE in Simulink and called the simulink file say ode.slx, using:
y=sim("ode.slx");
I wish to make a function handle for this call, and hopefully make a phase portrait with a quiver plot and contour lines. Is there some way to pull that off?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Event Functions dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by