How do edit this code to answer this.

조회 수: 15 (최근 30일)
Prutha Shah
Prutha Shah 2021년 4월 4일
댓글: Walter Roberson 2021년 4월 5일
% 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
Walter Roberson
Walter Roberson 2021년 4월 4일
Your code runs and produces values. What difficulty are you having?
Prutha Shah
Prutha Shah 2021년 4월 4일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
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
Prutha Shah
Prutha Shah 2021년 4월 5일
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 2021년 4월 5일
You have a variable named min that is intefering with using the min function.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Vector Fields에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by