Code: Select all

`' Circuit:`

'

' +---(L1)---+---(L2)---+

' | | |

' | | |

' (Us) (C1) (R1)

' | | |

' | | |

' +----------+----------+

'

' Inductor: U = L * dI / dt or dI = U * dt / L

'

' Capacitor: I = C * dU / dt or dU = I * dt / C

'

' Resistor: U = I * R

'known/set values

dim as double Us = 200 'V (power supply)

dim as double L1 = 320 * 1e-6 'H

dim as double L2 = 100 * 1e-6 'H

dim as double C1 = 10 * 1e-6 'F

dim as double R1 = 1.0 'Ohm

'to be calculated / intermediate values

dim as double U_L1, U_C1, U_L2, U_R1 'V

dim as double I_L1, I_C1, I_L2, I_R1 'A

dim as double dU_L1, dU_C1, dU_L2, dU_R1 'U

dim as double dI_L1, dI_C1, dI_L2, dI_R1 'A

'simulation time settings

dim as double t = 0, tEnd = 2e-3 's

dim as double dt = (tEnd - t) / 10000 's

const W = 800, H = 600

screenres W, H, 32

width W \ 8, H \ 16

'iterate over time

while t < tEnd

'step 1, determine I_L1

U_L1 = Us - U_C1

dI_L1 = U_L1 * dt / L1

I_L1 += dI_L1

'step 2, determine U_C1

I_C1 = I_L1 - I_L2

dU_C1 = I_C1 * dt / C1

U_C1 += dU_C1

'step 3, determine U_R1

I_R1 = I_L2

U_R1 = I_R1 * R1

'step 4, determine I_L2

U_L2 = U_C1 - U_R1

dI_L2 = U_L2 * dt / L2

I_L2 += dI_L2

'plot at top: Voltages

pset((t / tEnd) * (W - 20) + 10, H * 0.4 - Us), rgb(0, 0, 255)

pset((t / tEnd) * (W - 20) + 10, H * 0.4 - U_C1), rgb(0, 255, 0)

pset((t / tEnd) * (W - 20) + 10, H * 0.4 - U_R1), rgb(255, 0, 0)

'plot at bottom: Currents

pset((t / tEnd) * (W - 20) + 10, H * 0.9 - I_L1), rgb(0, 0, 255)

pset((t / tEnd) * (W - 20) + 10, H * 0.9 - I_C1), rgb(0, 255, 0)

pset((t / tEnd) * (W - 20) + 10, H * 0.9 - I_L2), rgb(255, 0, 0)

'update time

t += dt

wend

'display what is what

draw string (10, 10), "Us (blue), U_C1 (green), U_R1 (red)"

draw string (10, H - 30), "I_L1 (blue), I_C1 (green), I_L2 (red)"

'show I_R1 end value

draw string (W \ 2, H \ 2), "I_R1 [A] = " + str(I_R1)

'wait until key press

while inkey = "" : wend