Need some help finding finding the gradient and creating a vector space to use contour and quiver plots.

4 次查看(过去 30 天)
The potential of an electric field due to a point charge is given (in spherical coordinates) by V= k/r where k is a constant. Assuming k = 1, find V in cartesian coordinates. Use Matlab to determine the gradient in cartesian coordinates via the symbolic toolbox , and then create a vector space and use the contour and quiver plots to illustrate the gradient of the function in the z = 0 plane.

回答(1 个)

Nithin Kumar
Nithin Kumar 2023-9-8
Hi Hunter,
I understand that you want to determine the gradient in cartesian coordinates using Symbolic toolbox. To perform this analysis in MATLAB, kindly refer to the following steps:
1. Define the symbolic variables and constants:
syms x y z r k;
k = 1; % Assuming k = 1
r = sqrt(x^2 + y^2 + z^2); % Define r in Cartesian coordinates
2. Calculate the electric potential 'V':
V = k / r;
3. Calculate the gradient of 'V' in Cartesian coordinates:
gradV = gradient(V, [x, y, z]);
4. Create a vector space in the xy-plane (z = 0) and evaluate the gradient:
[x_vals, y_vals] = meshgrid(-5:0.5:5, -5:0.5:5);
z_val = 0;
gradV_eval = subs(gradV, [x, y, z], [x_vals, y_vals, z_val]);
5. Create "contour" and "quiver" plots to illustrate the gradient in the "z = 0" plane:
% Contour plot of the electric potential
subplot(1, 2, 1);
contour(x_vals, y_vals, double(subs(V, [x, y, z], [x_vals, y_vals, z_val])));
title('Contour Plot of Electric Potential (V)');
% Quiver plot of the gradient
subplot(1, 2, 2);
quiver(x_vals, y_vals, double(gradV_eval(:,:,1)), double(gradV_eval(:,:,2)));
title('Quiver Plot of Gradient of V');
% Adjust plot parameters as required
axis equal;
For more information regarding “meshgrid”, “sbs”, "contour" and "quiver" functions, kindly refer to the following documentation:
I hope this provides you with the required information regarding your query.
Nithin Kumar.


Help CenterFile Exchange 中查找有关 General Physics 的更多信息





Community Treasure Hunt

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

Start Hunting!

Translated by