Returning 'traditional' notations of functions in the context of fourier interpolation

為{幸葍}努か 提交于 2019-12-10 17:39:40

问题


in numerical analysis we students are obligated to implement code in R that given a function f(x) finds its Fourier interpolation tN(x) and computes the interpolation error

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

or a variety of different $N$ I first tried to compute the d-coefficients according to this formular:

$d = \frac 1N  M  y$

with M denoting the DFT matrix and y denoting a series of equidistant function values 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 a sum that can be described by:

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

Which would be easier to later integrate in sort of a subsequently 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)
 } 

The main purpose of the transform function was, initially, to return a function - or rather: a mathematical expression - which could then be used in order to declarate my Fourier Interpolation Function. Problem is, based on my fairly limited knowledge, that I cannot integrate functions that still have sums nested in them (which is why I commented the corresponding line in the code). Out of absolute desperation I then tried to paste each of the summands in form of text subsequently, only to parse them again as an expression. So the main question that remains is: how do I return mathmatical expressions in a manner that allow me to use them as a function and later on integrate them? I am sincerely sorry for any misunderstanding or confusion, as well as my seemingly amateurish coding. Thanks in advance!


回答1:


A function in R can return any class, so specifically also objects of class function. Hence, you can make trans a function of x and return that.

Since the integrate function requires a vectorized function, we use Vectorize before outputting.

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)
} 

To integrate, now simply make a new variable with the output of transformFunction:

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

Test: (integrate can only handle real-valued functions)

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


来源:https://stackoverflow.com/questions/37802649/returning-traditional-notations-of-functions-in-the-context-of-fourier-interpo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!