Unable to perform assignment because the indices on the left side are not compatible with the size of the right side

조회 수: 1 (최근 30일)
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
madhan ravi
madhan ravi 2020년 7월 9일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Subhadeep Koley
Subhadeep Koley 2020년 7월 9일
편집: Subhadeep Koley 2020년 7월 9일
Pre allocate B1x as cell array instead of numeric array to solve the problem.
close
clear
clc
%%
mu0 = 4*pi*1e-7; % Vs/Am
M0 = 1e3; % A/m
maxnum = 31;
rho1_min = 0;
rho1_max = 0.25;
xlimit = [-1, 1];
ylimit = xlimit;
zlimit = xlimit;
x = linspace(min(xlimit), max(xlimit), maxnum);
y = linspace(min(ylimit), max(ylimit), maxnum);
z = linspace(min(zlimit), max(zlimit), 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
%%
% Pre-allocate F_x and B1x as cell array
F_x = cell(numel(RHO), numel(THETA), numel(PHI));
B1x = cell(numel(RHO), numel(THETA), numel(PHI));
for ii = 1:numel(RHO)
for jj = 1:numel(THETA)
for kk = 1:numel(PHI)
F_x{ii, jj, kk} = (RHO(ii)>= rho1_max) .* 2/3*M0*mu0 .* sin(theta) .* (RHO(ii) .* (sin(THETA(jj)) .* cos(theta) .* cos(PHI(kk)-phi) - cos(THETA(jj)) .* sin(theta)) ./ ...
(RHO(ii).^2 + rho1_max.^2 - 2.*RHO(ii) .* rho1_max .* (sin(THETA(jj)) .* sin(theta) .* cos(PHI(kk)-phi) + cos(THETA(jj)).* cos(theta))).^3/2) .* rho1_max.^2 .* sin(theta);
B1x{ii, jj, kk} = squeeze(-trapz(PHI,trapz(THETA,F_x{ii,jj,kk},2)));
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by