<jupyter_start><jupyter_text>Code for Visualizing Vector FieldsImporting numpy and matplotlib.<jupyter_code>################################################################

import numpy as np
import matplotlib.pyplot as plt

################################################################<jupyter_output><empty_output><jupyter_text>Define your variables. Defaults are currently in place.Read comments for some notes on formatting.<jupyter_code>################################################################

# choose plot title
title = 'Vector Field Computer Activity'

# choose grid size (N x M) keep square and even for best results
userN = 20
userM = 20

#choose number of charges
num = 3

# choose charges: 1 for outward arrows, -1 for inward arrows
# write as q = [q1, q2, .. , qN]
q = [1, -1, 1]

# choose coordinates of charges
# write as x = [x1, x2, .. , xN] and y = [y1, y2, .. , yN]
x = [4, 2, 5]
y = [4, 3, 6]

################################################################<jupyter_output><empty_output><jupyter_text>This code creates the coordinate plane, charge points, and field arrows.<jupyter_code>################################################################

N = int(userN / 2)
M = int(userM / 2)

# setting up coordinates
X = np.arange(0, N, 1)
Y = np.arange(0, M, 1)
X, Y = np.meshgrid(X, Y)

# strength of the field 
Ex = np.zeros((N, M))
Ey = np.zeros((N, M))

# computing
Q = [[], []]  # to store charges coordinates in loop

for n in range(0, num): 
    
    qq = q[n]
    qx, qy = x[n], y[n]
    
    print(qq, qx, qy)
    
    Q[0].append(qy)
    Q[1].append(qx)
    
    for i in range(N):
        for j in range(M):
            denom = ((i - qx) ** 2 + (j - qy) ** 2) ** 1.5
            if denom != 0: 
                Ex[i, j] += qq * (j - qy) / denom
                Ey[i, j] += qq * (i - qx) / denom
                
################################################################<jupyter_output>1 4 4
-1 2 3
1 5 6<jupyter_text>This last bit colors and normalizes the field, and prints the image.Picture should appear below.<jupyter_code>################################################################

# making arrows different colors, color determined by magnitude
C = np.hypot(Ex, Ey)

# normalizing: making sure arrows are the same length
E = (Ex ** 2 + Ey ** 2) ** .5
Ex = Ex / E
Ey = Ey / E

# drawing the plot
plt.figure(figsize = (userN/2, userM/2))
plt.plot(*Q, 'ko') # draws point charges
plt.quiver(X, Y, Ex, Ey, C) # draws arrows
plt.title(title)
plt.axis('equal')
plt.axis('on')
plt.show()

################################################################<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.30518742900416507, 'fraction_numerical': 0.01893222264293828, 'mean_word_length': 3.874538745387454, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '190205', 'license': 'MIT', 'n_tokens_mistral': 901, 'n_tokens_neox': 832, 'n_words': 278, 'path': 'vector_fields.ipynb', 'repo_name': 'villano-lab/analytical_mechanics', 'size': '2641'}