Evolution of an electromagnetic pulseBeijing Institute of Technology | Ming-Jian Li
The following MATLAB code is a companion code for the course on Artificial Intelligence and Simulation Science. It is designed to calculate the evolution process of an electromagnetic pulse using the Finite-Difference Time-Domain (FDTD) method.
x1% 1D FDTD simulation with pulse2clear;3ke = 100;4% Position of the source5ks = 1;6% ks = ke / 2;7% Number of time steps8nsteps = 2000;9% Cell size and time stepping10c0 = 3.e8;11dx = 0.01;12dt = dx / (2 .* c0);13% Constants14cc = c0 * dt / dx;15% Initialize vectors16ex = zeros(1, ke);17hy = zeros(1, ke);18% Gaussian pulse19t0 = 20;20spread = 8;21% Gaussian signal22for t = 1:nsteps23 time(t) = t;24 ex_source(t) = exp(- .5 * ((t - t0) / spread) ^ 2);25end26plot(time,ex_source)27
28% Start loop29M = moviein(nsteps);30
31for t = 1:nsteps32 % E field loop33 for k = 2:ke - 134 ex(k) = ex(k) + cc * (hy(k - 1) - hy(k));35 end36
37 % Source38 ex(ks) = exp(- .5 * ((t - t0) / spread) ^ 2);39 % H field loop40 for k = 1:ke - 141 hy(k) = hy(k) + cc * (ex(k) - ex(k + 1));42 end43
44 plot(ex); axis([1 ke -2 2]);45 % plot(hy); axis([1 ke -2 2]);46 M(:, t) = getframe;47endThe result is as follows.
