DiffglTest15.py


1
import numpy as np
2
from scipy.integrate import odeint
3
import matplotlib.pyplot as plt
4
5
6
R = 10
7
Rdson = 1
8
C = 100e-6
9
L = 100e-6
10
iSpule = 0.00
11
uLast = 0.00
12
uDiode = 2
13
14
# function that returns dz/dt
15
def model1(z,t1,uQuelle):
16
    uLast = z[1]
17
    iSpule = z[0]
18
19
    dildt = (uQuelle-uLast-Rdson*iSpule)/L
20
    durdt = (iSpule - uLast/R)/C
21
    dzdt = [dildt,durdt]
22
    return dzdt
23
24
25
def model2(z,t2,uQuelle):
26
    uLast = z[1]
27
    iSpule = z[0]
28
    #dildt = (-R*iSpule-uDiode) / L    #schwingt nicht, Versatz vorhanden
29
    dildt = (-uLast - uDiode) / L      #schwingt, Versatz vorhanden
30
    durdt = (iSpule - uLast/R)/C
31
    dzdt = [dildt,durdt]
32
    return dzdt    
33
   
34
35
# initial condition
36
z0 = [0,0]
37
38
# number of time points
39
40
41
# time points
42
t = np.linspace(0,12000e-6,6001)
43
# step input
44
uQuelle = np.zeros(6001)
45
46
uQuelle[0:2000] = 10.00
47
48
# store solution
49
uLast = np.empty_like(t)
50
iSpule = np.empty_like(t)
51
# record initial conditions
52
uLast[0] = z0[1]
53
iSpule[0] = z0[0]
54
55
# solve ODE
56
for i in range(1,2000):
57
    # span for next time step
58
    tspan = [t[i-1],t[i]]
59
    # solve for next step
60
    z = odeint(model1,z0,tspan,args=(uQuelle[i],))
61
    # store solution for plotting
62
    uLast[i] = z[1][1]
63
    iSpule[i] = z[1][0]
64
    # next initial condition
65
    z0 = z[1]
66
67
uQuelle[2000:] = 0.00
68
69
70
for i in range(2000,6000):
71
    # span for next time step
72
    tspan = [t[i-1],t[i]]
73
    # solve for next step
74
    z = odeint(model2,z0,tspan,args=(uQuelle[i],))
75
    # store solution for plotting
76
    uLast[i] = z[1][1]
77
    iSpule[i] = z[1][0]
78
    # next initial condition
79
    z0 = z[1]
80
81
# plot results
82
plt.plot(t,uQuelle,'g:',label='uQuelle(t)')
83
plt.plot(t,uLast,'b-',label='uLast(t)')
84
plt.plot(t,iSpule,'r--',label='iSpule(t)')
85
plt.ylabel('values')
86
plt.xlabel('time')
87
plt.legend(loc='best')
88
plt.show()