最近因为需要对一个函数进行泰勒展开,因此使用了Matlab中的tayloy()函数,结果发现,展开的结果是未经过化简的,因此可读性比较低。以下为例:
>> syms x a
>> y=taylor(sqrt(x^2+a^2),x)
y =
(a^2)^(1/2) + (x^2*(a^2)^(1/2))/(2*a^2) - (x^4*(a^2)^(1/2))/(8*a^4)
可以看到,其中的指数并没有化简开来,即(a^2)^1/2没有化简为a。
下面给出两种化简方法:
- 使用simplify()函数可以进行化简,使用时需将'IgnoreAnalyticConstraints'参数设为true。如下:
>> simplify(y,'IgnoreAnalyticConstraints',true) ans = (8*a^4 + 4*a^2*x^2 - x^4)/(8*a^3)
'IgnoreAnalyticConstraints'参数的描述如下:
false Use strict simplification rules. simplify
always returns results equivalent to the initial expression.true Apply purely algebraic simplifications to an expression. simplify
can return simpler results for expressions for which it would return more
complicated results otherwise. SettingIgnoreAnalyticConstraints
totrue
can lead to results that are not equivalent to the initial expression.
由上可知,该参数为true时使用会忽略掉一些约束条件,比如例子中x,a的正负性,该方法获得的解析式一般与simplify()函数默认配置得到的结果不同,适 用于已知参数的约束条件时。一般不推荐该方法。
-
定义参数时指定约束条件
实际上之所以泰勒展开后的结果中的指数没有化简,是因为在定义时没有指定x,a的正负性,指定其为正数后在进行展开就可以得到指数简化后的展开式:
由上可见,结果已经是化简后的展开式。>> syms x a positive >> y=taylor(sqrt(x^2+a^2),x) y = - x^4/(8*a^3) + x^2/(2*a) + a