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!