How do edit this code to answer this.
Show older comments

% 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 Comments
Walter Roberson
on 4 Apr 2021
Your code runs and produces values. What difficulty are you having?
Prutha Shah
on 4 Apr 2021
Answers (1)
Walter Roberson
on 4 Apr 2021
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 Comments
Prutha Shah
on 5 Apr 2021
Walter Roberson
on 5 Apr 2021
You have a variable named min that is intefering with using the min function.
Categories
Find more on Vector Fields 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!