Magnetic Levitation Stability Back
Electromagnetics & Control

Magnetic Levitation Stability Simulator

Simulate electromagnetic levitation with real-time PID control. Tune coil parameters, air gap, mass and PID gains to explore closed-loop step response and the stability of this inherently unstable plant.

Electromagnet Parameters
Object Parameters
PID Gains
Linearized Coefficients
Theory
Results
Instability coefficient kx
N/m
Control gain ki
N/A
— Not calculated —
Position x(t) — step response (0-2 s)
Magnetic levitation animation
Final position error
mm
Key Formulas
F = μ₀N²AI²/(4x²)
kₓ = 2F/x₀ (destabilizing)
kᵢ = 2F/I₀ (control sensitivity)

What is Magnetic Levitation Stability?

🙋
What exactly is the big deal about magnetic levitation being "inherently unstable"? Can't you just use a magnet to repel another magnet and have it float?
🎓
Basically, that's a common misconception! For static, permanent magnets, it's mathematically impossible to achieve stable levitation in all directions—this is Earnshaw's theorem. The core problem is the force law: for an electromagnet attracting a steel ball, the force increases as the gap shrinks ($F \propto 1/x^2$). If the ball drops a bit, the pull gets weaker, so it falls more. If it rises, the pull gets stronger, yanking it up. It's pure positive feedback. Try moving the "Gap" slider in the simulator above and watch how the "Force" value changes dramatically.
🙋
Wait, really? So how do real maglev trains work? They must be stabilizing it somehow.
🎓
Exactly! They use active, closed-loop control. Sensors constantly measure the gap, and a computer adjusts the electromagnet's current millions of times a second to correct any drift. That's what the PID controller in this simulator does. In practice, you're fighting physics with fast electronics. For instance, if you set the Proportional Gain (Kp) too high in the simulator, the system will oscillate wildly. Too low, and the ball will sluggishly drift and fall.
🙋
So the "Mass" and "Coil Constant" sliders matter too? I thought it was all about tuning the PID gains.
🎓
Great question! They define the physical plant the controller has to manage. A heavier mass (like a larger steel sphere) has more inertia and requires more force to move. A higher coil constant means your electromagnet is stronger, so the same current produces a bigger force. Changing these is like changing the car you're trying to drive—you have to re-tune the controller (the PID gains) for a smooth ride. Try doubling the mass in the simulator without changing the gains; you'll see the ball becomes much harder to control.

Physical Model & Key Equations

The fundamental challenge is balancing the electromagnetic force against gravity. The attractive force from the coil on the ferromagnetic ball is approximately inversely proportional to the square of the air gap.

$$F_{mag}= k \frac{I^2}{x^2}$$

Where $F_{mag}$ is the magnetic force (N), $k$ is the Coil Constant (N·m²/A²) set by the electromagnet's design, $I$ is the coil current (A), and $x$ is the Gap distance (m) from the coil to the ball. This nonlinear relationship is the source of the instability.

The system is stabilized by a PID controller, which calculates the required coil current based on the error between the desired gap and the measured gap.

$$I(t) = K_p e(t) + K_i \int_0^t e(\tau) d\tau + K_d \frac{de(t)}{dt}$$

Where $e(t) = x_{desired}- x_{actual}$ is the gap error. $K_p$ (Proportional Gain) reacts to present error, $K_i$ (Integral Gain) eliminates steady-state offset by accumulating past error, and $K_d$ (Derivative Gain) predicts future error by looking at its rate of change, adding damping. Tuning these three gains in the simulator is the key to achieving a fast, stable, and accurate levitation.

Frequently Asked Questions

First, gradually increase the proportional gain Kp to pull it toward the target gap, then adjust the derivative gain Kd to suppress oscillation. Add the integral gain Ki starting from a small value to eliminate any remaining steady-state error. A good starting point is to try Kp=500, Kd=50, and Ki=1.
The attractive force Fem is inversely proportional to the square of the gap x, so the force increases nonlinearly as x decreases. This characteristic causes unstable behavior: when the levitating object gets too close to the electromagnet, it is suddenly pulled in, and when it moves away, the force weakens, causing it to fall. PID control is necessary to counteract this.
Yes, it is necessary. A larger mass slows the response, so Kp and Kd tend to need larger values. Changing the target gap alters the influence of nonlinearity, so the optimal gains also change. After modifying parameters, always check stability and readjust as needed.
Yes, it can be used for basic stability analysis and initial PID gain exploration. However, in real hardware, non-ideal factors such as coil saturation, response delays, and sensor noise will have an impact. Therefore, treat simulation results as a guideline, and fine-tuning on the actual device is necessary.

Real-World Applications

Maglev Transportation: High-speed trains like Japan's SCMaglev use superconducting electromagnets and sophisticated control systems to levitate and propel trains at speeds over 600 km/h, eliminating wheel-rail friction for unparalleled efficiency and smoothness.

Contactless Bearings in Vacuum & Cleanrooms: Magnetic levitation is used for frictionless bearings in high-speed centrifuges, flywheel energy storage, and semiconductor manufacturing equipment, where physical contact would cause contamination or wear.

Magnetic Levitation for Display & Demonstration: Popular science exhibits and luxury product displays (like levitating speakers or planters) use small, controlled electromagnets to create the striking visual effect of objects floating in mid-air.

Advanced Material Processing: In containerless processing, materials are levitated to be melted and solidified without touching a crucible, which can introduce impurities or cause unwanted crystallization. This is used in metallurgy and pharmaceutical research.

Common Misconceptions and Points to Note

First, it is a dangerous misconception to think that "the system will stabilize if you just make the P-gain strong enough". It's true that increasing Kp strengthens the restoring force toward the target position. However, if it's too strong, the levitating object will overshoot the target and be pushed hard to the opposite side, where a strong force in the reverse direction acts... This cycle repeats, causing severe oscillation. For example, try running a simulation with Kp at its maximum and Kd near zero. The graph will show wild instability, and in reality, the object would certainly collide with the electromagnet. The key to stabilization lies in the balance between Kp (restoring force) and Kd (damping force).

Next, note that starting parameter tuning from the 'mass' often leads to failure. First, fix the mass to a standard value and use the 'coil turns' and 'pole area' to ensure a rough estimate of the static attractive force needed for levitation. For instance, if you double the mass, you need to increase the turns N or area A (or raise the initial current) to more than double the attractive force. If this "static balance" isn't achieved, tweaking the PID gains won't even allow levitation to occur in the first place.

Finally, it's crucial to understand the decisive gap between the simulator and a real machine. This tool uses an ideal "single degree of freedom" model. However, in a real device, significant issues arise from the levitating object's "rotation (pitch/roll)", "sensor measurement noise", and "the limited response speed of the amplifier generating the control current". Gains perfectly tuned in the simulator are not guaranteed to work directly on the actual hardware. In practice, the golden rule is to use the simulation for rough design and then start with much smaller gains on the real machine, increasing them cautiously.

How to Use

  1. Set coil parameters: enter number of turns (nCoil, typically 500–2000) and initial current i0 (0.5–2.0 A) for your electromagnet.
  2. Define gap geometry: specify air gap x0 (5–20 mm) and pole area poleA (0.01–0.05 m²) to establish magnetic force characteristics.
  3. Input mass and PID gains: set levitated mass, then adjust proportional (Kp), integral (Ki), and derivative (Kd) terms; run simulation to observe step response overshoot and settling time.
  4. Analyze stability: monitor closed-loop poles and gain margin; iterate gains until response settles within ±2 mm of setpoint in under 500 ms.

Worked Example

A 1.2 kg steel ball levitated in an 800-turn solenoid with initial gap x0 = 12 mm and pole area poleA = 0.015 m². At i0 = 1.0 A, the magnetic force balances weight. Tuning PID with Kp = 8.5, Ki = 1.2, Kd = 0.35 produces a 0.5 mm undershoot and 280 ms settling time when a 50 mm step input (new setpoint) is applied. Reducing Kd to 0.25 increases overshoot to 3.8 mm; increasing to 0.45 causes sluggish response with 600 ms settling.

Practical Notes

  1. Magnetic force nonlinearity (proportional to i²/x²) demands aggressive integral action at large gaps; use Ki = 0.8–1.5 for gaps exceeding 15 mm to prevent steady-state error.
  2. Coil saturation occurs above ~2.2 A in ferrite cores; respect this limit or linearization assumptions fail and the system becomes unstable.
  3. Sensor noise and 50 Hz electromagnetic interference require derivative filtering; set Kd low (0.2–0.4) and add a 10–20 Hz low-pass cutoff to avoid chatter.
  4. For rapid prototyping, start with Kp ≈ 10×(magnetic stiffness), then reduce by 30–40% and tune Ki and Kd incrementally.