[Matlab] Subscripted assignment dimension mismatch.

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 10K views
s_hy
Messages
57
Reaction score
0
Hi all,

I have the following program, but with this error [Subscripted assignment dimension mismatch.

Error in test_vlf_spherical (line 62)
ep(i,j)=ga(i,j)*ep(i,j)+gb(i,j)*((1./r(i)./dr)*(rph*hr(i,j)-rmh*hr(i-1,j))...]

i am trying to solve the problem by using (./) but i didnt work. can anyone help me to detect the problem? thank you

Code:
close all
clear all

k=200;
T=500;

c=3*10^8;
f=3*10^9;
lamda = c/f;
omega = 2*pi*f;

%ddx = lamda/20;
rr = 201;
dr = 0.05*lamda;
dt = dr/(2*c);
r = 0:dr:dr*(rr-1);

u0=4*pi*1e-7;
eps = 1/(4*pi*9*10^9);  
sigma = 0;  
       
t0 = 40;                

%initialization
ep = zeros(k+1,k+1);
ht = zeros(k+1,k);
hr = zeros(k,k+1);

%ionospheric profile for earth-ionosphere waveguide as coefficients
omegap = 1;
epsr   = 1;
sigmar = 1;

%property coefficient
for i=1:k                                  
    for j=1:k;
        ga(i,j)=exp(-(sigmar*dt)/(eps*epsr));  
        gb(i,j)=(1./sigmar)*(1-exp(-(sigmar*dt)/(eps*epsr)));              
    end;
end;


%define theta
dth(2) = pi/180;

 % update equations
 for t=1:T        
      % source
      %pulse=exp((-0.5)*( (t0-t)/spread ).^2);
      pulse=sin(2*pi*f*t*dt);
      ep(1,:)=pulse;
     
   
    %  update Ez field
    for i=2:k
         for j=2:k
            rph = r(k)+dr/2;
            rmh = r(k)-dr/2;
            ep(i,j)=ga(i,j)*ep(i,j)+gb(i,j)*((1./r(i)./dr)*(rph*hr(i,j)-rmh*hr(i-1,j))...
                -(1./r(i)./dth)*(ht(i,j)+ht(i,j-1)));
         end
    end
     %
      for j=1:k+1
          ep(1,j)=0;
          ep(i+1,j)=0;
      end      
      for i=1:k+1
          ep(i,1)=0;
          ep(i,k+1)=0;
      end      
      %  
         
    %update Ht (theta) field =Hx
    for i=1:k+1
          for j=1:k
              rph = r(i)+dr/2;
              rmh = r(i)-dr/2;
              ht(i,j) = ht(i,j)+(dt/u0/r(i)/dr)*(rph*ep(i,j)-rmh*ep(i,j+1));
         end
    end      

     % update Hr field =Hy
      for i=1:k    
        for j=1:k+1
            hr(i,j) = hr(i,j)+(dt/u0/r(i)/dth/sin(dth))*(sin(dth(i+1))*ep(i+1,j)...
                -sin(dth)*ep(i,j));
        end
    end
   
    % plot
    mesh(ep)
    tm=['T=',num2str(t)]
    text(10,100,0.5,tm)
    axis([1 101 1 101 -1 1]);
    drawnow;
         
end
 
on Phys.org
This error occurs when you reference a group of elements and try to do something that mismatches the size.

For example, if you try to assign a 2 element vector as a single element in a matrix:

A = magic(3);
A(1,1) = [1,2]

Subscripted assignment dimension mismatch.

So I recommend you go through your code and track the sizes of each variable to find the error.