How to use 'solve' function in simulink , using matlab function block
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2016-2-11
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 个评论
Walter Roberson
2016-2-12
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);
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!