Skip to main content

Julia calculations

 



using Plots
using Printf

# Calculation of the Magnetic Field Along the Axis of a Circular Current Loop

# -------------------------------------------------------------------------
# 1. Define Constants and Parameters
# -------------------------------------------------------------------------

mu0 = 4 * pi * 1e-7  # Permeability of free space (T*m/A)
I = 1.0          # Current in the loop (Amperes) -  use floating point
a = 0.1        # Radius of the loop (meters)

# -------------------------------------------------------------------------
# 2. Define the Range of x Values (Distance along the axis)
# -------------------------------------------------------------------------

# Create a vector of distances.  Start very close to zero (1e-6).
x = range(1e-6, 0.5, length=500)  # 500 points from near-zero to 0.5 meters

# -------------------------------------------------------------------------
# 3. Calculate the Magnetic Field (Vectorized)
# -------------------------------------------------------------------------

# Calculate B for all values of x at once.  Use ./ and .^ for element-wise operations
B = (mu0 * I * a^2) ./ (2 .* (a^2 .+ x.^2).^(3/2))

# -------------------------------------------------------------------------
# 4. Display Results and Plot
# -------------------------------------------------------------------------

# Display the magnetic field at a specific point (e.g., x = 0.2 m)
x_example = 0.2
B_example = (mu0 * I * a^2) / (2 * (a^2 + x_example^2)^(3/2)) # Calculate for example
@printf("Magnetic field at x = %.2f m is: %.6e Tesla\n", x_example, B_example)


# Create the plot using the Plots package
p = plot(x, B,
    xlabel="Distance along the axis, x (meters)",
    ylabel="Magnetic Field Strength, B (Tesla)",
    title="Magnetic Field Along the Axis of a Circular Current Loop",
    label="B(x)",  # Add a label for the legend
    legend=:topright, # Place the legend
    linewidth=2,   # Set line width
    grid=true)     # Turn on the grid

# -------------------------------------------------------------------------
# 5. Calculate magnetic field strength in the center of the loop
# -------------------------------------------------------------------------
x_center = 0.0
B_center = (mu0 * I * a^2) / (2 * (a^2 + x_center^2)^(3/2))
@printf("Magnetic field at the center of the loop (x = 0) is: %.6e Tesla\n", B_center)

# -------------------------------------------------------------------------
# 6. Display or save the plot
# -------------------------------------------------------------------------

# Choose a backend that works well in the command line.
# GR is generally a good choice.  Other options include PGFPlots (for LaTeX output),
# UnicodePlots (for text-based plots), or Plotly (for interactive plots in a browser).

# --- Method 1: Using GR (recommended for command-line display) ---
gr()  # Select the GR backend
display(p)  # Display the plot
readline() #Wait for user to hit enter. Prevents closing.

# --- Method 2: Saving to a file (works with any backend) ---
# savefig(p, "magnetic_field_plot.png")  # Save as PNG
# savefig(p, "magnetic_field_plot.pdf")  # Save as PDF (good for vector graphics)

# --- Method 3: Using UnicodePlots (text-based plot) ---
# using UnicodePlots  # You might need to install this: Pkg.add("UnicodePlots")
# unicodeplots()      # Select the UnicodePlots backend
# display(p)          # This will show a text-based plot in the terminal

# --- Method 4: Using Plotly (interactive plot in browser) ---
# plotly()       # Select the Plotly backend
# display(p)      # This will open a new tab in your web browser with the plot

Popular posts from this blog

SPQR

    Here's a copy of  draft testimony by CDC Director Julie Gerberding, provided to The Atlanta Journal- Constitution by Physicians for Social Reponsibility, a Washington-based advocacy group. Areas highlighted in yellow are absent from the written testimony Gerberding filed Tuesday with the Senate Environment and Public Works Committee. alt.coxnewsweb.com/ajc/pdf/gerberding.pdf Blogged with Flock

Untitled

Blogged with Flock

Xgrid Leopard Series

With Leopard came Xgrid 2, with much improved performance and a few new features that many Xgrid users will probably find useful. Initially just one post, my article on Xgrid Leopard had grown too big, and I decided to split it into 2 posts, that are both available right now: Xgrid Leopard: the good, the bad, the ugly, and the new stuff Xgrid Leopard: Scoreboard rules! read more Blogged with Flock