简化matlab中的符号表达式并仅获得系数
Simplify symbolic expression in matlab and get only the coeeficents
我正在寻找 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)
但是返回的表达式是:
![\frac{\sqrt{\mathrm{e}}\,\left(32\,x^5+80\,x^4+400\,x^3+1160\,x^2+2330\,x+2329\right)}{3840}](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Csqrt%7B%5Cmathrm%7Be%7D%7D%5C,%5Cleft(32%5C,x%5E5+80%5C,x%5E4+400%5C,x%5E3+1160%5C,x%5E2+2330%5C,x+2329%5Cright)%7D%7B3840%7D)
是的,表达是简化了,其实我想要的是:
![0.999966624 + 1.00039482x + 0.4980512172x^2+0.171741799x^3+0.03434835981x^4+0.01373934392x^5](https://latex.codecogs.com/gif.latex?0.999966624&space;+&space;1.00039482x&space;+&space;0.4980512172x%5E2+0.171741799x%5E3+0.03434835981x%5E4+0.01373934392x%5E5)
其中每一项乘以他的系数。我怎么能这样simplify(f, x, 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
我正在寻找 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, x, 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