How do edit this code to answer this.

8 visualizzazioni (ultimi 30 giorni)
Prutha Shah
Prutha Shah il 4 Apr 2021
% 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 Commenti
Walter Roberson
Walter Roberson il 4 Apr 2021
Your code runs and produces values. What difficulty are you having?
Prutha Shah
Prutha Shah il 4 Apr 2021
Hi walter, i am not getting the correct matlab answer that matches the theoretical one. for example, i seem to get errors for when i put the test point in as (0.5, 0.5). here i should get the divergence as 1.787

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 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 Commenti
Prutha Shah
Prutha Shah il 5 Apr 2021
Hi walter, thank you for your help. However im not sure if this is what you ment? From this i still recieve an eerror "insufficient number of outputs from right hand side of equal sign to satisfy assignment." This error reffers to [~, ind] = min(probe_dist(:));
Walter Roberson
Walter Roberson il 5 Apr 2021
You have a variable named min that is intefering with using the min function.

Accedi per commentare.

Categorie

Scopri di più su Vector Fields in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by