-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorField.m
More file actions
68 lines (51 loc) · 1.35 KB
/
Copy pathVectorField.m
File metadata and controls
68 lines (51 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
clc, clearvars, close all
l = -1.5; r = 1.5; d = 0.2;
[x,y,z] = meshgrid(l:d:r, l:d:r, l:d:r);
c1x = -0.6; c1y = 0; c1z = 0; q1 = 1e-8;
c2x = 0.6; c2y = 0; c2z = 0; q2 = -1e-8;
k = 1/(4*pi*8.85e-12);
r1x = x - c1x;
r1y = y - c1y;
r1z = z - c1z;
r1 = sqrt(r1x.^2 + r1y.^2 + r1z.^2);
r2x = x - c2x;
r2y = y - c2y;
r2z = z - c2z;
r2 = sqrt(r2x.^2 + r2y.^2 + r2z.^2);
E1x = k * q1 * (r1x./r1.^3);
E1y = k * q1 * (r1y./r1.^3);
E1z = k * q1 * (r1z./r1.^3);
E2x = k * q2 * (r2x./r2.^3);
E2y = k * q2 * (r2y./r2.^3);
E2z = k * q2 * (r2z./r2.^3);
u = E1x + E2x;
v = E1y + E2y;
w = E1z + E2z;
figure(1)
title("Electric field")
subtitle("Graph of a field generated by two opposite charges")
hold on
minR = 0.1;
mask = (r1>minR) & (r2>minR);
xq = x(mask);
yq = y(mask);
zq = z(mask);
uq = u(mask);
vq = v(mask);
wq = w(mask);
quiver3(xq,yq,zq,uq,vq,wq,5);
plot3(c1x, c1y, c1z, "ob", 'MarkerSize', 10, 'MarkerFaceColor', "blue")
plot3(c2x, c2y, c2z, "or", 'MarkerSize', 10, 'MarkerFaceColor', "red")
view(3)
[startX, startY, startZ] = meshgrid(-0.8:0.25:-0.4, -1:0.25:1, -1:0.25:1);
verts = stream3(x,y,z,u,v,w,startX,startY,startZ);
streamline(verts, "Color", "yellow");
xlim([-1 1]), ylim([-1 1]), zlim([-1 1])
xlabel("x"),ylabel("y"),zlabel("z")
az = 0;
while true
az = mod(az+1,360);
view(az, 30); % azimuth changes, elevation fixed
drawnow;
pause(0.01)
end