差分法与傅里叶谱方法计算拉普拉斯算子

使用差分法和傅里叶谱方法求解拉普拉斯算子

%Test laplacian operator with difference method and fourier spectral method
clear;close all;
Nx = 128;Ny=Nx;dx=1;dy=dx;
%% Generage Gaussian Distribution
X = 0 : 1 : Nx-1;Y = 0 : 1: Ny-1;
fangcha= 100;fangcha2 = 220;
u1 = zeros(Nx, Ny);
u2 = zeros(Nx,Ny); 
%A = 10 ^ 5;
A=1;
center_u1 = 50;
for row = 1 : 1 : Nx
    for col = 1 : 1 : Ny
        u1(row, col) = (X(row) - center_u1) .* (X(row)-center_u1) + (Y(col) - center_u1) .* (Y(col) - center_u1);
    end
end
u1 = -u1/(2*fangcha);
u1 = A * exp(u1) / (sqrt(2*pi) * sqrt(fangcha));
surf(X, Y, u1);surf(X, Y, u2);

%% Calculate Laplacian with Difference Method 
lap_u1_diff = laplacian_var(Nx,Ny,dx,dy,u1);

%% Calculate Laplacian with Spectral Method
[kx,ky,kX,kY,k2,k4] = prepare_fft(Nx,Ny,dx,dy);
lap_u1_spectral_method = real(ifft2(-1*k2.*fft2(u1)));
figure(1);
subplot(2,2,1);imagesc(lap_u1_diff);colorbar;title('lap u1 DifferenceMethod');
subplot(2,2,2);imagesc(lap_u1_spectral_method);colorbar;title('lap u1 SpectralMethod');
subplot(2,2,3);mesh(lap_u1_diff);colorbar;title('lap u1 DifferenceMethod');
subplot(2,2,4);mesh(lap_u1_spectral_method);colorbar;title('lap u1 SpectralMethod');
error = lap_u1_diff - lap_u1_spectral_method;
error_sum = sum(error(:));

差分法与傅里叶谱方法计算拉普拉斯算子_第1张图片

你可能感兴趣的:(差分法与傅里叶谱方法计算拉普拉斯算子)