How to use 'solve' function in simulink , using matlab function block
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nithin S
el 11 de Feb. de 2016
Comentada: Walter Roberson
el 12 de Feb. de 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 comentarios
Respuesta aceptada
Walter Roberson
el 11 de Feb. de 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 comentarios
Nithin S
el 12 de Feb. de 2016
Editada: Walter Roberson
el 12 de Feb. de 2016
Walter Roberson
el 12 de Feb. de 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);
Más respuestas (0)
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!