在傅里叶插值的情况下返回函数的“传统"符号 [英] Returning 'traditional' notations of functions in the context of fourier interpolation

查看:170
本文介绍了在傅里叶插值的情况下返回函数的“传统"符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数值分析中,我们必须让学生在R中实现代码,即在给定函数f(x)找到其傅立叶插值tN(x)并计算出插值误差的情况下

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$ 

或各种不同的$ N $ 我首先尝试根据以下公式计算d系数:

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

$d = \frac 1N  M  y$

其中M表示DFT矩阵,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!

推荐答案

R中的函数可以返回任何类,尤其是function类的对象.因此,您可以将trans设为x的函数并将其返回.

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.

由于integrate函数需要矢量化函数,因此我们在输出前使用Vectorize.

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

要进行集成,现在只需使用transformFunction的输出创建一个新变量:

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

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

测试:(integrate只能处理实值函数)

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

这篇关于在傅里叶插值的情况下返回函数的“传统"符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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