From: Stephanie on
I am trying to use a test script to run some code I created. The test script run fine but I cannot get the code to work. I get the error
??? Error: File: FDTD_2D_Absorbing.m Line: 59 Column: 52
Unexpected MATLAB operator.
when trying to run the code.

There is also an error in the editor for that line that says the *variable* appears to change size on every lop iteration. Consider preallocating for speed. This error actually shows up on lines 59, 60, 70, 77, 80, 83, 86, 93, 96, 99. The variable that the error is given for are Ex, Ez, and Hy. Perhaps this error is related to the unexpected Matlab error.

I am pretty sure the matrix is 501 x 501 but I might not be taking into account the boundaries. Therefore, I am hesitant to preallocate. Also, Ex, Ez, HY should not change size, so it might be something wrong in my code causing them to change size. Everything in the code should be regular multiplication, etc. not matrix multiplication, etc. However, in some spots when I added the . in front of the operator it gave me errors. You can clearly see this in those same lines because there are some operators without an . in front of them. If you need to know something else please let me know. This is my first time coding in Matlab and so I apologize if I am lacking some knowledge. Oh I am running R2010A on a Mac.

Below is my code.

Test Script


%====================================================
% This is a test script for FDTD_2D

clear all

% Physical Constants
e_0 = 8.85e-12;
mu_0 = 4e-7 * pi;
c = 2.99e8;
eta_0 = sqrt(mu_0/e_0);

%Source variables

fo=0; % center frequency 300 MHz
BW=3e8; % Bandwidth
Nt = 2000;

if fo==0

lambdao=c/BW;
dx=lambdao/10;
dz=lambdao/10;
x=[0:dx:50*lambdao];
z=[0:dz:50*lambdao];
Nx=length(x);
Nz=length(z);

else


lambdao=c/fo;
dx=lambdao/10;
dz=lambdao/10;
x=[0:dx:50*lambdao];
z=[0:dz:50*lambdao];
Nx=length(x);
Nz=length(z);

end

dt=0.99.*(1./(sqrt((1./dx.^2)./(1./dz.^2))));

% Simulation Constants

e_r = ones(1,Nz);
mu_r = ones(1,Nz - 1);
sigma = zeros(1,Nz);

[Ex,Ey,Hy] = FDTD_2D_Absorbing(x,z,dt,Nt,e_r,mu_r,sigma);

My CODE

function [Ex,Ey,Hy] = FDTD_2D_Absorbing(x,z,dt,Nt,e_r,mu_r,sigma)
%======================================================================================
% function [Ex,Hy] = FDTD_2D_Absorbing(Ex_PS,Source_loc,z,dt,Nt,e_r,mu_r,sigma)
%
% This function performs two-dimensional (Cartesian) finite-difference time-domain
% simulations. It assumes the spatial grid extends from k = (1, 1) to k = (Nx, Nz)
% and that the simulation runs for 'Nt' time steps.
% The function should take the following input arguments, in this order:
%
% Input Parameter Size Description
% x Nx x 1 spatial location along x (m)
% z 1xNz spatial location along z (m)
% dt 1x1 time step size (s)
% Nt 1x1 # steps (time) for simulation
% e_r Nx x Nz relative permittivity
% mu_r Nz-1)x(Nz-1) relative permeability
% sigma Nx x Nz conductivity (S/m)



eo=8.85e-12;
muo=4e-7*pi;
c=3e8;

Nx=length(x);
dx=x(2)-x(1);
Nz=length(z);
dz=z(2)-z(1);

c1 = (1.-((sigma.*dt)./(2.*e_r.*eo)))./(1.+((sigma.*dt)./(2.*e_r.*eo)));
c2 = (dt./sigma)./(1.+((sigma.*dt)/(2.*e_r.*eo)));
c3 = dt ./ (mu_r.*muo);

Hy_old = zeros(Nx,Nz);
Ex_old = zeros(Nx,Nz);
Ez_old = zeros(Nx,Nz);

Ex_1_old=0;
Ex_Nz_old=0;
Ex_2_old=0;
Ez_1_old=0;
Ez_Nx_old=0;
Ez_2_old=0;

for n = 2:Nt %all time steps

% compute E at all interior spatial nodes
for ii=2:(Nx-1)
for jj=2:(Nz-1)

Ex(ii,jj)=c1(ii,jj).*Ex_old(ii,jj)-c2(ii,jj)*((Hy_old(ii,jj)-Hy_old(ii,jj-1))./dz);
Ez(ii,jj)=c1(ii,jj).*Ez_old(ii,jj)+c2(ii,jj)*((Hy_old(ii,jj)-Hy_old(ii-1,jj))./dx);

end
end

% compute H at all interior spatial nodes

for kk=1:(Nx-1)
for mm=1:(Nz-1)

Hy(kk,mm) = Hy_old(kk,mm)-(c3(kk,mm)*((Ez_old(kk+1,mm)-Ez_old(kk,mm))./dx)-((Ex_old(kk,mm+1)-Ex_old(kk,mm))./dz));

end
end

for nn=1:Nx

Ex(nn,1)=Ex_old(nn,2)+((c*dt-dz)/(c*dt+dz))*(Ex(nn,2)-Ex_Nz_old(nn,1));
Ex_Nz_old=Ex(nn,Nz);

Ex(nn,Nz)=Ex_old(nn,Nz-1)+((c*dt-dz)/(c*dt+dz))*(Ex(nn,Nz-1)-Ex_2_old(nn,Nz));
Ex_2_old=Ex(nn,Nz);

Ez(nn,1)=Ez_old(nn,2)+((c*dt-dz)/(c*dt+dz))*(Ez(nn,2)-Ez_Nz_old(nn,1));
Ez_Nz_old=Ez(nn,Nz);

Ez(nn,Nz)=Ez_old(nn,Nz-1)+((c*dt-dz)/(c*dt+dz))*(Ez(nn,Nz-1)-Ez_2_old(nn,Nz));
Ez_2_old=Ez(nn,Nz);

end

for oo=1:Nz

Ex(1,oo)=Ex_old(2,oo)+((c*dt-dz)/(c*dt+dz))*(Ex(2,oo)-Ex_1_old(1,oo));
Ex_1_old=Ex(oo,Nz);

Ex(Nx,oo)=Ex_old(Nx-1,oo)+((c*dt-dz)/(c*dt+dz))*(Ex(Nx-1,oo)-Ex_Nx_old(Nx,oo));
Ex_Nx_old=Ex(oo,Nz);

Ez(1,oo)=Ez_old(2,oo)+((c*dt-dz)/(c*dt+dz))*(Ez(2,oo)-Ez_1_old(1,oo));
Ez_1_old=Ex(oo,Nz);

Ez(Nx,oo)=Ez_old(Nx-1,oo)+((c*dt-dz)/(c*dt+dz))*(Ez(Nx-1,oo)-Ez_Nx_old(Nx,oo));
Ez_Nx_old=Ez(oo,Nz);

end

Ez_old=Ez;
Ex_old=Ex;
Hy_old=Hy;

if mod(n,10)==1

% figure(1)
% plot(z,Ex);
% axis([0 max(z) -1 1]);
% xlabel('z, meters')
% ylabel('Electric Field V/m');
% drawnow
% figure(2)
% plot(z,Hy);
% axis([0 max(z) -1/377 1/377]);
% xlabel('z, meters')
% ylabel('Magnetic Field A/m');

figure(1)
imagesc(x,z,Ex)

figure(2)
imagesc(x,z,Ez)

figure(3)
imagesc(x,z,Hy)

drawnow

end

end
From: Steven Lord on

"Stephanie " <swtchick13543removethis(a)yahoo.com> wrote in message
news:i1fcc1$6c8$1(a)fred.mathworks.com...
>I am trying to use a test script to run some code I created. The test
>script run fine but I cannot get the code to work. I get the error ???
>Error: File: FDTD_2D_Absorbing.m Line: 59 Column: 52
> Unexpected MATLAB operator.
> when trying to run the code.
> There is also an error in the editor for that line that says the
> *variable* appears to change size on every lop iteration. Consider
> preallocating for speed. This error actually shows up on lines 59, 60,
> 70, 77, 80, 83, 86, 93, 96, 99. The variable that the error is given for
> are Ex, Ez, and Hy. Perhaps this error is related to the unexpected
> Matlab error.

*snip*

In the future, when you post large blocks of code and refer to specific
lines, please indicate in the code itself the line to which you're
referring. Using the Code Analyzer message indications you gave I was able
to perform "code registration" and determine that the line in question is:

Ex(ii,jj)=c1(ii,jj).*Ex_old(ii,jj)-c2(ii,jj)*((Hy_old(ii,jj)-Hy_old(ii,jj-1))./dz);

inside the loop that computes E, with column 52 being the first minus sign.
I don't see anything wrong with the code as posted; in Code Analyzer, if you
received that error at runtime, you should have seen a red marker indicating
some sort of syntax error.
My _guess_ is that you wrote this code in some other editor (like Microsoft
Word) that performs some form of "auto-replacement" and that the minus sign
was replaced with some other character that _looks_ like a minus sign but
isn't. Try deleting that character and physically typing the minus sign
from your keyboard in its place. If this resolves the problem, you will
need to run your code, find all the locations where this type of
substitution was performed, and correct them.

As for the second error that says the variable changes size each iteration,
that's not actually an error. That's a message from Code Analyzer (which
used to be called M-Lint) indicating that you're using a programming pattern
that could be inefficient. Read the "Techniques for Improving Performance"
documentation for more information on preallocation:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f8-705906.html

and here for information on Code Analyzer:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f9-22317.html

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Stephanie on
That did not work. And I did not use another editor. I did start out using Matlab on a PC until I was able to get Matlab on my Mac. I have emailed my code to my professor to see if he can fix the error. I have given up.
From: Andy on
I'm not at MATLAB, so I can't double check this, but I'm not aware of a .- operator. For example, in this line:

c1 = (1.-((sigma.*dt)./(2.*e_r.*eo)))./(1.+((sigma.*dt)./(2.*e_r.*eo)));

It's not listed in the documentation, anyway. And I'm not sure how it should differ from regular matrix subtraction.
From: someone on
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i1ftt5$ocb$1(a)fred.mathworks.com>...
> I'm not at MATLAB, so I can't double check this, but I'm not aware of a .- operator. For example, in this line:
>
> c1 = (1.-((sigma.*dt)./(2.*e_r.*eo)))./(1.+((sigma.*dt)./(2.*e_r.*eo)));
>
> It's not listed in the documentation, anyway. And I'm not sure how it should differ from regular matrix subtraction.

I think MATLAB would simply interpret the above as:

> c1 = (1.0-((sigma.*dt)./(2.*e_r.*eo)))./(1.0+((sigma.*dt)./(2.*e_r.*eo)));