Simulating an RL circuit

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]-----|---------------------|------------| | | | | | / [capacitor] [Load] | / | | |------------------------|---------------------|------------|

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

Reply to
Scott Ronald
Loading thread data ...

Why? Don't you know about SPICE?

Reply to
a7yvm109gf5d1

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 .

Reply to
Fred Bloggs

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.
Reply to
John Larkin

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

Reply to
oopere

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

Reply to
scottm361

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

John

Reply to
John Larkin

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 than your integration...

Reply to
Fred Bloggs

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

Reply to
John Larkin

John Larkin wrote:

Wow- what fun!!!

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

. . . . OffState: . . . . I . -> Vo . . .---------[L]--------------+------------. . | | | . | C | + | . | === | . Vin | - [R] . | | | . | | | . | | | . | | | . '--------------------------+------------' . . . . . dI . 1) Vin= L -- + Vo . dt . . dVo Vo . 2) I= C --- + -- . dt R . . 2 . d Vo L dVo . 1) & 2) -> Vin= LC ---- + - ---- + Vo . 2 R dt . d t . . . 2 . d Vo Vin 1 dVo 1 . Re-arranging -> ---- = --- - --- --- - -- Vo . 2 LC RC dt LC . d t . . t . dVo Vin 1 / Vo dVo . -> --- = --- t - --(Vo-Vo(0)) - | (--)dt + ---(0) . dt LC RC / LC dt . 0 . . . t t . Vin 2 1 / // Vo 2 1 dVo . -> Vo = ---t - -- | Vo dt- || -- dt + --Vo(0)t + ---(0)t+Vo(0) . 2LC RC / // LC RC dt . 0 0 . . t t t . / // 2 / . Then define V1= | Vo dt and V2= || Vo dt = | V1 dt . / // / . 0 0 0 . . Vin 2 1 1 t dVo . making Vo= ---t - -- V1- -- V2 + (-- + 1)Vo(0) + ---(0)t . 2LC RC LC RC dt . . . dVo I(0) Vo(0) . 2) -> ---(0) = ---- - ----- . dt C RC . . . . This then reduces to a simple iteration: . . Vo=Vozero . . DVozero=(Izero-Vozero/R)/C . . V1=0 . . V2=0 . . K1=1/(L*C) . . K2=0.5*K1*Vin . . K3=1/(R*C) . . K4=K3*Vozero . . TI=( integration time interval) . . N= (number of integration points) . . DT=TI/N . . DIM VO(1;N) . . T=0 . . FOR I= 1 to N . . T=T+DT . . V1= Vo*DT + V1 . . V2= V1*DT + V2 . . Vo=(K2*T+K4+DVozero)*T-K3*V1-K1*V2+Vozero . . V0(I)=Vo . . NEXT I . . END . .

Reply to
Fred Bloggs

You mean, aside from the obvious programming errors?

John

Reply to
John Larkin

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

Reply to
Scott Ronald

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

Reply to
Fred Bloggs

Run the code and see.

John

Reply to
John Larkin

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

Reply to
Scott Ronald

There's nothing wrong with /my/ code, you smoke blowing ass, but the same can't be said for your really bad kluge...

Reply to
Fred Bloggs

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.

. . . . . Is . -> VD . Vout . .--[L]-[ESR1]-+--|>|-----+---------. . | | | |Ir | . | | CF| v | . Vin--- .----+ === | . - | | | | . | Cos| | [ESL] [R] . | === o | | . | | / [ESR2] | . | | o | | . | | | | | . '--------+----+----------+---------' . . . .

Reply to
Fred Bloggs

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

But kluge?

John

Reply to
John Larkin

Now, children! Kiss and make up ;-)

...Jim Thompson

--
|  James E.Thompson, P.E.                           |    mens     |
|  Analog Innovations, Inc.                         |     et      |
|  Analog/Mixed-Signal ASIC's and Discrete Systems  |    manus    |
|  Phoenix, Arizona            Voice:(480)460-2350  |             |
|  E-mail Address at Website     Fax:(480)460-2142  |  Brass Rat  |
|       http://www.analog-innovations.com           |    1962     |
             
         America: Land of the Free, Because of the Brave
Reply to
Jim Thompson

Geeze Jim! No one should EVER have to kiss Bloggs. even at gunpoint. :(

--
Service to my country? Been there, Done that, and I\'ve got my DD214 to
prove it.
Member of DAV #85.

Michael A. Terrell
Central Florida
Reply to
Michael A. Terrell

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.