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

evergreen post

March 2025: This seems to be a common occurence on   /. lately. I got into the same sort of interaction with someone a few weeks back. He just wanted to argue with everything anyone said. And then you go look at his history and that seems to be all he could do. And each time it was the same format. At least the guy arguing with me didn't 'break' once he couldn't argue anymore. That guy would just keep going forever, even if you said the sky was blue, he was going to argue about it. ===  # Common Discord These verbal sparring matches plague our forums now, Where men of endless argument hold court. With every comment met by furrowed brow, And each response a chance for sharp retort. I crossed such paths with one disputant keen, Whose history revealed a single art: Contention was his only routine scene, Each statement challenged from the very start. Some warriors yield when logic brings defeat, But he fought on with unrelenting might. If "skies are blue" should ...