title: How to calculate the double integration in R
body: <p>This is my  r code to calculate beta values for each case which is pretty simple</p>
  
  <pre><code>data =data.frame(
    "t" = seq(0, 1, 0.001)
  )
  
  
  B3t &lt;- function(t){
    t**3 - 1.6*t**2 +0.76*t+1
  }
  
  B2t &lt;- function(t){
  
    ifelse(t &gt;= 0 &amp; t &lt; 0.342,
           ((t-0.5)^2-0.025),
  
           ifelse( data$t &gt;=  0.342 &amp; data$t &lt;= 0.658, 
                   0,
  
                   ifelse(t &gt;  0.658 &amp; t &lt;= 1, 
                          (-(t-0.5)^2+0.025),
                          0
                   )))
  
  }
  
  B1t &lt;- function(t){
    0
  }
  
  
  X1t &lt;- function(t){
    a0 = rnorm(1)
    a1 = rnorm(1)
    a2 = rnorm(1)
    a3 = rnorm(1)
  
    return(a0 + a1*t + a2*(t^2) + a3*(t^3))
  }
  
  X2t &lt;- function(t){
    a0 = rnorm(1)
    a1 = rnorm(1)
    a2 = rnorm(1)
    a3 = rnorm(1)
    a4 = rnorm(1)
  
    return(a0 + a1 * sin(2*pi*t) + a2 * cos(2*pi*t) + a3 * sin(4*pi*t) + a4 * cos(4*pi*t))
  }
  </code></pre>
  
  <p>Now I want to calculate the error term.</p>
  
  <blockquote>
    <p>I  have one issue:
    Can anyone help me with this question?</p>
    
    <ol>
    <li>How do I solve the double integration in order to calculate the error term.</li>
    </ol>
  </blockquote>
  
  <p>I know there are functions in r to do <code>integrate</code> but I am not sure how do I implement it here.</p>
  
  <p>I am trying to do functional data analysis problem mentioned below:</p>
  
  <p><a href="https://i.stack.imgur.com/xLvrf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xLvrf.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/MLDer.png" rel="noreferrer"><img src="https://i.stack.imgur.com/MLDer.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>What I don't know is how to find the variance in order to find the error term which follows normal distribution N(0, variance)</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/26hr0.png" rel="noreferrer"><img src="https://i.stack.imgur.com/26hr0.png" alt="enter image description here"></a></p>