How to use 'solve' function in simulink , using matlab function block

4 views (last 30 days)
I am trying to use "solve' function inside simulink making use of the Matlab function block. The function is as given below
function y = fcn(u)
%#codegen
coder.extrinsic('syms');
P=u;
V=2*(P*0.0031-pi);
syms a value ;
value = V;
sol=solve((sin(2*a)-(2*a))== value);
y=0;
y = sol;
it gives an error.. " undefined function or variable 'a' "

Accepted Answer

Walter Roberson
Walter Roberson on 11 Feb 2016
You could change
syms a value ;
to
a = sym('a');
and you could change
sol=solve((sin(2*a)-(2*a))== value);
to
sol = double( solve( ((sin(2*a)-(2*a))) - (value), a) );
However, you have a more fundamental problem. Embedded MATLAB Function blocks are for code that is to be optimized by compiling, and it is not allowed to compile anything in the Symbolic Toolbox.
You are going to need to switch to fzero() or the like. The solution is positive for P < pi/*0.0031 and negative for larger P, which might help you decide on the appropriate bounds to use.
  2 Comments
Nithin S
Nithin S on 12 Feb 2016
Edited: Walter Roberson on 12 Feb 2016
Thanks for the reply, as per your suggestion I am trying to use fzero() now, I have modified the function with fzero(), the value of 'u' will be in the range of [100 1000], for this I manually calculated the value of 'a' as x0=[2.973 0.40089 ] and set is as interval.
function y=fcn(u)
V=2*(u*0.0031-pi) ;
myfun = @(a)(sin(2*a)-(2*a) - V);
x0=[2.3322 0.40089 ] ;
y=0;
y = fzero(myfun,x0)
during execution I am getting an error
This kind of expression is not supported.
Function 'MATLAB Function' (#33.381.405), line 19, column 9:
"@(a)(sin(2*a)-(2*a) - V)"
when I run the script in workspace it is working. what is that I am doing wrong ?
Walter Roberson
Walter Roberson on 12 Feb 2016
Anonymous functions are not supported, I think. You would need to create a different true function that did the calculation for you. According to the documentation you would use a handle to that function. I think you should be able to put it in the same file.
I am not sure if nested functions are supported. To share parameters you might possibly need to resort to global. Perhaps
function y = fcn(u)
V = 2*(u*0.0031-pi);
global myfun_V
myfun_V = V;
y = 0;
a0=[0.40089 2.3322] ;
y = fzero(@myfun, a0);
function residue = myfun(a)
global myfun_V
residue = 0;
residue = (sin(2*a)-(2*a) - myfun_V);

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!