How do edit this code to answer this.
6 次查看(过去 30 天)
显示 更早的评论

% Define the grid
step = 0.1;
min = -7; max = 7;
[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./3).^2));
Aphi = 0;
Az = 0;
% Define vector components in the Cartesian c.s
Ax = Ar.*cosd(phi) - Aphi.*sind(phi);
Ay = Ar.*sind(phi) + Aphi.*cosd(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 = [3, 2];
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 = 0.102;
% Print the answers
fprintf('x = %.10f y = %.10f\n', probe_ind(1), probe_ind(2));
fprintf('divergence calculated analytically = %.10f\n', calcul_div);
fprintf('divergence calculated by matlab = %.10f\n', probe_div);
2 个评论
回答(1 个)
Walter Roberson
2021-4-4
probe_ind = [3, 2];
probe_x = x(probe_ind(1), probe_ind(2));
Those probe_ind are array indices that are going to refer back to
[x, y] = meshgrid(min:step:max, min:step:max);
The probe_ind will not lead you to P2 = (3,2) .
To find (3,2) you need
probe_dist = (x-probe_ind(1)).^2 + (y-probe_ind(2)).^2;
[~, ind] = min(probe_dist(:));
probe_x = x(ind);
probe_y = y(ind);
2 个评论
Walter Roberson
2021-4-5
You have a variable named min that is intefering with using the min function.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Vector Fields 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
