JP / EN
Coffee Break

Drawing Tangent Lines with SPICE

A "perverted" way to use SPICE.
Letting an electronic circuit simulator solve pure math.

1. What is the "SPICE-Oriented Approach"?

SPICE is an electronic circuit simulator, but it actually has a hidden face as a powerful "mathematical expression analysis engine."

In the field of circuit design, we use SPICE to check the amplification rate of op-amps or to observe the transient response of transistors via waveforms. However, we can also treat voltages and currents simply as "mathematical variables" and write equations directly as a circuit network to find solutions.

On this site, we call this approach of mapping pure mathematical problems onto simulator functions the "SPICE-Oriented Approach". For a quick coffee break, let me introduce a perverted way to find tangent lines using this technique.

2. The Challenge: The Tangent Line of $y = x^3 - 3x$

Let's use the following cubic function, familiar from high school math, as our target.

$$ y = x^3 - 3x $$

We want to find the "slope of the tangent line" on this curve at the points $x=1$ and $x=2$. If calculated by hand (differentiation), the derivative is $y' = 3x^2 - 3$. Substituting the respective $x$ values gives us these theoretical values:

  • Slope at $x=1$: $3(1)^2 - 3 = 0$ (It's the bottom of the valley, so the slope is zero)
  • Slope at $x=2$: $3(2)^2 - 3 = 9$

Now, let's build a "perverted netlist" to make SPICE calculate this.

3. A Netlist with a Circuit Designer's Aesthetic

When making SPICE solve equations, writing a B-source (non-linear dependent source) directly as a voltage source is the simplest way. However, this time, I added a bit of "chic" as a circuit designer and constructed it like this:

***** MacSpice Sens Test ***** * The knob to set the current position of x (set to x=1 here) Vx x 0 1 * Non-linear dependent "current source" calculating y = x^3 - 3x By 0 y I = V(x)^3 - 3 * V(x) * 1-ohm resistor to convert current to voltage Ry y 0 1 * Find the "sensitivity" of each variable to the voltage of y .SENS V(y) .END

By calculating the equation as a current source `I` and feeding it into a 1-ohm resistor `Ry`, we generate the calculation result as a voltage at node `y` using Ohm's law ($V = I \times R$). The star of this show is the `.SENS V(y)` command.

The True Identity of `.SENS` (Sensitivity Analysis)

`.SENS` is generally used for worst-case analysis, such as "Which resistor's variation has the most negative impact on the output voltage of this circuit?"

However, the true nature of the calculation happening inside is "simultaneously calculating the partial derivatives of all parameters in the circuit with respect to the specified output." SPICE uses a matrix calculation trick called the "Adjoint Network Method" to derive the partial derivatives (gradient vector) of all variables at the current DC operating point in just a single calculation.

In short, `.SENS` is a super-powerful differentiation command that outputs the "slope of the tangent line in each direction" at the current location as exact numerical values.

4. The Results: Could SPICE Solve the Math?

Let's run the above netlist ($x=1$) in MacSpice.

MacSpice 5 -> run MacSpice 6 -> print all ry = -2.00000e+00 vx = 0.00000e+00

`vx` (the sensitivity to the input $x$, meaning the slope of the tangent line) is beautifully output as `0.00000e+00`. It accurately detects that this is the bottom of the valley.

Next, let's change the first line of the netlist to `Vx x 0 2` and run it again.

MacSpice 7 -> run MacSpice 8 -> print all ry = 2.00000e+00 vx = 9.00000e+00

This time, `vx` is `9.00000e+00`. This also perfectly matches the theoretical value from our hand calculation!

5. Dr.WataWata's Insight

As you can see, SPICE is not just a circuit verification tool; it is also an excellent mathematical solver capable of instantly deriving the derivatives (gradients) of multivariable functions.

If you use the "slope of the tangent line" spit out by `.SENS` as a compass, solving complex non-linear optimization problems (like a full-solution search for equations) is not just a dream. Occasionally shifting your perspective and letting a simulator solve pure math might be the most exciting playground for an engineer.