Embedded MATLAB Function and Anonymous function
31 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi!
I have an issue with simulink and I would like your own opinion about this..
i'm using a matlab script in order to solve a non linear system with newton raphson method. this script accept an anonymous function ('f') and solve the equation system. if i run it as a normal script from matlab, it works very well!
But, i need to introduce this scrip as a black box in Simulink: i would like to give as inputs all the function's parameters, so to obtain the three solution of system.
Now, i'm trying with Embedded MATLAB Function but Matlab report: Embedded MATLAB Function does not support anonymous function!
Are there other solution in order fix the problem? how can I get around the issue? i need to work with anonymous function..!
Thanks in advance, Giuseppe
Now, i'm trying with Embedded MATLAB Function but Matlab report: Embedded MATLAB Function does not support anonymous function!
Are there other solution in order fix the problem? how can I get around the issue? i need to work with anonymous function..!
Thanks in advance, Giuseppe
0 comentarios
Respuestas (5)
Orion
el 12 de Nov. de 2014
Hi,
an Embedded Matlab Function is not a Mfile, it's a simulink block, in which you can use classic matlab functions.
if you want/need to use more complicated matlab language, such as anonymous function,..., then use an interpreted matlab function ("matlab function" in older version). this way you can use your code.
try this, create a Mfile test
function [y] = test(a)
sqr = @(x) x.^2;
y = sqr(a);
in the interpreted matlab function, put test as argument, then simulate it with a constant block as input and see the result.
0 comentarios
Mike Hosea
el 13 de Nov. de 2014
Editada: Mike Hosea
el 13 de Nov. de 2014
If you want to call an interpreted MATLAB function from a MATLAB function block, do not try to create the function inside the block. Instead, write an interpreted MATLAB function that creates the anonymous function in MATLAB. So your MATLAB function block looks like
function y = fcn(a,b,c)
x0 = a + b + c; % or whatever
y = 0; % Preallocate the result.
y = feval('mywrapper',a,b,c,x0);
Where mywrapper.m is on the MATLAB path somewhere. Inside mywrapper.m you would do whatever you need to:
function y = mywrapper(a,b,c,x0)
f = @(x)a*x.^2 + b*x + c; % or whatever
y = newtonraphson(f, x0);
Note that this function mywrapper will execute in MATLAB, so you can do anything you want that MATLAB can do. The preallocation of the output simply sets a constraint on what has to be returned. In this case, I have constrained the function to return a scalar double, but it can be another type or another size.
Note that FZERO is supported, BTW. Another approach that handles parameters without making the "extrinsic" call is a little more involved. For this approach you put everything inside the MATLAB function block and use "persistent" to hold the parameters. That looks like this:
function y = fcn(a,b,c)
x0 = a + b + c; % or whatever
f('set',a,b,c); % set the parameters.
y = fzero(@f,x0);
function y = f(x,a_in,b_in,c_in)
persistent a b c
if isempty(a)
a = 0;
b = 0;
c = 0;
end
if ischar(x) && strcmp(x,'set')
a = a_in;
b = b_in;
c = c_in;
else
y = a*x.^2 + b*x + c;
end
There's nothing special about the word 'set' here. One just has to choose some syntax to key off of. If the type of a is always the same as the type of x, you could key of nargin if you wanted.
0 comentarios
Orion
el 12 de Nov. de 2014
Giuseppe,
I don't know your code, but I guess you're gonna make something like :
In this screenshot, you have all the component to make your model work.
The important thing is that the MatlabFcn block has only one input and one output, so you just need to mux/demux your signals.
Also, here I used Constant blocks as Inputs. Of course, it works with varying signals, but you need to be sure that your problem has an existing solution for all the inputs you could send in.
Adapt this to your problem and good luck with your high temperature fuel cell system :)
0 comentarios
Giuseppe
el 13 de Nov. de 2014
1 comentario
Mike Hosea
el 14 de Nov. de 2014
My answer was about how to make the "Embedded MATLAB Function" block do what you wanted. Orion points out that the "MATLAB Fcn" block does what you need. The main reason the Embedded MATLAB Function block exists is to make it possible to generate stand-a-lone code that will run without MATLAB or Simulink using Real-time Workshop. Since you don't care about that, you can choose whichever block gets the results you want more easily.
Please note that those are all old names. In current versions of Simulink what was the "Embedded MATLAB" block is now called the "MATLAB Function" block, and what was the "MATLAB Fcn" block is now called the "Interpreted MATLAB Function" block. I think Real-time Workshop is now called "Simulink Coder".
Ver también
Categorías
Más información sobre Naming Conventions en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!