user1003131
user1003131

Reputation: 527

Indefinite Integral in R

I am looking to calculate the indefinite integral of an equation.

I have data from an accelerometer feed into R through a visual C program, and from there it was simple enough to come up with an equation to represent the acceleration curve. That is all well in good, however i need to calculate the impact velocity as well. From my understanding from the good ol' highschool days, the indefinite integral of my acceleration curve will yield the the equation for the velocity.

I know it is easy enough to perform numerical integration with the integrate() function, is there anything which is comparable for an indefinite integral?

Upvotes: 12

Views: 11362

Answers (3)

Yorgos
Yorgos

Reputation: 30455

library(Ryacas)
x <- Sym("x")
Integrate(sin(x), x)

gives

expression(-cos(x))

An alternative way:

yacas("Integrate(x)Sin(x)")

You can find the function reference here

Upvotes: 11

IRTFM
IRTFM

Reputation: 263362

If the NA's you mention are informative in the sense of indicating no acceleration input then they should be replace by zeros. Let's assume you have the data in acc.vec and the device recorded at a rate of rec_per_sec:

acc.vec[is.na(ac.vec)] <- 0
vel.vec <- cumsum(acc.vec)/recs_per_sec

I do not think constructing a best fit curve is going to improve your accuracy in this instance. To plot velocity versus time:

plot(1:length(acc.vec)/recs_per_sec, vel.vec, 
       xlab="Seconds", ylab="Integrated Acceleration = Velocity")

Upvotes: 1

Carl Witthoft
Carl Witthoft

Reputation: 21502

As Ben said, try the Ryacas package for calculating the antiderivative of a function. But you probably should ask yourself whether you really want to generate a continuous function which only approximates your data in the first place (fitting errors). I'd stick with numerical integration of your actual data. Keep in mind the uncertainty in each data point, of course.

Upvotes: 2

Related Questions