简化matlab中的符号表达式,只得到系数

问题描述

我正在寻找 Matlab 中泰勒级数的系数。我的做法是:

% Declare symbolic expression and function:
syms x;
f = exp(x);

% Calculate the taylor expansions in a concrete point:
T = taylor(f,x,0.5);

% And finally I simplify the expression:
coefs = simplify(T)

但是返回的表达式是:

是的,它的表达是简化的,但实际上我想要的是:

其中每一项都乘以他的系数。我怎么能这样simplify(f,0.5,10 之类的选项(其中 10 指的是简化步骤)在我的情况下不起作用。我也一直在看到这个问题,问题是一样的:

How get to simplify a symbolic and numeric mixed expression in Matlab

解决方法

根据您想要的数字数量以及您想要结果的确切格式,以下是一个示例:

>> c = double(coeffs(T))
c =
  Columns 1 through 4
   0.999966624859531   1.000395979357109   0.498051217190664   0.171741799031263
  Columns 5 through 6
   0.034348359806253   0.013739343922501
>> digits 15
>> x.^(0:numel(c)-1) * sym(c,'d').'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531

编辑

请注意,您也可以使用 vpa( ) 而不是先转换为 double:

>> c = coeffs(T)
c =
[ (2329*exp(1/2))/3840,(233*exp(1/2))/384,(29*exp(1/2))/96,(5*exp(1/2))/48,exp(1/2)/48,exp(1/2)/120]
>> x.^(0:numel(c)-1) * vpa(c).'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...