Plot E-field ?

조회 수: 13 (최근 30일)
sanbadgerdude
sanbadgerdude 2016년 3월 25일
답변: Chaitanya 2023년 7월 11일
Here is my code. It calculates e-field at a point (PE) based on multiple charges (NOC) spaced along the Z-axis. Loops through calculating for each point and sums it up to give E. I want to plot the results for E.
clc;
clear all;
NOC = 9;
Q = 0.3e-6;
PE = [2 3 4];
Eps0 = 8.854e-12;
E = [0 0 0];
for i=(-(NOC-1)/2:(NOC-1)/2)
PC=[0 0 i];
R=(PE-PC);
E1=(Q*R)/(4*pi*Eps0*norm(R)^3);
E=E+E1
end
(results)
E =
439.4929 659.2394 451.1651

답변 (1개)

Chaitanya
Chaitanya 2023년 7월 11일
To plot the results for the electric field (E), you can use the plot3 function in MATLAB. Here's an example of how you can modify your code to plot the values of E:
clc;
clear all;
NOC = 9;
Q = 0.3e-6;
PE = [2 3 4];
Eps0 = 8.854e-12;
E = [0 0 0];
% Arrays to store the calculated electric field values
x = [];
y = [];
z = [];
for i = (-(NOC-1)/2 : (NOC-1)/2)
PC = [0 0 i];
R = (PE - PC);
E1 = (Q * R) / (4 * pi * Eps0 * norm(R)^3);
E = E + E1;
% Store the electric field values for plotting
x = [x E(1)];
y = [y E(2)];
z = [z E(3)];
end
% Plot the electric field values
figure;
plot3(x, y, z, 'o-');
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Electric Field (E) at each point');
% Optional: Add gridlines to the plot
grid on;
This modified code will calculate the electric field values at each point and store them in separate arrays (x, y, z). Then, the plot3 function is used to create a 3D plot of the electric field values. The xlabel, ylabel, and zlabel functions are used to label the axes, and the title function sets the title of the plot. Finally, the grid on command adds gridlines to the plot for better visualization.
You can run this code in MATLAB to plot the electric field values and visualize them in a 3D plot.
Hope this helps!

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by