【问题标题】:Trouble with taylor series in python (sympy)python中泰勒系列的问题(sympy)
【发布时间】:2017-04-05 15:18:07
【问题描述】:

我正在尝试获取此函数的泰勒级数

这应该与此类似,考虑到 d 居中或围绕 rs

但是,当我尝试以@Saullo 为例来解决我的问题时,

如您所见,结果是从泰勒系列中删除“d”,这不应该是我的目标。

事实上,关于该函数的另一个附加信息是:

我做错了什么??有没有办法在不删除“d”的情况下得到我的结果??

感谢任何帮助

代码

感谢您的回复和对帮助我的兴趣,这是我的代码直到现在@asmeurer

import sympy as sy
#import numpy as np

from sympy import init_printing
init_printing(use_latex=True)

# Define the variable and the function to approximate
z, d, r_s, N_e, r_t, r_s, r_b = sy.symbols('z  d r_s N_e r_t r_s r_b')

# Define W_model
def W_model(r_t=r_t, r_b=r_b, r_s=r_s, z=z):

    s_model = sy.sqrt(pow(r_t, 2) - pow(r_s*sy.sin(z), 2)) - sy.sqrt(pow(r_b, 2) - pow(r_s*sy.sin(z), 2))
    d_model = r_t - r_b

    STEC_approx = N_e * s_model
    VTEC_approx = N_e * d_model

    return STEC_approx/VTEC_approx

f = W_model() 
# printing Standard model
f

# Some considerations for modify Standard model
rb = r_s - d/2
rt = r_s + d/2

f = W_model(r_b=rb, r_t=rt, r_s=r_s, z=z) 
# printing My model
f

## Finding taylor series aproximmation for W_model
num_of_terms = 2
# creates a generator
taylor_series = f.series(x=d, n=None)

# takes the number of terms desired for your generator
taylor_series = sum([next(taylor_series) for i in range(num_of_terms)])
taylor_series

【问题讨论】:

  • 请在此处粘贴您的代码。如果我可以复制和粘贴它,测试起来会容易得多。
  • 感谢您的回复和对帮助我的兴趣,直到今天我已经用我的代码更新了我的问题。 @asmeurer

标签: python sympy taylor-series


【解决方案1】:

问题是您的表达式足够复杂以至于series 不知道奇数阶项为零(您会得到复杂的表达式,但如果您对它们调用simplify(),它们会变为0) .考虑

In [62]: s = f.series(d, n=None)

In [63]: a1 = next(s)

In [64]: a2 = next(s)

In [65]: simplify(a0)
Out[65]:
       rₛ
────────────────
   _____________
  ╱   2    2
╲╱  rₛ ⋅cos (z)

In [66]: simplify(a1)
Out[66]: 0

如果您打印a0a1,它们都是复杂的表达式。实际上,您需要获得几个术语(最多 a3),然后系列才能获得不取消为 0 的术语:

In [73]: simplify(a3)
Out[73]:
      _____________
 2   ╱   2    2        2
d ⋅╲╱  rₛ ⋅cos (z) ⋅sin (z)
───────────────────────────
           3    6
       8⋅rₛ ⋅cos (z)

如果你使用f.series(d, n=3),它会将扩展扩展到d**2n=3 表示+ O(d**3))。您可以使用

简化表达式
collect(expr.removeO(), d, simplify)

在内部,当您为系列提供显式 n 时,它使用逐项生成器来获取所需的尽可能多的项,以提供适当的 O(d**n) 扩展。如果您自己使用生成器 (n=None),则需要手动执行此操作。

一般来说,迭代器不能保证给你下一个订单项。如果您想保证您拥有所有条款,则需要提供明确的nseries 返回的 O 项总是正确的(意味着所有低阶项都是完整的)。

【讨论】:

  • 很好的解释,现在更清楚了,因为我无法识别的表达方式。谢谢你的帮助。 @asmeurer
猜你喜欢
  • 1970-01-01
  • 2021-02-08
  • 2019-05-27
  • 2017-04-02
  • 2022-06-18
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
相关资源
最近更新 更多