Plot the potential surface and electric field strength vectors in the Oxy plane

49 views (last 30 days)
Write Matlab program to:
1) Enter an expression , for example, lg (x) + y
2) Limit the space of the Oxy plane with xmax = ymax = 10
3) Use symbolic operations to compute and compute Ex and Ey components at all points in given space.
4) Plot the potential surface V and electric field vectors .

Answers (1)

Shishir Reddy
Shishir Reddy on 6 Nov 2024
Hi Nguyen
As per my understanding, you would like to write a MATLAB program that computes and visualizes the potential surface and electric field vectors from a user-defined mathematical expression over a specified 2D space.
Kindly refer the following snippets to achieve the tasks you've outlined (Assumption - ‘V’, ‘xmax’,’ ymax’ are defined)–
Computing the Electric field components –
Ex = -diff(V, x);
Ey = -diff(V, y);
Creating a grid for plotting –
[xGrid, yGrid] = meshgrid(linspace(-xmax, xmax, 20), linspace(-ymax, ymax, 20));
Evaluating the potential and electric field components on the grid
VGrid = double(subs(V, {x, y}, {xGrid, yGrid}));
ExGrid = double(subs(Ex, {x, y}, {xGrid, yGrid}));
EyGrid = double(subs(Ey, {x, y}, {xGrid, yGrid}));
Plotting the potential surface –
surf(xGrid, yGrid, VGrid);
Plotting the electric field vectors –
quiver(xGrid, yGrid, ExGrid, EyGrid);
For more information regarding the ‘diff’, ‘meshgrid’, ‘subs’, ‘surf’, ‘quiver’ functions, kindly refer to the following documentations -
I hope this helps.
  2 Comments
Nguyen Duc
Nguyen Duc on 6 Nov 2024
% Define constants
k = 8.99e9;% Coulomb's constant in N*m^2/C^2
q = 1e-9;% Charge in Coulombs
% Define grid for the Oxy plane
x = linspace(-1, 1, 50); % X-axis range
y = linspace(-1, 1, 50); % Y-axis range[X, Y] = meshgrid(x,y);
% Calculate the potential V at each point (X,Y)
% For a point charge at the origin:
r = sqrt(X.^2 + Y.^2); % Distance from the origin
V=k* q ./ r; % Potential (Avoid r = e to prevent infinity)
V(r == 0 ) NaN; % Set NaN at r = e to avoid infinity in plot
% Calculate the electric field component Ex and Ey
[Ex, Ey] = gradient(-V,x,y);
% Plot the potential surface
figure;
surf(X, Y, V,'EdgeColor','none');
colorbar;
title('Electric Potential Surface');
xlabel('x(m)");
ylabel("y(m)');
zlabel('Potential(V)');
view(3);% 3D view
% Overlay electric field vectors hold on; quiver(X, Y,Ex, Ey, 'k'); % Electric field vectors in black
hold off;
% Add labels for electric field plot title('Electric Potential Surface and el.V ric Field vectors');
xlabel('x(m)');
ylabel("y(m)');
zlabel('Potential (V)");
view(3); % Adjust viewing angle for better visualization
Do you think my code is ok? If it is not hope you can give me more helps to improve it. Thanks a lot!

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!