How to use 'solve' function in simulink , using matlab function block
4 views (last 30 days)
Show older comments
Nithin S
on 11 Feb 2016
Commented: Walter Roberson
on 12 Feb 2016
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' "
0 Comments
Accepted Answer
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
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);
More Answers (0)
See Also
Categories
Find more on String in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!