MATLAB Answers

how to plot quiver evenly along lines?

31 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
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)
%% to set number of plotting points along x,y and z axes
%% 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
%% 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
%% 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);
if Rkmag2 > 0
Rk_hat2 = Rk2/Rkmag2;
D2 = D + (rhoL)*Rk_hat2/(4*pi*Rkmag2^2);
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
% 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
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'});


darova on 12 Nov 2019
Is this what you want?
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
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?

Sign in to comment.

Answers (1)

Daniel M
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 :)


Daniel M
Daniel M on 13 Nov 2019
Beauty! Good job, I love see these sorts of plots.
Daniel M
Daniel M on 13 Nov 2019
Please consider accepting this answer.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by