Maker Pro
Maker Pro

Simulation of RLC circuit

S

Scott Ronald

Jan 1, 1970
0
Hi

To continue this discussion about simulation of incremental difference
equations, I was wondering how you could add a power source to the code
that John Larkin came up with.
I have been experimenting with this and I found that merely adding the
voltage like


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

causes problems because the voltage accumulates in the Vout variable and
the voltage quickly goes very large. Do I need to subtract the previous
voltage before I add the new voltage like:

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

Would this be remotely accurate?

Here is the circuit I am looking at:

[Input]--[coil]---|-------|----[output]--|
| | | |
| [cap] [resistor] [voltage]
| | | |
|---------|-------|--------------|
|
[ground]

Scott



simulate a really simple RLC circuit (2 network). I know that I can use
one of the freely available programs to do this, but I want to be able
change the input voltage arbitrarily with my own code using feedback
from the output of my circuit.
It seems like spice variants cannot do this, and I do not know how to create the differential equations to write my own code.

What r-l-c topology did you have in mind? It's not hard to program the
incremental difference equations.

But Spice program can do most anything.

John

I need to do something like this:

[Input]-|-[coil]-----|-----[output]
| | |
| [cap] |
| | |
-------|--------------
|
[ground]

I hope this is readable.


No resistor?

OK, assume a time step DT.


Oops, make that first line
FOR T = 0 TO 1 STEP DT

IL = IL + (Vin-Vout) * DT / L

Vout = Vout + IL * DT / C

NEXT
Hi

Wow that is great, is there a textbook somewhere that covers this technique?

How would it change if I add a resistor?

[Input]-|-[coil]-----|-------|----[output]
| | |
| [cap] [resistor]
| | |
-------|--------------
|
[ground]
 
J

John Larkin

Jan 1, 1970
0
Hi

To continue this discussion about simulation of incremental difference
equations, I was wondering how you could add a power source to the code
that John Larkin came up with.
I have been experimenting with this and I found that merely adding the
voltage like


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

causes problems because the voltage accumulates in the Vout variable and
the voltage quickly goes very large. Do I need to subtract the previous
voltage before I add the new voltage like:

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

Would this be remotely accurate?

Here is the circuit I am looking at:

[Input]--[coil]---|-------|----[output]--|
| | | |
| [cap] [resistor] [voltage]
| | | |
|---------|-------|--------------|
|
[ground]

Scott

Assuming that [output] is a high-impedance gadget, the presence of
[voltage] does not change the voltage waveform at Vout, which is what
we named the node at the top of [resistor].

So if we clarify the names and polarities...

Vout + -
Vin-------[coil]--|-------|---[Output]---|
| | | | +
| [cap] [resistor] [Voltage]
| | | | -
|---------|-------|--------------|
|
[ground]

the code becomes

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


John
 
S

Scott Ronald

Jan 1, 1970
0
Acutally I need the [output] part to be low impedance, so that it will
affect the vout node:

Vout
Vin-----|-[coil]--|-------|--------------|
| | | | +
| [cap] [resistor] [Voltage]
| | | | -
|---------|-------|--------------|
|
[ground]

Scott


John said:
Hi

To continue this discussion about simulation of incremental difference
equations, I was wondering how you could add a power source to the code
that John Larkin came up with.
I have been experimenting with this and I found that merely adding the
voltage like


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

causes problems because the voltage accumulates in the Vout variable and
the voltage quickly goes very large. Do I need to subtract the previous
voltage before I add the new voltage like:

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

Would this be remotely accurate?

Here is the circuit I am looking at:

[Input]--[coil]---|-------|----[output]--|
| | | |
| [cap] [resistor] [voltage]
| | | |
|---------|-------|--------------|
|
[ground]

Scott

Assuming that [output] is a high-impedance gadget, the presence of
[voltage] does not change the voltage waveform at Vout, which is what
we named the node at the top of [resistor].

So if we clarify the names and polarities...

Vout + -
Vin-------[coil]--|-------|---[Output]---|
| | | | +
| [cap] [resistor] [Voltage]
| | | | -
|---------|-------|--------------|
|
[ground]

the code becomes

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


John
 
E

EEdiot

Jan 1, 1970
0
Scott Ronald said:
Hi

To continue this discussion about simulation of incremental difference
equations, I was wondering how you could add a power source to the code
that John Larkin came up with.
I have been experimenting with this and I found that merely adding the
voltage like


//FOR T = 0 TO 1 STEP DT
// IL = IL + (Vin-Vout) * DT / L
// IR = Vout / R
// IC = IL - IR
// Vout = Vout + IC * DT / C +myVoltage
//NEXT
...
Here is the circuit I am looking at:

[Input]--[coil]---|-------|----[output]--|
| | | |
| [cap] [resistor] [voltage]
| | | |
|---------|-------|--------------|
|
[ground]

Let's call the voltage source you label [voltage] "Vsource"

If you hook a voltage source between output and ground, then it will
hold output the same as the voltage source. In other words, Vout =
Vsource. In terms of the program, it would look like this:


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

Note, this probably isn't what you want, and isn't very interesting.
If Vin and Vsource are unequal, then you essentially have a short
through the coil, which will produce an infinite current in the coil.
The capacitor voltage will equal Vsource, and the capacitor current
would either be 0 (for DC) or infinite (for AC).

If you assume some source resistance (call it Rsource) in series with
your voltage source, then things calm down a bit. Here's what that
would look like:


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

John Larkin

Jan 1, 1970
0
Acutally I need the [output] part to be low impedance, so that it will
affect the vout node:

Vout
Vin-----|-[coil]--|-------|--------------|
| | | | +
| [cap] [resistor] [Voltage]
| | | | -
|---------|-------|--------------|
|
[ground]

Scott


Then the entire program becomes


Vout = Voltage


John
 
Top