MATLAB Answers

0

Embedded MATLAB Function and Anonymous function

Asked by Giuseppe on 12 Nov 2014
Latest activity Commented on by Mike Hosea
on 14 Nov 2014
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 Comments

Sign in to comment.

5 Answers

Answer by Orion
on 12 Nov 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 Comments

Sign in to comment.


Answer by Giuseppe on 12 Nov 2014

Hi Orion, thanks for your time.
I tryed to implement your advice, but i don't understand very well what do you mean.. So, i would appreciate if you could spend just few minutes, i will try to explain better what i have to do.
This is the original matlab code:
----------------
%% Equilibrium Solver
f=@(x)solver(x,m_CO,K_ref,K_wgs,p_cell,m_H2,n_ing,m_CH4,m_H2O,m_CO2,Uf);
x0=[m_CH4*.1,m_CO2*0.1,m_H2O*.1];
[x] = newtonraphson(f, x0);
%x(1) --> mol of CH4 react
%x(2) --> mol of CO react
%x(3) --> mol of H2 react
------------------------------------------
where:
- solver is a vector containing the equations (N°3) that i need to solve (in form of f(x)=0). all the parameter in the parentesis are know.
- Newtonrapshon is a function that solve the non linear system, giving as output a vector x contained the solutions that i'm looking for;
Now, i need to create a block or something else in simulink (yes, i'm not a very simulink expert..) in order to complete this scheme:
is it possible to do that? if yes, how could i introduce this box in the simulink environment?
I would be very grateful since it is an important thing ..!

  0 Comments

Sign in to comment.


Answer by Orion
on 12 Nov 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 Comments

Sign in to comment.


Answer by Mike Hosea
on 13 Nov 2014
Edited by Mike Hosea
on 13 Nov 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 Comments

Sign in to comment.


Answer by Giuseppe on 13 Nov 2014

Hi Orion! Yes of course, i'm try to develop a dynamic Model forma solid oxide fuel cell :) In this afternoon i'll try to implement your advice, i hope they will solve the issue! I'm newer of simulink software, i need to improve my knowledge! Thanks for your time!
Hi Mike, thanks to you too! Tour solution seems to ne more complicate than the previosly one, but i'LL check and try to sue both!

  1 Comment

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".

Sign in to comment.