如何计算正弦曲线两端的面积 [英] How to calculate the area under each end of a sine curve

查看:369
本文介绍了如何计算正弦曲线两端的面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出此数据集:

y<-c(-13,16,35,40,28,36,43,33,40,33,22,-5,-27,-31,-29,-25,-26,-31,-26,-24,-25,-29,-23,4)
t<-1:24

我的目标是计算两个面积.第一个区域将仅对来自零线上方的曲线第一部分的数据进行积分.第二个区域将对位于零线下方的曲线第二部分的数据进行积分.

My goal is to calculate two areas. The first area would integrate only data from the first part of the curve found above the Zero line. The second area would integrate data from the second part of the curve found below the zero line.

首先,我想对这个数据拟合一个正弦波.使用这个出色的答案:

First I would like to fit a sine wave to this data. Using this excellent answer:

https://stats.stackexchange.com/questions/60994/fit -正弦项到数据

我能够拟合正弦波(我将使用具有二次谐波的周期,看起来更合适)

I was able to fit a sine wave (I will be using the periodic with second harmonic which looks to have a better fit)

ssp <- spectrum(y)  
per <- 1/ssp$freq[ssp$spec==max(ssp$spec)]
reslm <- lm(y ~ sin(2*pi/per*t)+cos(2*pi/per*t))
summary(reslm)

rg <- diff(range(y))
plot(y~t,ylim=c(min(y)-0.1*rg,max(y)+0.1*rg))
lines(fitted(reslm)~t,col=4,lty=2)   # dashed blue line is sin fit

# including 2nd harmonic really improves the fit
reslm2 <- lm(y ~ sin(2*pi/per*t)+cos(2*pi/per*t)+sin(4*pi/per*t)+cos(4*pi/per*t))
summary(reslm2)
lines(fitted(reslm2)~t,col=3)    # solid green line is periodic with second harmonic
abline(h=0,lty=2)

接下来,我想计算曲线下仅正的面积,以及曲线下仅负的面积.我很幸运使用Bolstad2和Mess软件包中的AUC函数查看类似的答案.但是我的数据点并没有整齐地落在零线上,而且我不知道如何将正弦函数分解为仅在零线以上和零线以下的区域.

Next I would like to calculate the area under the curve that is only positive, as well as the area under the curve that is exclusively negative. I've had luck looking at similar answers using the AUC functions in the Bolstad2 and Mess packages. But my data points do not fall neatly on zero line, and I do not know how to break up the sine function into areas only above the Zero line and only below the Zero line.

推荐答案

第一件事.为了获得精确的计算,您将需要使用二次谐波傅立叶的精确函数.其次,谐波函数的优点在于它们具有重复性.因此,如果要查找函数到达0的位置,只需将间隔扩展到,这样就可以确保找到两个以上的根.

First things first. To get an exact calculation, you will need to work with the exact function of the 2nd harmonic fourier. Secondly, the beauty of harmonics functions is that they are repetitive. So if you want to find where your function reaches 0, you merely need to expand your interval to so you can be sure to find more than 2 roots.

首先,我们从回归模型中获得确切的函数

First we get the exact function from the regression model

fourierfnct <- function(t){
  fnct <- reslm2$coeff[1]+
    reslm2$coeff[2]*sin(2*pi/per*t)+
    reslm2$coeff[3]*cos(2*pi/per*t)+
    reslm2$coeff[4]*sin(4*pi/per*t)+
    reslm2$coeff[5]*cos(4*pi/per*t)
  return(fnct)
}

其次,您可以编写一个可以找到根的函数(该函数为0). R提供了uniroot函数,可用于在循环中查找多个根.

secondly,you can write a function which can find the roots (where the function is 0). R provides a uniroot function which you can use to find multiple roots in a loop.

manyroots <- function(f,inter,period){
  roots <- array(NA, inter)
  for(i in 1:(length(inter)-1)){
    roots[i] <- tryCatch({
      return_value <- uniroot(f,c(inter[i],inter[i+1]))$root
    }, error = function(err) {
      return_value <- -1
    })
  }
  retroots <- roots[-which(roots==-1)]
  return(retroots)
}

然后,您只需计算根,然后使用它们跨这些边界集成函数.

then you simply calculate the roots, and use them to integrate the function across those boundaries.

roots <- manyroots(fourierfnct,seq(0,25),per)
integrate(fourierfnct, roots[1],roots[2])
#300.6378 with absolute error < 3.3e-12
integrate(fourierfnct, roots[2],roots[3])
#-284.6378 with absolute error < 3.2e-12

这篇关于如何计算正弦曲线两端的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆