Hi Electronman,
If you look at the inductor's magnetic field you can see what happens.
When the inductor is fed a current, it's field builds up in one direction.
When the switch is opened, the field starts to collapse. The collapsing
field produces a voltage of opposite polarity to what the voltage *was*
before the switch opened.
The inductor becomes a power source, but it is often looked at as a current
source in order to simplify the calculations, and a perfect inductor works
very much like that anyway.
Here's what happens when we view the inductor as a current source as the
switch is opened...
A current source puts out constant current, such as 1 amp for example, and
the switch starting to open looks like a resistance say 0.01 ohms. 1 amp
through 0.01 ohms is not very much voltage, so the voltage is only 0.01 volts
for now, but the switch opens more and more as time goes by (this happens
gradually but it appears to happen instantaneously). As the switch opens more,
it's resistance might go up to 0.1 ohms, and now that same 1 amp means there
is 0.1 volts across the switch. A tiny fraction of a second later, the switch opens
more, and now maybe it has 1 ohm resistance, and so the voltage goes up to 1 volt.
Already the voltage has increased from 0.01 volts to 1 volt, but the switch opens
even more now, maybe the resistance goes up to 10 ohms, so now we end up with
10 volts across the switch. It doesnt stop there though. The resistance goes up
to 100 ohms, then 1000 ohms, then 10k, then 100k, then 1 megohm, then 10 megs,
then 100megs, then possibly even higher. As this happens, the voltage goes up
accordingly, to 100 volts, then 1000 volts, then even higher if the inductor's wire
insulation does not break down, but then something else starts to happen: the
voltage is up so high that the inductor can expel it's total energy in a very short
time, so by the time the switch is open fully all of the energy might be dissipated
and the voltage falls back down to zero (or oscillates if there is any self capacitance
in the inductor, which there usually is at least some).
It's also interesting to note the polarity of the voltage across the inductor while
all this is taking place as you already noticed...
As the inductor is charged from a source like a battery, the polarity would be positive
at the battery's positive terminal which say is wired to the switch. That means
the inductor terminal connected to the switch is positive as it is being charged.
As the switch opens however, the current flows through the switch and produces
a negative voltage at the switch instead of positive (it initially stays positive
because the resistance of the switch does not rise instantaneously, but it happens
so fast we often talk about it going negative as soon as the switch is opened).
In this way we end up with first a positive voltage at the switch and then a negative
voltage at the switch (the terminal that connects to the inductor).
So the real cause of the change in voltage polarity is because the field changes polarity,
but we can ignore that extra information if we think of the inductor as a current
source who's current flows in the same direction even when the switch is opened.
If you look at a circuit like this but replace the switch with a resistor of some value
like 100 ohms and keep the direction of current flow in the inductor the same as it
was just before opening the 'switch', you will see that the voltage across the
100 ohm resistor changes polarity, and so does the voltage across the inductor.
This is a principle that is used in many boost converters, to get a higher voltage from
a lower voltage using an inductor as the storage/converter element.
For driving relays however, this action works against us because the voltage may
become so high that it blows out the driving element (transistor), or starts to break
down the winding insulation and eventually causes a short in the coil. For this reason
we often use a diode to 'catch' that spike and shunt it so that the voltage can not
build up. A diode clamps the voltage to about 0.7 to 1.0 or so volts, so that's a
lot lower than 1000 volts which would blow out a regular 40v transistor.
Also, sometimes more than one diode is used in series to get that 'spike' to
subside more quickly. The level of the voltage across the coil has a profound effect
on the time it takes to dissipate the energy in that coil, and a higher voltage means
it dissipates faster, so sometimes a zener is used which will clamp the voltage to
some safe level like 10 volts which will not harm a regular 40v transistor.