Unable to perform assignment because the indices on the left side are not compatible with the size of the right side
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nima
le 9 Juil 2020
Modifié(e) : Subhadeep Koley
le 9 Juil 2020
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 commentaire
madhan ravi
le 9 Juil 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
Réponse acceptée
Subhadeep Koley
le 9 Juil 2020
Modifié(e) : Subhadeep Koley
le 9 Juil 2020
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!