Array dimensions not agreeing

1 visualización (últimos 30 días)
JC
JC el 16 de Jul. de 2020
Comentada: JC el 16 de Jul. de 2020
I'm trying to calculate the magnetic field of a stack of magnet rings given the dimensions of the magnets. However, when I try to calculate the field for each of the parameters for the ring dimensions, the array dimensions of the magnetic field do not agree, giving the error "Array dimensions must match for binary array op.".
I think there is an issue with the way I am combining the arrays in the equation for Bz (see below).
%Range of axial field and step size for functions
zmin=-1;
zmax=1;
dz=0.01; % step size for the plots
nz=round((zmax-zmin)/dz)+1; %number of z steps in the limit
iz=1:nz; %index for each z step
z(iz)=linspace(zmin,zmax,nz); %values of z
%Structure for a magnet, defining the length of a magnet, the
%length of the stack and the position of each magnet (everthing to
%calculate the B field)
B0=1.4; % saturation of permanent magnetic material
nmag=10; % number of rings
imag=1:nmag; %ring index
zlength=(zmax-zmin)/nmag; %length of ring
zstart(imag)=zmin:zlength:(zmax-zlength); %position of ring edge with lower z value
zend(imag)=zstart+zlength; % set up a vector with the ends of each ring section
zmag(imag)=-0.9:0.2:0.9;
%five optimisation parameters
ia=1:5;
a(ia)=linspace(-0.2,0,ia(end));
ib=1:5;
b(ib)=linspace(-0.1,0.1,ib(end));
ic=1:5;
c(ic)=linspace(-0.1,0.1,ic(end));
id=1:5;
d(id)=linspace(-0.1,0.1,id(end));
ie=1:5;
e(ie)=linspace(-0.1,0.1,ie(end));
%magnet radii
or=zeros(imag(end),ia(end),ib(end));
for ib = 1:ib(end)
or(:,:,ib) = 1 + zmag'.^2*a + b(ib)*zmag'.^4;
end
ir=zeros(imag(end),ic(end),id(end),ie(end));
for id = 1:id(end)
for ie = 1:ie(end)
ir(:,:,id,ie) = c + d(id)*zmag'.^2 + e(ie)*zmag'.^4;
end
end
Bz=zeros(nz,nmag,ia(end),ib(end),ic(end),id(end),ie(end)); % store the B field for each ring separately in Bz
for imag=1:imag(end)
Bz=Bz+(B0*0.5*( (zend(imag)-z')./sqrt((zend(imag)-z').^2+ir(imag,:,:,:).^2/4) ...
-(zstart(imag)-z')./sqrt((zstart(imag)-z').^2+ir(imag,:,:,:).^2/4) ...
-(zend(imag)-z')./sqrt((zend(imag)-z').^2+or(imag,:,:).^2/4) ...
+(zstart(imag)-z')./sqrt((zstart(imag)-z').^2+or(imag,:,:).^2/4) ));
end

Respuestas (1)

Matt J
Matt J el 16 de Jul. de 2020
Editada: Matt J el 16 de Jul. de 2020
I think there is an issue with the way I am combining the arrays in the equation for Bz (see below).
I don't see why you would think that unless you are seeing a different error message than the one I am seeing, which occurs far earlier in line 35:
Error in test (line 35)
or(:,:,ib) = 1 + zmag'.^2*a + b(ib)*zmag'.^4;
The output of whos() reveals the problems straight away,
K>> whos or zmag ib a
Name Size Bytes Class Attributes
a 5x1 40 double
ib 1x1 8 double
or 10x5x5 2000 double
zmag 1x10 80 double
Clearly, zmag'.^2 would be a 10x1 matrix, but you are multiplying it with a matrix a that is 5x1. Because the calculation needs to end up giving a 10x5 matrix to agree with the size of or(:,:,ib), I can only guess that you meant to have zmag'.^2 * a.' ?
  8 comentarios
Matt J
Matt J el 16 de Jul. de 2020
Only you can answer that. No one in your audience can know what the intended calculation is.
JC
JC el 16 de Jul. de 2020
Ok I'll try to work it out. Thank you anyway!

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by