Unable to perform assignment because the indices on the left side are not compatible with the size of the right side
Show older comments
Can anyone solve the problem in my code that returns the error by
?
? close all
clear all
clc
%%
mu0 = 4*pi*1e-7; % Vs/Am
M0 = 1e3; % A/m
maxnum = 31;
rho1_min = 0;
rho1_max = 0.25;
xlim = [-1, 1];
ylim = xlim;
zlim = xlim;
x = linspace(min(xlim), max(xlim), maxnum);
y = linspace(min(ylim), max(ylim), maxnum);
z = linspace(min(zlim), max(zlim), maxnum);
[Xg, Yg, Zg] = ndgrid(x, y, z);
rho = sqrt(Xg.^2 + Yg.^2 + Zg.^2);
phi = angle(Xg + 1i*Yg);
theta = angle(Zg + 1i*sqrt(Xg.^2 + Yg.^2));
%%
RHO = sqrt(x.^2 + y.^2 + z.^2);
THETA = linspace(0, pi, 31); % Trapz
PHI = linspace(0, 2*pi, 31); % Trapz
%%
for i=1:numel(RHO)
for j=1:numel(THETA)
for k=1:numel(PHI)
F_x{i,j,k} = (RHO(i)>= rho1_max) .* 2/3*M0*mu0 .* sin(theta) .* (RHO(i) .* (sin(THETA(j)) .* cos(theta) .* cos(PHI(k)-phi) - cos(THETA(j)) .* sin(theta)) ./ ...
(RHO(i).^2 + rho1_max.^2 - 2.*RHO(i) .* rho1_max .* (sin(THETA(j)) .* sin(theta) .* cos(PHI(k)-phi) + cos(THETA(j)).* cos(theta))).^3/2) .* rho1_max.^2 .* sin(theta);
B1x(i,j,k) = -trapz(PHI,trapz(THETA,F_x{i,j,k},2)) ;
end
end
end
1 Comment
madhan ravi
on 9 Jul 2020
Couple of suggestions:
1) Never name a variable which is the same as MATLAB’s in - built function (xlim.., etc)
2) i and j are imaginary units use ii and jj instead.
3) preallocation is really significant
4) Use cell arrays for preallocation which avoids ambiguities
Accepted Answer
More Answers (0)
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!