Bug? sym alpha takes precedence over matlab's alpha.m at the command line, but not within a matlab function.

1 次查看(过去 30 天)
When I run the following code, there is no problem:
syms xDot(t) t alpha
dF_dxDot = @(t) -exp(-t)*alpha*xDot(t)^(alpha-1);
Euler = matlabFunction(simplify(diff(dF_dxDot(t),t)),'vars',[t,alpha]);
But when I run the same code within a function.
function nothing
syms xDot(t) t alpha
dF_dxDot = @(t) -exp(-t)*alpha*xDot(t)^(alpha-1);
Euler = matlabFunction(simplify(diff(dF_dxDot(t),t)),'vars',[t,alpha]);
it throws an error,
Error using alpha
Too many output arguments.
since
which alpha
returns
/usr/local/MATLAB/R2016a/toolbox/matlab/graph3d/alpha.m
what appears to be happening is that my sym command takes precedence over matlab's command, provided I run the code within a regular script, but the precedence is reversed when I run it within a function. Surely this has to be a bug???

采纳的回答

Walter Roberson
Walter Roberson 2016-5-3
编辑:Walter Roberson 2019-9-10
This effect is documented... somewhere or other, I do not recall where. It occurs only in quite recent versions of MATLAB. It occurs because the JIT (Just In Time) compiler now gives priority to function calls to known routines, whereas before it gave priority to run-time assignments that are hidden with assignin().
syms is not a command: syms is a function. In its basic form, it is equivalent to
function syms(varargin)
for K = 1 : nargin
symname = varargin{K};
assignin('caller', symname, sym(symname));
end
end
It "poofs" variables into existence -- and MATLAB now gives priority to static analysis over dynamic analysis.
To avoid this problem, use
alpha = sym('alpha');
or any assignment to alpha before the syms call. For example,
alpha = [];
syms alpha
will do fine.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by