问题
I am trying to run the function mtaylor
from the MuPAD engine in MatLab, which provides a multivariate Taylor expansion of a function. However, it keeps telling me I am trying to expand around an invalid point. Here is a minimal working example of what I tried:
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), [x, y], 4)
Error message:
vError using mupadengine/feval (line 157)
MuPAD error: Error: Invalid expansion point. [mtaylor]
Why doesn't this work?
回答1:
The reason this works with MuPAD's mtaylor
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), x, 4) % [x] is fine too
and this doesn't
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), [x, y], 4)
is that the [x, y]
argument is seen as a single symbolic vector argument/variable rather than a list of variables for the expansion. Your expression, exp(x^2 - y)
, is not in terms of vector variables, but simple scalars, x
and y
.
The workaround is to pass the list as a string:
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), '[x, y]', 4)
or
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), ['[' char(x) ',' char(y) ']'], 4)
or use evalin to write the MuPAD command a single string as suggested by @Daniel in the comments:
syms x y;
evalin(symengine,'mtaylor(exp(x^2 - y), [x, y], 4)')
Arrays and Matrices vs. Lists in MuPAD
For further clarification, arrays of symbolic variables in Matlab correspond to the MuPAD array type, which can be created via feval(symengine,'array','1..1','1..2','[x,y]')
. More specifically, they are of type Dom::Matrix(), which can be created via V=feval(symengine,'Dom::Matrix()','[x,y]')
or just syms x y;
V=[x,y]
.
The mtaylor
function requires a list input, which can be created via L=evalin(symengine,'[x,y]')
. Thus
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), V, 4)
will produce the same error as in your question, but
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), L, 4)
will work correctly. Unfortunately, L
and V
appear identical from within Matlab, but you can use MuPAD's domtype function to differentiate them:
feval(symengine,'domtype',V)
feval(symengine,'domtype',L)
which returns Dom::Matrix()
and DOM_LIST
.
来源:https://stackoverflow.com/questions/28237062/mtaylor-mupad-matlab