在 MATLAB 中编写 n 项傅立叶级数的函数?
Writing function for n-numbered term Fourier series in MATLAB?
使用fit函数,在MATLAB中使用Fourier fit可以得到的最大项数是8:
f = fit(xs,ys,'fourier8')
不幸的是,一个 8 学期的系列不适合我的目的。目标是用傅立叶模型拟合数据集并提取系数,并且系数需要尽可能精确以供以后分析。我想知道是否有人知道如何使用以下方法修改预先存在的拟合函数:
fitoptions('fourier8')
这将创建一个 n 项傅里叶级数,其中 n 是项数(有限)。如果这是不可行的,那么如何编写一个程序来使用下图中显示的通用公式创建一个具有可提取系数的 n 项傅里叶级数(有限)注意:边界不会是 INF 和将改为 n.
更新:我找到了接受已知系数的代码,但是如何调整它以便在 n 项之前未知时主动 adjusts/fine-tunes 系数?此外,如何找到该期间的上限和下限(因为这些是此脚本的输入)?
a0 = input('a0: ');
an = input('an: ');
bn = input('bn: ');
a = input('lower boundary: ');
b = input('upper boundary: ');
t = linspace(a,b,10000); % note: this is just for testing the code
suma =0;
for n=1:100
ebn = evalin('caller',bn);
ean = evalin('caller',an);
suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
end
series = a0 + suma;
plot(t,series)
我很确定您正在做与我在研究中所做的类似的事情。以下代码为 n 个点和 2 x hmodes+1 生成傅立叶矩阵。现在。你可以调整它。所以解决 Ax=b 给出了这个系数。如果没有,它有点方便。我只是注意到这很慢。有更快的 FFT 方法。我认为,如果您在我的问题答案中的某处查看,就会有一个 python 问题列出 one. 这实际上是相同的研究。我在别处修好了。
function FCMat = FCMatGen(pts,hmodes)
% Takes the following inputs
% pts: the number of pts
% hmodes: the numbers of modes
% Returns the followinmn
% The matrix A: npts x hmodes for the Fourier Continuation
% problem
A = dftmtx(2*pts);
A = A(1:pts,:);
Ar = real(A(:,(1:(hmodes+1))));
Ai = imag(A(:,(2:(hmodes+1))));
A = [Ar,Ai];
FCMat = A;
end
使用fit函数,在MATLAB中使用Fourier fit可以得到的最大项数是8:
f = fit(xs,ys,'fourier8')
不幸的是,一个 8 学期的系列不适合我的目的。目标是用傅立叶模型拟合数据集并提取系数,并且系数需要尽可能精确以供以后分析。我想知道是否有人知道如何使用以下方法修改预先存在的拟合函数:
fitoptions('fourier8')
这将创建一个 n 项傅里叶级数,其中 n 是项数(有限)。如果这是不可行的,那么如何编写一个程序来使用下图中显示的通用公式创建一个具有可提取系数的 n 项傅里叶级数(有限)注意:边界不会是 INF 和将改为 n.
更新:我找到了接受已知系数的代码,但是如何调整它以便在 n 项之前未知时主动 adjusts/fine-tunes 系数?此外,如何找到该期间的上限和下限(因为这些是此脚本的输入)?
a0 = input('a0: ');
an = input('an: ');
bn = input('bn: ');
a = input('lower boundary: ');
b = input('upper boundary: ');
t = linspace(a,b,10000); % note: this is just for testing the code
suma =0;
for n=1:100
ebn = evalin('caller',bn);
ean = evalin('caller',an);
suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
end
series = a0 + suma;
plot(t,series)
我很确定您正在做与我在研究中所做的类似的事情。以下代码为 n 个点和 2 x hmodes+1 生成傅立叶矩阵。现在。你可以调整它。所以解决 Ax=b 给出了这个系数。如果没有,它有点方便。我只是注意到这很慢。有更快的 FFT 方法。我认为,如果您在我的问题答案中的某处查看,就会有一个 python 问题列出 one. 这实际上是相同的研究。我在别处修好了。
function FCMat = FCMatGen(pts,hmodes)
% Takes the following inputs
% pts: the number of pts
% hmodes: the numbers of modes
% Returns the followinmn
% The matrix A: npts x hmodes for the Fourier Continuation
% problem
A = dftmtx(2*pts);
A = A(1:pts,:);
Ar = real(A(:,(1:(hmodes+1))));
Ai = imag(A(:,(2:(hmodes+1))));
A = [Ar,Ai];
FCMat = A;
end