# Simulating an RL circuit

S

#### Scott Ronald

Jan 1, 1970
0
Hi

I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?

I would love to see a text or something on how to write my own code like
this, anyone know of one?

Scott

A

#### [email protected]

Jan 1, 1970
0
I would love to see a text or something on how to write my own code like
this, anyone know of one?

Scott

Why? Don't you know about SPICE?

F

#### Fred Bloggs

Jan 1, 1970
0
I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?

I would love to see a text or something on how to write my own code like
this, anyone know of one?

It starts here, let's see how much you love it now:
View in a fixed-width font such as Courier.

J

#### John Larkin

Jan 1, 1970
0
Hi

I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?

I would love to see a text or something on how to write my own code like
this, anyone know of one?

Scott

There's no "fancy algorithm" involved. For a time interval dt,

for a resistor,

I = E / R

for an inductor,

I2 = I1 + E * dt / L for an inductor, where
I2 = new current, I1 = previous current

and for a capacitor,

V2 = V1 + I * dt / C

And the rest is algebra.

This is simplistic rectangular integration. There are better ways to
do it, but this isn't real bad if you keep dt small.

It's interesting to play with stuff like this. I used to simulate
steamship control systems like this [1], drive train and hull dynamics
included, first on an HP 9100 programmable calculator, then on a PDP-8
running FOCAL. But once you get a feel for the basics, and the
mathematical pathologies [2], you may as well cut over to LT Spice.

John

[1] Most big ships are direct-drive diesels these days. Steam plants
can be very efficient, but are complex and hard to maintain at sea.

[2] Almost anything, like a parallel RC, can be made to oscillate if
you pump up dt. You can do that in Spice, too.

O

#### oopere

Jan 1, 1970
0
Scott said:
Hi

I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?

I would love to see a text or something on how to write my own code like
this, anyone know of one?

Scott

If I understand the schematic correctly, Vout=0 when the switch is on.
So, you only have to compute the inductor current before going off.

The underlying algorithm is to substitute the memory elements (C, L)
with a source and a resistor. Let's see:

For the inductor:

i(n*delta_t)=1/L*integral(-infinity,n*delta_t){v(t)dt}
i(n*delta-t)=i((n-1)*delta_t)+(1/L)*V(n)*delta_t

So the inductor current is the sum of two currents: one independent on
the voltage (modeled as a current source) and one proportional to the
voltage (modeled as a resistor).

Hence, the inductor is equivalent to a parallel combination of a current
source of value i(n-1) and a resistor of value delta_t/L

Similarly for the capacitor, which will be a serial combination of
voltage source and resistor.

Best regards

Pere

S

#### [email protected]

Jan 1, 1970
0
I am trying to simulate a boost converter like this:
[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|
So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.
On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|
I can use this algorithm for the on state ( I got it here from John Larkin)
FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT
So now I just need to do the off state.
Off state:
[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|
where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?
I would love to see a text or something on how to write my own code like
this, anyone know of one?

It starts here, let's see how much you love it now:
View in a fixed-width font such as Courier.

.
.
.
. OffState:
.
.
. I0
. .---->---.
. | | Vo
. | |
. [Vin]----+--[L]---+----------+------------.
. | | |
. | C | |
. | === |
. | | [R]
. | V0 --- |
. | - |
. | | |
. | | |
. '--------------------------+------------'
.
.
.
.
.
. Vo= Vo| + Vo| + Vo|
. Vin I0 V0
.
.
. R
. -------
. RCs + 1
. Vin
. Vo| = ---------------- x ---
. Vin R s
. ------- + sL
. RCs + 1
.
.
.
. 1 I0
. Vo| = -------------- x --
. I0 1 1 s
. --- + sC + -
. sL R
.
.
.
. RLs
. ------
. R + Ls Vo
. Vo| = ---------------- x --
. V0 s
. RLs 1
. ------ + --
. R + Ls sC
.

Yes Laplace transforms and transfer functions are great if you want to
figure it out on paper, so you go ahead and do that. See if you can
beat my computer for speed.

Scott

J

#### John Larkin

Jan 1, 1970
0
I am trying to simulate a boost converter like this:
[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|
So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.
On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|
I can use this algorithm for the on state ( I got it here from John Larkin)
FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT
So now I just need to do the off state.
Off state:
[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|
where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?
I would love to see a text or something on how to write my own code like
this, anyone know of one?

It starts here, let's see how much you love it now:
View in a fixed-width font such as Courier.

.
.
.
. OffState:
.
.
. I0
. .---->---.
. | | Vo
. | |
. [Vin]----+--[L]---+----------+------------.
. | | |
. | C | |
. | === |
. | | [R]
. | V0 --- |
. | - |
. | | |
. | | |
. '--------------------------+------------'
.
.
.
.
.
. Vo= Vo| + Vo| + Vo|
. Vin I0 V0
.
.
. R
. -------
. RCs + 1
. Vin
. Vo| = ---------------- x ---
. Vin R s
. ------- + sL
. RCs + 1
.
.
.
. 1 I0
. Vo| = -------------- x --
. I0 1 1 s
. --- + sC + -
. sL R
.
.
.
. RLs
. ------
. R + Ls Vo
. Vo| = ---------------- x --
. V0 s
. RLs 1
. ------ + --
. R + Ls sC
.

Yes Laplace transforms and transfer functions are great if you want to
figure it out on paper, so you go ahead and do that. See if you can
beat my computer for speed.

Scott

Applying Laplace equations to nonlinear circuits is even slower.
That's the advantage of numerical methods like Spice; they don't care.

John

F

#### Fred Bloggs

Jan 1, 1970
0
John said:
I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?

I would love to see a text or something on how to write my own code like
this, anyone know of one?

It starts here, let's see how much you love it now:
View in a fixed-width font such as Courier.

.
.
.
. OffState:
.
.
. I0
. .---->---.
. | | Vo
. | |
. [Vin]----+--[L]---+----------+------------.
. | | |
. | C | |
. | === |
. | | [R]
. | V0 --- |
. | - |
. | | |
. | | |
. '--------------------------+------------'
.
.
.
.
.
. Vo= Vo| + Vo| + Vo|
. Vin I0 V0
.
.
. R
. -------
. RCs + 1
. Vin
. Vo| = ---------------- x ---
. Vin R s
. ------- + sL
. RCs + 1
.
.
.
. 1 I0
. Vo| = -------------- x --
. I0 1 1 s
. --- + sC + -
. sL R
.
.
.
. RLs
. ------
. R + Ls Vo
. Vo| = ---------------- x --
. V0 s
. RLs 1
. ------ + --
. R + Ls sC
.

Yes Laplace transforms and transfer functions are great if you want to
figure it out on paper, so you go ahead and do that. See if you can
beat my computer for speed.

Scott

Applying Laplace equations to nonlinear circuits is even slower.
That's the advantage of numerical methods like Spice; they don't care.

John

Not in this case, the circuits are linear. You are confusing this
situation with the case where you have to solve for zeroes of a
transcendental or something. There is nothing like that going on here,
the Laplace allows for an exact determination by evaluation of a simple
sum of hyperbolic trig functions. It will be way more precise and faster

J

#### John Larkin

Jan 1, 1970
0
John said:
I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?

I would love to see a text or something on how to write my own code like
this, anyone know of one?

It starts here, let's see how much you love it now:
View in a fixed-width font such as Courier.

.
.
.
. OffState:
.
.
. I0
. .---->---.
. | | Vo
. | |
. [Vin]----+--[L]---+----------+------------.
. | | |
. | C | |
. | === |
. | | [R]
. | V0 --- |
. | - |
. | | |
. | | |
. '--------------------------+------------'
.
.
.
.
.
. Vo= Vo| + Vo| + Vo|
. Vin I0 V0
.
.
. R
. -------
. RCs + 1
. Vin
. Vo| = ---------------- x ---
. Vin R s
. ------- + sL
. RCs + 1
.
.
.
. 1 I0
. Vo| = -------------- x --
. I0 1 1 s
. --- + sC + -
. sL R
.
.
.
. RLs
. ------
. R + Ls Vo
. Vo| = ---------------- x --
. V0 s
. RLs 1
. ------ + --
. R + Ls sC
.

Yes Laplace transforms and transfer functions are great if you want to
figure it out on paper, so you go ahead and do that. See if you can
beat my computer for speed.

Scott

Applying Laplace equations to nonlinear circuits is even slower.
That's the advantage of numerical methods like Spice; they don't care.

John

Not in this case, the circuits are linear. You are confusing this
situation with the case where you have to solve for zeroes of a
transcendental or something. There is nothing like that going on here,
the Laplace allows for an exact determination by evaluation of a simple
sum of hyperbolic trig functions. It will be way more precise and faster

Hey, we could have a contest. Someone could post a modestly complex
circuit, like the boost regulator example here, in a realistic
setting, say closed-loop at some load, discontinuous to make it
interesting, and ask for, say, all the waveforms. Let's include some
reasonable fet and diode models. At some agreeed time, someone could
post the problem, and I could Spice it while you work it out
analytically. The group could evaluate the results for
time-to-complete, usefulness, and accuracy.

Sort of like John Henry and the Machine.

John

F

#### Fred Bloggs

Jan 1, 1970
0
John said:
John said:
On Wed, 5 Mar 2008 11:55:16 -0800 (PST), "[email protected]"

I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state and
one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the moment
the switch is opened. The only way i know how to do this is use the
exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone here write
the fancy algorithm like above that makes it more efficient for code?

I would love to see a text or something on how to write my own code like
this, anyone know of one?

It starts here, let's see how much you love it now:
View in a fixed-width font such as Courier.

.
.
.
. OffState:
.
.
. I0
. .---->---.
. | | Vo
. | |
. [Vin]----+--[L]---+----------+------------.
. | | |
. | C | |
. | === |
. | | [R]
. | V0 --- |
. | - |
. | | |
. | | |
. '--------------------------+------------'
.
.
.
.
.
. Vo= Vo| + Vo| + Vo|
. Vin I0 V0
.
.
. R
. -------
. RCs + 1
. Vin
. Vo| = ---------------- x ---
. Vin R s
. ------- + sL
. RCs + 1
.
.
.
. 1 I0
. Vo| = -------------- x --
. I0 1 1 s
. --- + sC + -
. sL R
.
.
.
. RLs
. ------
. R + Ls Vo
. Vo| = ---------------- x --
. V0 s
. RLs 1
. ------ + --
. R + Ls sC
.

Yes Laplace transforms and transfer functions are great if you want to
figure it out on paper, so you go ahead and do that. See if you can
beat my computer for speed.

Scott

Applying Laplace equations to nonlinear circuits is even slower.
That's the advantage of numerical methods like Spice; they don't care.

John

Not in this case, the circuits are linear. You are confusing this
situation with the case where you have to solve for zeroes of a
transcendental or something. There is nothing like that going on here,
the Laplace allows for an exact determination by evaluation of a simple
sum of hyperbolic trig functions. It will be way more precise and faster

Hey, we could have a contest. Someone could post a modestly complex
circuit, like the boost regulator example here, in a realistic
setting, say closed-loop at some load, discontinuous to make it
interesting, and ask for, say, all the waveforms. Let's include some
reasonable fet and diode models. At some agreeed time, someone could
post the problem, and I could Spice it while you work it out
analytically. The group could evaluate the results for
time-to-complete, usefulness, and accuracy.

Sort of like John Henry and the Machine.

John

Wow- what fun!!!

Do you see the error in here:
View in a fixed-width font such as Courier.

J

#### John Larkin

Jan 1, 1970
0
Oh you think there's a programming error or two in there, pea-brain?

Run the code and see.

John

S

#### Scott Ronald

Jan 1, 1970
0
Scott said:
John said:
Hi

I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state
and one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John
Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What I
need to figure out is the voltage at top of the load/cap is the
moment the switch is opened. The only way i know how to do this is
use the exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone
here write the fancy algorithm like above that makes it more
efficient for code?

I would love to see a text or something on how to write my own code
like this, anyone know of one?

Scott

There's no "fancy algorithm" involved. For a time interval dt,
for a resistor,

I = E / R

for an inductor,

I2 = I1 + E * dt / L for an inductor, where
I2 = new current, I1 = previous current

and for a capacitor,

V2 = V1 + I * dt / C

And the rest is algebra.

This is simplistic rectangular integration. There are better ways to
do it, but this isn't real bad if you keep dt small.

It's interesting to play with stuff like this. I used to simulate
steamship control systems like this [1], drive train and hull dynamics
included, first on an HP 9100 programmable calculator, then on a PDP-8
running FOCAL. But once you get a feel for the basics, and the
mathematical pathologies [2], you may as well cut over to LT Spice.

John

[1] Most big ships are direct-drive diesels these days. Steam plants
can be very efficient, but are complex and hard to maintain at sea.

[2] Almost anything, like a parallel RC, can be made to oscillate if
you pump up dt. You can do that in Spice, too.

Hi John

Here is a more complex circuit and the code to simulate it to see if I
am understanding this correctly.

[V1]
[Vin]-----[L1]------|--------[L2]--------|------|---[Vout]
| | |
[R1] [R2] [R3]
| | |
[C1] [C2] |
| | |
|------------|-------|------|
|
[gnd]

FOR T = 0 TO 1 STEP DT
IL1 = IL1 + (Vin-V1)*DT/L1
IR3 = Vout/R3
IR1 = IL1 - IL2
IL2 = IL2 + (V1-Vout)*DT/L2
IR2 = IL2 - IR3
V1 = V1 - IR1*DT/C1 - IR1*R1
Vout = Vout - IR2*DT/C2 - IR2*R2
NEXT

Scott

Hi

I am pretty sure i screwed up a couple things there

I need a voltage node in between the resistor cap pairs, and I screwed
up my signs for last 2 lines of the loop.

So it should be more like:

FOR T = 0 TO 1 STEP DT
IL1 = IL1 + (Vin-V1)*DT/L1
IR3 = Vout/R3
IR1 = IL1 - IL2
IL2 = IL2 + (V1-Vout)*DT/L2
IR2 = IL2 - IR3
VC1 = VC1 + IR1*DT/C1
VC2 = VC2 + IR2*DT/C2
V1 = VC1 + IR1*R1
Vout = VC2 + IR2*R2
NEXT

I assume that the order of the calculations in the loops is fairly
unimportant if you use a very small DT, is this correct?

Can anyone check the code for correctness?

Scott

F

#### Fred Bloggs

Jan 1, 1970
0
John said:
Run the code and see.

John

There's nothing wrong with /my/ code, you smoke blowing ass, but the

F

#### Fred Bloggs

Jan 1, 1970
0
Scott said:
Scott said:
John said:
Hi

I am trying to simulate a boost converter like this:

[Vin]-------[inductor]---|----------[diode]----|-------[output]
| | |
| [control]--[mosfet] [capacitor]
| | |
|----------------------|---------------------|

So I was thinking of splitting into 2 sections: one for the on state
and one for the off-state.

On State:
[Vin]-------[inductor]-----|---------------------|------------|
| | | |
| / | |
|------------------------|---------------------|------------|

I can use this algorithm for the on state ( I got it here from John
Larkin)

FOR T = 0 TO 1 STEP DT
IL = IL + (Vin-Vout) * DT / L
IR = Vout / R
IC = IL - IR
Vout = Vout + IC * DT / C
NEXT

So now I just need to do the off state.

Off state:

[Vin]-------[inductor+inductorResistance]-----|
| |
| |
|-------------------------------------------|

where the inductor charges for the time the switch is closed. What
I need to figure out is the voltage at top of the load/cap is the
moment the switch is opened. The only way i know how to do this is
use the exponential charging function.Vf=Vi(1-e^[-t/T]) Can anyone
here write the fancy algorithm like above that makes it more
efficient for code?

I would love to see a text or something on how to write my own code
like this, anyone know of one?

Scott

There's no "fancy algorithm" involved. For a time interval dt,
for a resistor,

I = E / R

for an inductor,

I2 = I1 + E * dt / L for an inductor, where
I2 = new current, I1 = previous current

and for a capacitor,

V2 = V1 + I * dt / C

And the rest is algebra.

This is simplistic rectangular integration. There are better ways to
do it, but this isn't real bad if you keep dt small.

It's interesting to play with stuff like this. I used to simulate
steamship control systems like this [1], drive train and hull dynamics
included, first on an HP 9100 programmable calculator, then on a PDP-8
running FOCAL. But once you get a feel for the basics, and the
mathematical pathologies [2], you may as well cut over to LT Spice.

John

[1] Most big ships are direct-drive diesels these days. Steam plants
can be very efficient, but are complex and hard to maintain at sea.

[2] Almost anything, like a parallel RC, can be made to oscillate if
you pump up dt. You can do that in Spice, too.

Hi John

Here is a more complex circuit and the code to simulate it to see if I
am understanding this correctly.

[V1]
[Vin]-----[L1]------|--------[L2]--------|------|---[Vout]
| | |
[R1] [R2] [R3]
| | |
[C1] [C2] |
| | |
|------------|-------|------|
|
[gnd]

FOR T = 0 TO 1 STEP DT
IL1 = IL1 + (Vin-V1)*DT/L1
IR3 = Vout/R3
IR1 = IL1 - IL2
IL2 = IL2 + (V1-Vout)*DT/L2
IR2 = IL2 - IR3
V1 = V1 - IR1*DT/C1 - IR1*R1
Vout = Vout - IR2*DT/C2 - IR2*R2
NEXT

Scott

Hi

I am pretty sure i screwed up a couple things there

I need a voltage node in between the resistor cap pairs, and I screwed
up my signs for last 2 lines of the loop.

So it should be more like:

FOR T = 0 TO 1 STEP DT
IL1 = IL1 + (Vin-V1)*DT/L1
IR3 = Vout/R3
IR1 = IL1 - IL2
IL2 = IL2 + (V1-Vout)*DT/L2
IR2 = IL2 - IR3
VC1 = VC1 + IR1*DT/C1
VC2 = VC2 + IR2*DT/C2
V1 = VC1 + IR1*R1
Vout = VC2 + IR2*R2
NEXT

I assume that the order of the calculations in the loops is fairly
unimportant if you use a very small DT, is this correct?

Can anyone check the code for correctness?

Scott

It looks right this time. Instead of wasting your time on hypotheticals,
solve for the circuit below, assume diode is ideal:

ON state means switch closed
OFF state means switch open
switching time instantaneous
ON to ON +OFF time ratio =D , duty cycle parameter
Outputs of interest:
Is source current
Ir ripple current
Vout output voltage

View in a fixed-width font such as Courier.

J

#### John Larkin

Jan 1, 1970
0
There's nothing wrong with /my/ code, you smoke blowing ass, but the

It would either crash with a runtime error, or it would corrupt
memory. Depends on the compiler.

But kluge?

John

J

#### Jim Thompson

Jan 1, 1970
0
It would either crash with a runtime error, or it would corrupt
memory. Depends on the compiler.

But kluge?

John

Now, children! Kiss and make up ;-)

...Jim Thompson

J

#### John Larkin

Jan 1, 1970
0
Geeze Jim! No one should EVER have to kiss Bloggs. even at gunpoint.

You said it. Put me right off my feed, it did.

Hey, if

R = 50

C = 1 uF

L = 2.5 mH

---------+---------R-----------C--------+---------
| |
| |
| |
+---------R-----------L--------+

then this looks like a 50 ohm resistor at all frequencies.

That has some serious possibilities.

John

J

#### Jim Thompson

Jan 1, 1970
0
You said it. Put me right off my feed, it did.

Hey, if

R = 50

C = 1 uF

L = 2.5 mH

---------+---------R-----------C--------+---------
| |
| |
| |
+---------R-----------L--------+

then this looks like a 50 ohm resistor at all frequencies.

That has some serious possibilities.

John

John, Are you just discovering that trick?

In general: R*C = L/R

I was using that to make inductive drivers for modems ~1977.

But it dates MANY years back to GenRad bridge designs.

...Jim Thompson

J

#### John Popelish

Jan 1, 1970
0
John said:
Hey, if

R = 50

C = 1 uF

L = 2.5 mH

---------+---------R-----------C--------+---------
| |
| |
| |
+---------R-----------L--------+

then this looks like a 50 ohm resistor at all frequencies.

That has some serious possibilities.

A filter with a perfectly constant input impedance?

I think it works for any LC pair with that ratio of values
and 50 ohm resistors.

J

#### Jim Thompson

Jan 1, 1970
0
A filter with a perfectly constant input impedance?

I think it works for any LC pair with that ratio of values
and 50 ohm resistors.

Doesn't have to be 50 Ohms.

See my other post... as long as: R*C == L/R

...Jim Thompson

Replies
4
Views
2K
M
Replies
1
Views
825
JeffM
J
Replies
1
Views
1K
Replies
0
Views
2K
Replies
1
Views
2K