如何从R中的函数读入数学函数?

时间:2017-04-29 09:00:48

标签: r

我想要做的是在函数中读取用户的函数。

好的,首先我查看了R的文档,并确定readline适合读取一串文本,而扫描适合读取逻辑,整数,数字,复杂,字符,原始和列表。这似乎很像它应该能够做我想做的事情。但它无法在函数中读取。这是我的观点。

所以我认为我从一个函数开始:fx = x ^ 2 + 3 如果我将此函数相对于x从x = 1的下限整合到x = 5的上限,则此曲线下的面积为53 +(1/3)= ~53.33333333

这是你在R中集成函数的方法。

    > fx <- function(x){x^2 + 3}

    > integrate(fx, lower = 1, upper = 5)
    53.33333 with absolute error < 5.9e-13

    # To extract the value without the error, it's ...

    > integrate(fx, lower = 1, upper = 5)$value
     [1] 53.33333

我想使用具有以下公式的函数计算任何函数的平均值,以获得曲线形式微积分的平均值:

平均值= [1 /(b - a)] * a b ∫f(x)dx

基本上,这是&#34; a&#34;的曲线下面积。到&#34; b&#34;超出范围&#34; a&#34;到&#34; b&#34;与x相关的整合。所以我想创建一个函数,它可以计算用户指定范围内任何函数的平均值。

So the average value of the above function is 53.33333 / (5 - 1) = 13.33333

首先,我检查了我的&#34; fx&#34; is.logical,is.integer,是扫描功能可以接收的所有东西,而且它都不是。这一切都是假的。

这是我认为应该解决问题的代码,除非它无效。

   average_value_function <- function()
{
    print("I will compute the average value of curve over a user defined range")
    print("Give me a function")
    print("Example type: 5*x + 3")

    user_function = readline() # scan() doesn't work, either.  

    fx <- function(x)
    {
        user_function
    }

    print("Give me the lower bound")
    lower_bound <- scan(file = "", what = numeric())

    print("Give me the upper bound")
    upper_bound <- scan(file = "", what = numeric())

    area = integrate(fx, lower = lower_bound, upper = upper_bound)$value

    interval = upper_bound - lower_bound

    average_value = area / interval

    print(average_value)
}

当我尝试代码时,我的average_value_function跳过fx函数扫描语句并给出以下错误消息:

> average_value_function()
[1] "I will compute the average value of curve over a user defined range"
[1] "Give me a function"
[1] "Example type: 5*x + 3"
[1] "Give me the lower bound"
1: 1
2: 
Read 1 item
[1] "Give me the upper bound"
1: 5
2: 
Read 1 item

Error in integrate(fx, lower = lower_bound, upper = upper_bound) : 
  evaluation of function gave a result of wrong length
Called from: integrate(fx, lower = lower_bound, upper = upper_bound)
Browse[1]> 

如果通过将函数设置为x ^ 2 + 3并且不接受标准输入来减少此功能的机器学习潜力,此功能可以很好地工作。

average_value_function <- function()
    {
        print("I will compute the average value of curve over a user defined range")
        print("Give me a function")
        print("Example type: 5*x + 3")

        fx <- function(x)
        {
            x^2 + 3 # main change 
        }

        print("Give me the lower bound")
        lower_bound <- scan(file = "", what = numeric())

        print("Give me the upper bound")
        upper_bound <- scan(file = "", what = numeric())

        area = integrate(fx, lower = lower_bound, upper = upper_bound)$value

        interval = upper_bound - lower_bound

        average_value = area / interval

        print(average_value)
    }

因此,它正在降低机器学习潜力。

> average_value_function()
[1] "I will compute the average value of curve over a user defined range"
[1] "Give me a function"
[1] "Example type: 5*x + 3"
[1] "Give me the lower bound"
1: 1
2: 
Read 1 item
[1] "Give me the upper bound"
1: 5
2: 
Read 1 item
[1] 13.33333

TL; DR

基本上,问题是扫描和读取线语句不能在函数中被接受,即使它被扫描到变量中然后直接传入或传递到函数中,问题还在于扫描和读取线与函数结构类型不兼容。

我希望我的代码能够工作,以便用户可以输入任何函数和任何集成边界,并从中获取平均值,以便用户可以输入字面上的任何函数:x ^ 2 + 3,log(x) ,2 * x + 5,1 / x,x ^(8)+ 3,并获得积分函数以处理适当的边界和用户定义的函数以收集适当的平均值。

0 个答案:

没有答案