【问题标题】:Calculate Taylor series using rSymPy使用 rSymPy 计算泰勒级数
【发布时间】:2014-08-19 13:55:50
【问题描述】:

我一直在尝试将 R 接口 rSymPy 连接到 CAS SymPy,它运行良好。但是,我找不到使用某些更复杂功能的正确语法,例如查找泰勒级数。 例如,我尝试了以下方法:

library(rSymPy)
sympy("var('p')")
#
##### Cannot make this work ???
#
sympy("from sympy.mpmath import *")
xt <- sympy("p=taylor(exp, 0, 10)")

但它会抛出错误:

Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
  SyntaxError: ("no viable alternative at input '='", ('<string>', 1, 8, '__Rsympy= from sympy.mpmath import *\n'))

任何帮助表示赞赏。

【问题讨论】:

    标签: r cas


    【解决方案1】:

    似乎没有明确的泰勒级数可用,但级数函数可用。以下代码有效:

    library(rSymPy)
    sympy("var('p')")
    sympy("var('x')") # or sympy("x = Symbol('x', real=True)")
    #
    xt <- sympy("p=series(exp(x), x, 0, 10)") # expand about 0 to 10th order
    

    给出了答案:

    [1] "1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + x**6/720 + x**7/5040 + x**8/40320 + x**9/362880 + O(x**10)"
    

    我们可以通过修改代码来检查这个答案:

    library(rSymPy)
    sympy("var('p')")
    sympy("var('x')") # or sympy("x = Symbol('x', real=True)")
    #
    xt <- sympy("p=series(exp(x), x, 0, 10)") # expand about 0 to 10th order
    # Remove order information
    xt0 <- sympy("p.removeO()")
    # Test results
    x <- 1/3
    T1 <- eval(parse(text=xt0)) # Evaluate the result, xt0
    T2 <- exp(x)                # The correct value
    print(T1-T2)                # Print the error
    

    最后,级数展开的错误是:

    [1] -4.811929e-12
    

    我希望这对希望使用 R 包 rSymPy 的其他人有所帮助

    【讨论】:

      猜你喜欢
      • 2015-05-15
      • 2017-03-20
      • 2017-09-27
      • 2020-03-01
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 2015-11-23
      • 2014-03-27
      相关资源
      最近更新 更多