# how to plot quiver evenly along lines?

6 views (last 30 days)
Neptune16 on 12 Nov 2019
Commented: Daniel M on 13 Nov 2019
I am currently try to plot electric flux along two lines. but it seem that the electric flux is not spred evenly along this two lines
%%
clear;
clc; % to clear the command line
% to remove all previous variables
%% to set the minimum and maximum values on the plot space (x,y,z axes)
PlotXmin=-3;
PlotXmax=3;
PlotYmin=-3;
PlotYmax=3;
PlotZmin=-3;
PlotZmax=3;
%% to set number of plotting points along x,y and z axes
NumberOfXPlottingPoints=10;
NumberOfYPlottingPoints=10;
NumberOfZPlottingPoints=10;
%% to set number of identical point charge for each 1 meter length
N = 10; % number of point for each 1 meter of length
% Line 1 from -1<x<2 at y=-2, z=0,
xa1 = -1;
xa2 = 2;
L1 = xa2-xa1;%Length of the line charge
% Line 2 from -1<x<2 at y=2, z=0
xb1 = -1;
xb2 = 2;
L2 = xb2-xb1;%Length of the line charge
NL1 = N*L1;
NL2 = N*L2;
dxa= (xa2-xa1)/NL1;
dxb= (xb2-xb1)/NL2;
Xpoints1 = zeros(NL1,1);
Zpoints1 = zeros(NL1,1);
Ypoints1 = zeros(NL1,1);
Xpoints2 = zeros(NL2,1);
Zpoints2 = zeros(NL2,1);
Ypoints2 = zeros(NL2,1);
%% to compute step-size in x,y and z direction berapa jumlah nak langkau
PlotStepX=(PlotXmax-PlotXmin)/(NumberOfXPlottingPoints-1);
PlotStepY=(PlotYmax-PlotYmin)/(NumberOfYPlottingPoints-1);
PlotStepZ=(PlotZmax-PlotZmin)/(NumberOfZPlottingPoints-1);
%% to generate the position vectors
% % untuk create berapa langkau in array
[X,Y,Z]=meshgrid(PlotXmin:PlotStepX:PlotXmax,PlotYmin:PlotStepY:PlotYmax,PlotZmin:PlotStepZ:PlotZmax);%build arrays of plot space
% to initialize matrices of zeros, in x,y and z-direction
Dxa=zeros(NumberOfZPlottingPoints,NumberOfYPlottingPoints,NumberOfXPlottingPoints);
Dza=zeros(NumberOfZPlottingPoints,NumberOfYPlottingPoints,NumberOfXPlottingPoints);
Dya=zeros(NumberOfZPlottingPoints,NumberOfYPlottingPoints,NumberOfXPlottingPoints);
Dxb=zeros(NumberOfZPlottingPoints,NumberOfYPlottingPoints,NumberOfXPlottingPoints);
Dzb=zeros(NumberOfZPlottingPoints,NumberOfYPlottingPoints,NumberOfXPlottingPoints);
Dyb=zeros(NumberOfZPlottingPoints,NumberOfYPlottingPoints,NumberOfXPlottingPoints);
%% General Information on the point charge
rhoL=1e-6; % value of the line charge density (charge per unit length)
%% Computing the vectors of flux density
for kk=1:NumberOfZPlottingPoints % start 1 to number of z plotting
for jj=1:NumberOfYPlottingPoints % start 1 to number of y plotting
for ii=1:NumberOfXPlottingPoints % start 1 to number of x plotting
Xplot=X(kk,jj,ii);%x coordinate of current plot point
Yplot=Y(kk,jj,ii);%y coordinate of current plot point
Zplot=Z(kk,jj,ii);%z coordinate of current plot point
r =[Xplot Yplot Zplot]; %position vector of observation points
D=[0 0 0]; % to initialize the value of the flux density
for kkk = 1:1:NL1
xCenter1 = xa1 +(kkk-1)*dxa +0.5*dxa;
xCenter2 = xb1 +(kkk-1)*dxb +0.5*dxb;
rqk1 = [xCenter1, 2, 0]; %% center point of flux LINE 1
rqk2 = [xCenter2, -2, 0]; %% center point of flux LINE 2
Rk1 = r-rqk1;
Rk2 = r-rqk2;
Xpoints1(kkk) = xCenter1; %%location of line charge
Ypoints1(kkk) = -2; %%location of line charge
Zpoints1(kkk) = 0; %%location of line charge
Xpoints2(kkk) = xCenter2; %%location of line charge
Ypoints2(kkk) = 2; %%location of line charge
Zpoints2(kkk) = 0; %%location of line charge
Rkmag1 = norm(Rk1);
Rkmag2 = norm(Rk2);
if Rkmag1 > 0
Rk_hat1 = Rk1/Rkmag1;
D1 = D + (rhoL)*Rk_hat1/(4*pi*Rkmag1^2);
end
if Rkmag2 > 0
Rk_hat2 = Rk2/Rkmag2;
D2 = D + (rhoL)*Rk_hat2/(4*pi*Rkmag2^2);
end
end
Dxa(kk,jj,ii)=D1(1,1); %get x component at the current observation point
Dya(kk,jj,ii)=D1(1,2); %get z component at the current observation point
Dza(kk,jj,ii)=D1(1,3); %get y component at the current observation point
Dxb(kk,jj,ii)=D2(1,1); %get x component at the current observation point
Dyb(kk,jj,ii)=D2(1,2); %get z component at the current observation point
Dzb(kk,jj,ii)=D2(1,3); %get y component at the current observation point
end
end
end
% to plot the flux density vector lines
h1 = quiver3(X,Y,Z,Dxa,Dya,Dza);
hold on
h2 = quiver3(X,Y,Z,Dxb,Dyb,Dzb);
hold on
% to appropriately labelling the axes
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
axis([PlotXmin,PlotXmax,PlotYmin,PlotYmax,PlotZmin,PlotZmax]);
grid on
%to plot the line charge distribution L1,L2, L3,L4
h1_rhoL = plot3(Xpoints1,Ypoints1,Zpoints1,'-k','LineWidth',4);
h2_rhoL = plot3(Xpoints2,Ypoints2,Zpoints2,'-k','LineWidth',4);
% h3 = plot3([Xmin1,Xmax1],[0,0],[0,0]); % plot line charge 1
%to put legend on each plot
legend([h1;h2],{'Flux 1';'FLux 2'});
##### 3 CommentsShow 1 older commentHide 1 older comment
Neptune16 on 13 Nov 2019
Edited: Neptune16 on 13 Nov 2019
Hello, sir. Thank you for your concern for helping me.
#please refer image attachment (1233.png) above#
If you can see in the "red frame" , the flux is not same length along the point 1 (marked in green) to point 2 (marked in red). My plan is to make both lines in "red frame" distribute flux same as line in "green frame" but for 2 lines.
Daniel M on 13 Nov 2019
Edited: Daniel M on 13 Nov 2019
I didn't go through your code but it doesn't look like you're plotting a line charge at all. It looks like you're plotting the flux from two point charges, originating from point 2 and the equivalent location on the second black line. I would suspect it's something to do with the loops only storing the values for the last iteration.
But you seem to have done it properly for the green frame. Just do that, twice. What's the problem?

Daniel M on 13 Nov 2019
I just took a closer look and it is what I suspected. In your loop over NL1 you are only storing the outputs, D1 and D2, for the last iteration. So you are basically calculating the field from a single point charge, stored in the last iteration of that inner most loop.
Instead you should do the following. Preallocate D1 and D2 to be zeros of length NL1. Then store each value using D1(kkk,:) = ...; Then sum D1 for x, y, and z and store into Dxa.
There are several other minor quirks with this code but I will let you figure them out :)
##### 3 CommentsShow 1 older commentHide 1 older comment
Daniel M on 13 Nov 2019
Edited: Daniel M on 13 Nov 2019
Beauty! Good job, I love see these sorts of plots.
Daniel M on 13 Nov 2019

### Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

R2018b

### Community Treasure Hunt

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

Start Hunting!