Clear Filters
Clear Filters

Relate a matrix 1000*3 streamslice representation with 2 points in a graph

3 views (last 30 days)
Ok, so the problem is related to the representation of a charge in a electric field that changes according to a cosine. Now, i have done this representation with a streamslice function and i have in the other hand a 10000*3 matrix with values of the field. The issue, is that i have to calculate the torque that is tau=p x E, where tau is the torque, p de electric dipole moment and E de electric field matrix 10000*3. Therefore, to do so i thought of relating the points red in the graph with the so to said value of the blue line of the streamslice, in order to obtain a 3x1 matrix for the electric field and , as the p(dipolar electric moment is a vector of the form(x,y,0), i can therefore multiply it and obtain the desire torque. So basically, i think that what i have to relate is where the points red are disposed with the closest point of the streamslice function, so to kind of iterate among values of it, therefore changing p(as the dipolar moment is calculated as the distance between charges, that are disposed in the linspace of -1 to 1)
clear; clc; close all
figure; hold on;grid on
q1= (rand-.5)*(4*rand+1)*1e-6;
r1 =[1.95*(rand-.5) 1.95*(rand-.5) 0];
q2 = -q1;
r2 = [1.95*(rand-.5) 1.95*(rand-.5) 0];
qa = (rand-.5)*(4*rand+1)*1e-6;
ra = [1 1 0];
qa = (rand-.5)*(4*rand+1)*1e-6;
rb = [-1 -1 0];
qb = -qa;
h=plot3(r1(1),r1(2),r1(3),'.','MarkerSize',30,'Color',[255,87,51]/255);
text(r1(1)+0.03,r1(2)+0.03,r1(3)+0.03,[num2str(q1*1e6,'%.2f'),' \muC']);
h2=plot3(r2(1),r2(2),r2(3),'.','MarkerSize',30,'Color',[255,87,51]/255);
text(r2(1)+0.03,r2(2)+0.03,r2(3)+0.03,[num2str(q2*1e6,'%.2f'),' \muC']);
plot3(ra(1),ra(2),ra(3),'.','MarkerSize',30,'Color',[93,173,226]/255);
text(ra(1)+0.03,ra(2)+0.03,ra(3)+0.03,[num2str(qa*1e6,'%.2f'),' \muC']);
plot3(rb(1),rb(2),rb(3),'.','MarkerSize',30,'Color',[93,173,226]/255);
text(rb(1)+0.03,rb(2)+0.03,rb(3)+0.03,[num2str(qb*1e6,'%.2f'),' \muC']);
[X,Y] = meshgrid(linspace(-1,1,100),linspace(-1,1,100)); % XY plane
d = 0.001; % Distance from the charges to the XY plane
Z = d*ones(size(X));
R = [X(:) Y(:) Z(:)];
%{
E1 = zeros(size(R));
E1 = electricField(r1,R,q1)
E2 = zeros(size(R))
E2 = electricField(r2,R,q2)
%}
Ea = zeros(size(R));
Ea = electricField(ra,R,qa);
Eb = zeros(size(R));
Eb = electricField(rb,R,qb);
Et = Ea + Eb;
Ex = reshape(Et(:,1),size(X));
Ey = reshape(Et(:,2), size(X));
streamslice(X,Y,Ex,Ey); axis tight
%% Functions to visualize the vectors of the Coulomb force
drawLineBetweenCharges=@(ri,rt) plot3([ri(1) rt(1)],[ri(2) rt(2)],[ri(3) rt(3)],':k'); % A line is drawn between qi and qt
drawForceArrow=@(posOrigin,forceVector,varargin) quiver3(posOrigin(1),posOrigin(2),posOrigin(3),forceVector(1),forceVector(2),forceVector(3),varargin{:});
r = drawLineBetweenCharges(r2,r1) % A line is drawn between q2 and qt
l = sqrt((r1(1)-r2(1))^2 + (r1(2)-r2(2))^2+(r1(3)-r2(3))^2)
p = l*q1
R = zeros(size(Et));
%{
Fcharge1_x = Ex* q1;
Fcharge1_y = Ey * q1;
Fcharge2_x = Ex * q2;
Fcharge2_y = Ey * q2;
%}
xlabel('x (m)'), ylabel('y (m)');
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AUXILIARY FUNCTIONS (they are functions that are going to be used often) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function E=electricField(ri,R,qi)
c=299792458; % Speed of light (m/s)
ke=c^2/1e7; % Coulomb constant (Nm^2/C^2)
% r = norm(R-ri); % Distance in the direction ri to dipole
r =sqrt(sum((R-ri).^2,2)); % Distance in the direction ri to dipole
u = (R-ri)./r; % Unitary vector of distance in Cartesian coordinates
E = ke*qi./r.^2 .* u; % Electric field (E)
end

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!