MATLAB [Matlab] Subscripted assignment dimension mismatch.

Click For Summary
The discussion revolves around a programming error encountered in a MATLAB script, specifically a "Subscripted assignment dimension mismatch" error occurring in the update equations for a simulation involving electromagnetic fields. The error arises when attempting to assign values to the matrix `ep` using expressions that do not match the expected dimensions. The user is advised to carefully check the sizes of the variables involved in the assignment to identify the source of the mismatch. The code initializes several matrices and coefficients for a simulation of wave propagation, but issues arise during the update of the electric field matrix `ep`. The conversation emphasizes the importance of ensuring that the dimensions of the matrices and vectors being manipulated are compatible to avoid such errors.
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
 
Physics news 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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K