Beijing Institute of Technology | Ming-Jian Li
The following MATLAB code is a companion code for the course on Artificial Intelligence and Simulation Science. It functions to calculate the propagation and reflection of a sinusoidal electromagnetic wave using the Finite-Difference Time-Domain (FDTD) method.
x
1clear; clc;
2ke = 100;
3% Position of the source
4ks = 1;
5% ks = ke / 2;
6% Number of time steps
7nsteps = 3000;
8% Cell size and time stepping
9c0 = 3.e8;
10dz = 0.01;
11dt = dz / (2 .* c0);
12% Constants
13cc = c0 * dt / dz;
14% Initialize vector
15ex = zeros(1, ke);
16hy = zeros(1, ke);
17space = zeros(1, ke);
18for k = 1:ke
19 space(k) = k*dz;
20end
21empty = zeros(1, ke);
22% sine wave
23amp = 0.5;
24omg = 0.1;
25for t = 1:nsteps
26 % E field loop
27 for k = 2:ke - 1
28 ex(k) = ex(k) + cc * (hy(k - 1) - hy(k));
29 end
30 % Source
31 ex(ks) = amp* sin( omg* t );
32 % H field loop
33 for k = 1:ke - 1
34 hy(k) = hy(k) + cc * (ex(k) - ex(k + 1));
35 end
36 clf
37 plot3(space,ex,empty, 'r', 'LineWidth', 5 );
38 axis([0 1 -2 2 -2 2]);
39 set(gcf,'Position',[200 200 1080 720]);
40 hold on
41 plot3(space,empty,hy, 'b', 'LineWidth', 5 );
42 pause(0.001);
43end
The result is as follows.