| Author |
Comment/Response |
Walter
|
10/29/10 11:04pm
Hi, I'm trying to convert a simple program from Matlab into Mathematica to get some practice learning Mathematica. I'm having some problems duplicating the results. Its a simple 2D FDTD program that has a simple pulse propagate from the middle of the grid. It should propagate in a circle, and it does in Matlab. But it has some weird error somewhere and it propagates in a square which is wrong. Also, the code in Mathematica runs ultra slow, can you suggest a way to speed it up a bit. I've been working on this all day and I would appreciate a second set of eyes on this. Thanks!
I attached the Mathematica Code Notebook
Here's the code from Matlab that works
clc; % clear command window
clear; % clear variables
c=2.99792458e8; % speed of light
mu=4.0*pi*1.0e-7; % free space def
eps=1.0/(c*c*mu);
freq=1e8; % frequency
ie = 100; % grids
je = 100;
steps = 2; % number of time steps to simulate
lambda = 1/freq;
dx = lambda / 20;
dy = lambda / 20;
dt = 1 / (c * sqrt(1 / dx^2 + 1 / dy^2));
% updating coefficients
ca = 1;
cb = dt/(eps*dx);
da = 1;
db = dt/(mu*dx);
% setup arrays
ez = zeros(ie+1,je+1);
hx = zeros(ie+1,je+1);
hy = zeros(ie+1,je+1);
%fdtd algorithm
for n=1:steps
ez(2:ie,2:je)=ca*ez(2:ie,2:je) + cb*(hx(2:ie,1:je-1)-hx(2:ie,2:je) + hy(2:ie,2:je)-hy(1:ie-1,2:je));
% source
ez(ie/2,je/2) = exp(-((n-20)/10)^2) - exp(-((n-40)/10)^2);
hx(2:ie,1:je)=hx(2:ie,1:je) + db*(ez(2:ie,1:je)-ez(2:ie,2:je+1));
hy(1:ie,2:je)=hy(1:ie,2:je) + db*(ez(2:ie+1,2:je)-ez(1:ie,2:je));
end;
%plot ez at last step
figure('position',[100,100,600,500]);
plot(ez),pcolor(ez);
shading flat;
caxis([-.4 0.4]);
colorbar;
axis image;
title(['ez']);
xlabel('i coordinate');
ylabel('j coordinate');
Attachment: fdtd_1d.nb, URL: , |
|