Returning "traditional" function notation in the context of Fourier interpolation

in numerical analysis, we students are required to implement a code in R that defines the function f (x), finds its Fourier interpolation tN (x) and calculates the interpolation error

$||f(x)-t_{N}(x)||=\int_{0}^{2\pi}$ $|f(x)-t_{N}(x)|^2$ 

or many different $ N $ At first I tried to calculate the d-coefficients in accordance with this formula:

$d = \frac 1N  M  y$

with M denoting the DFT matrix, and y denoting a series of values ​​of the equidistant function with

$y_j = f(x_j)$ and 
$x_j = e^{\frac{2*pi*i}N*j}$ 
for $j = 1,..,N-1$. 

My goal was to come up with an amount that can be described:

$t_{N}(x) = \Sigma_{k=0}^{N-1} d_k * e^{i*k*x}$

What would be easier to integrate later in the form of a subsequent additive notation.

f <- function(x) 3/(6+4*cos(x)) #first function to compare with
g <- function(x) sin(32*x) #second one
xj <- function(x,n) 2*pi*x/n

M <- function(n){
   w = exp(-2*pi*1i/n)
   m = outer(0:(n-1),0:(n-1))
   return(w^m)
}

y <- function(n){
   f(xj(0:(n-1),n))
} 
transformFunction <- function(n, f){
   d = 1/n * t(M(n)) %*% f(xj(0:(n-1),n))
   script <- paste(d[1])
   for(i in 2:n)
   script <- paste0(script,paste0("+",d[i],"*exp(1i*x*",i,")"))
   #trans <- sum(d[1:n] * exp(1i*x*(0:(n-1))))
   return(script)
 } 

, , : , . , , , , ( ). , , . , , , : , , ? , , -, . !

+4
1

R , , function. , trans x .

integrate , Vectorize .

transformFunction <- function(n, f){
    d = 1/n * t(M(n)) %*% f(xj(0:(n-1),n))

    ## Output function
    trans <- function(x) sum(d[1:n] * exp(1i*x*(0:(n-1))))
    ## Vectorize output for the integrate function
    Vectorize(trans)
} 

, transformFunction:

myint <- transformFunction(n = 10,f = f)

: (integrate )

integrate(function(x) Re(myint(x)),0,2)$value + 
    1i*integrate(function(x) Im(myint(x)),0,2)$value
# [1] 1.091337-0.271636i
+1

All Articles