Array dimensions not agreeing

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

Answers (1)

Matt J
Matt J on 16 Jul 2020
Edited: Matt J on 16 Jul 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 Comments

Thanks for replying!
When I output whos(), a is a 1x5 matrix so I'm not getting an error for that line
Name Size Bytes Class Attributes
a 1x5 40 double
b 1x5 40 double
or 10x5x5 2000 double
zmag 1x10 80 double
I'm still just getting an error in the calculation of Bz...
Since you accepted my answer, I assume you figured it out?
Sorry I thought it had worked but I'm having the same problem
It works if ia, ib, ic, id and ie range from 1 to 10 but not for any other end value
Matt J
Matt J on 16 Jul 2020
Edited: Matt J on 16 Jul 2020
It would be both more efficient and easier to debug if you would assign some of the intermediate quantities to temporary variables, especially those that will be re-used:
for imag=1:imag(end)
tmp_zend=(zend(imag)-z');
tmp_zstart=(zstart(imag)-z');
tmp_ir=ir(imag,:,:,:).^2/4;
tmp_or=or(imag,:,:).^2/4;
Bz=Bz+(B0*0.5*( tmp_zend./sqrt(tmp_zend.^2+tmp_ir) ...
-tmp_zstart./sqrt(tmp_zstart.^2+tmp_ir) ...
-tmp_zend./sqrt(tmp_zend.^2+tmp_or) ...
+tmp_zstart./sqrt(tmp_zstart.^2+tmp_ir) ));
end
Now, we can see that the sizes of the different temporary variables do not match as needed:
K>> whos tmp*
Name Size Bytes Class Attributes
tmp_ir 1x5x5x5 1000 double
tmp_or 1x5x5 200 double
tmp_zend 201x1 1608 double
tmp_zstart 201x1 1608 double
Thank you, that's helpful!
I'm still not sure how to combine the differently sized matrices so that Bz is a 7-D array with iz, imag, ia, ib, ic, id and ie as the indices.
Only you can answer that. No one in your audience can know what the intended calculation is.
Ok I'll try to work it out. Thank you anyway!

Sign in to comment.

Products

Release

R2018b

Asked:

JC
on 16 Jul 2020

Commented:

JC
on 16 Jul 2020

Community Treasure Hunt

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

Start Hunting!