我不能让这段代码起作用。谁能告诉我原因吗?我已经指出了错误出现在(Ar)的哪一行。
% Define the grid
step = 0.1;
min = -1; max = 1;
[x, y] = meshgrid(min:step:max, min:step:max);
% Coordinate transformation
r = sqrt(x.^2 +y.^2); phi = atan2d(y, x);
% Given vector
Ar = r.exp(-r.^2/9) --> (" Dot indexing is not supported for variables of this type")
Ax = Ar.*cosd(phi)
Ay = Ar.*sind(phi)
% Calculate divergence
D = divergence(x, y, Ax, Ay);
% Create a figure
quiver(x, y, Ax, Ay)
grid on; axis square; hold on contour(x, y, D, 30)
xlabel('x'); ylabel('y')
xlim([min max]); ylim([min max]); set(gca, 'Fontsize', 18)
% Choose divergence test points
probe_ind = [16, 16];
probe_x = x(probe_ind(1), probe_ind(2)); probe_y = y(probe_ind(1), probe_ind(2)); probe_r = sqrt(probe_x^2 + probe_y^2);
% Divergence calculated in MATLAB
probe_div = D(probe_ind(1), probe_ind(2));
% Divergence expression calculated manually
calcul_div = 1.78
% Print the answers
fprintf('x = %.10f y = %.10f\n', probe_x, probe_y); fprintf('divergence calculated analytically = %.10f\n', calcul_div); fprintf('divergence calculated by matlab = %.10f\n', probe_div);发布于 2022-04-06 09:59:04
您希望这样做吗?r.exp()主要用于访问属于特定类的结构r的字段exp()或对象r的方法exp()。考虑到r只是一个矩阵,您不能对其进行点索引。最有可能的是,您错过了*,即r.*exp(),从而导致了元向乘法。
https://stackoverflow.com/questions/71764449
复制相似问题