二维TE波FDTD方法引入高斯源作图,显示收敛阶matlab实现
时间: 2023-06-26 15:06:29 浏览: 36
Matlab实现高斯滤波
二维TE波FDTD方法的收敛性可以通过引入高斯源来验证。具体实现如下:
1. 定义计算区域和网格尺寸
```
% 计算区域和网格尺寸
nx = 100; % x方向网格数
ny = 100; % y方向网格数
dx = 0.01; % x方向网格尺寸
dy = 0.01; % y方向网格尺寸
dt = 0.0001; % 时间步长
maxTime = 0.1; % 最大模拟时间
```
2. 定义介质参数和边界条件
```
% 介质参数
epsilon = ones(nx, ny);
mu = ones(nx, ny);
% 边界条件
Ex = zeros(nx, ny);
Ey = zeros(nx, ny);
Hz = zeros(nx, ny);
```
3. 定义高斯源
```
% 定义高斯源
x0 = nx/2;
y0 = ny/2;
tau = 0.05;
f0 = 1/tau;
omega0 = 2*pi*f0;
source = exp(-((1:ny)-y0).^2/(2*tau^2)) .* exp(-1i*omega0*(0:ny-1)*dt);
```
4. 进行时域模拟
```
% 进行时域模拟
for n = 1:round(maxTime/dt)
% 更新Ex、Ey
Ex(:, 2:end) = Ex(:, 2:end) + dt./(dx*mu(:, 2:end)) .* (Hz(:, 2:end) - Hz(:, 1:end-1));
Ey(2:end, :) = Ey(2:end, :) - dt./(dy*mu(2:end, :)) .* (Hz(2:end, :) - Hz(1:end-1, :));
% 添加高斯源
Ey(:, 1) = Ey(:, 1) + source(n);
% 更新Hz
Hz(2:end-1, 2:end-1) = Hz(2:end-1, 2:end-1) - dt./(dx*dy*epsilon(2:end-1, 2:end-1)) .* ...
(Ex(2:end-1, 2:end) - Ex(2:end-1, 1:end-1) + Ey(2:end, 2:end-1) - Ey(1:end-1, 2:end-1));
% 边界处理
Hz(:, 1) = Hz(:, 2);
Hz(:, end) = Hz(:, end-1);
Hz(1, :) = Hz(2, :);
Hz(end, :) = Hz(end-1, :);
end
```
5. 计算收敛阶并作图
```
% 计算收敛阶
h = dx;
Ez = Hz;
error_norm = zeros(1, 6);
for i = 1:6
nx = 2^i*10;
ny = 2^i*10;
dx = h/i;
dy = h/i;
dt = dx/2;
maxTime = 0.1;
[Ez_ref, ~] = FDTDTE(nx, ny, dx, dy, dt, maxTime);
error_norm(i) = norm(Ez(1:i:end, 1:i:end) - Ez_ref, 'fro');
end
slope = polyfit(log(h./(2.^(1:6))), log(error_norm), 1);
convergence_order = -slope(1)
% 作图
figure;
loglog(h./(2.^(1:6)), error_norm, '-o');
title('Convergence of 2D FDTD with Gaussian Source', 'FontSize', 14);
xlabel('\Delta x', 'FontSize', 12);
ylabel('L_2 Norm Error', 'FontSize', 12);
grid on;
```
其中,`FDTDTE`为二维TE波FDTD方法的实现代码。运行上述代码即可得到收敛阶和收敛性图像。
阅读全文