MATLAB Answers

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

1 view (last 30 days)
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.
which alpha
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???

Accepted Answer

Walter Roberson
Walter Roberson on 3 May 2016
Edited: Walter Roberson on 10 Sep 2019
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));
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!