Error non-numeric argument to Mathematical Function

Asked

Viewed 656 times

2

Good afternoon, I have the following code in R

funcao<-function(delta) {delta}
    Intensity2AccumulationFactor<-function(delta,s,t){
    int<-function(s,t) {integrate(funcao,s,t)}
    p=int(s,t)
    b=c(p)
    a=exp(b)
    print(a)
}

When testing gives the following error:

Intensity2accumulationfactor(function,0,1) Error in Exp(b) : non-numeric argument to Mathematical Function

someone can help me?

The aim is to calculate 1/(-integral(delta)) amid s and t.

  • 1

    Which error? Edit the question and add the error to make it easier to get an answer. :)

  • When testing gives the following error > Intensity2accumulationfactor(function,0,1) Error in Exp(b) : non-numeric argument to Mathematical Function

  • Andreia, edit the question to add the error there. Do not post as comment. If possible, add also the line where the error occurs (note that I formatted your code to try to make it more readable, but I don’t know if it’s still there for you in rstudio - correct if it’s not, ok?).

1 answer

3

Some comments:

Your code indicates that a non-numeric value has been passed to exp() because you didn’t have the output you wanted from the function int(). You must use integrate(funcao,s,t)$value to read the value of integration, as integrate() returns an object integrate.

The line b=c(p) doesn’t do anything too much, c() does nothing if you receive only one argument.

Are you sure funcao<-function(delta) {delta} is that right? This function of yours does nothing, simply returns the parameter that was passed to it.

Although I don’t quite understand what you want with delta, you can simplify your code like this:

funcao<-function(delta) {delta}

Intensity2AccumulationFactor <- function(s,t){  
  exp(integrate(funcao,s,t)$value)    
}

Intensity2AccumulationFactor(0, 3)

The variable delta that you passed on to Intensity2AccumulationFactor was not used, and the result in this case is the exponential integration of s until t.

  • Thank you, with your help I’ve solved the problem :)

  • @Andreiasilva, if that answer answered you, you can accept it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.