R中的积分计算器

问题描述

我知道如何使用积分计算器获得数值,但不知道如何获得代数方程。

例如:

integrand <- function(x) {1/((x+1)*sqrt(x))}
integrate(integrand,lower = 0,upper = Inf)

使用此代码,我可以得到绝对错误为< 2.7e-05的3.141593

但是,如果我想使用此代码怎么办?

integrand <- function(x) {2*x}
integrate(integrand,lower = -Inf,upper = Inf)

获得x^2

显然,它不起作用。

如果有人可以帮助您,我将不胜感激。谢谢!

解决方法

R不执行符号数学运算,但有R程序包可以执行。我将使用CRAN软件包RyacasrSympy给出(非常)简短的示例。

1。打包Ryacas

library(Ryacas)

f <- ysym("2*x")
integrate(f,"x")
#y: x^2

2。打包rSymPy

对于软件包rSymPy,您需要首先定义符号"x"。这是通过功能Var完成的。
注意:在我的测试中,第一次调用Varsympy中的任何一个时,都会发出一系列警告。我打断了R,第二次一切都顺利。

library(rSymPy)

x <- Var("x")
sympy("integrate(2*x)")
#[1] "x**2"

相关问答

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