aUCBLogo Demos and Tests / electronic_sim
be electronic_sim
xy
end
be XYGraph frm width height ~
xmin xmax ymin ymax ticksx ticksy mode
local [g mx my mw mh x0 y0 w0 h0 zx zy zx0 zy0 p0 p pz
mouseleft mouseright inmotion c
xa ya xxa yya
xmin0 xmax0 ymin0 ymax0
maxx maxy]
g=(Graph frm
wxDefault_Frame_Style+wxFull_Repaint_on_Resize ;+wxStay_on_Top
[0 0] List width height [Graph])
;g=[]
setUpdateGraph false
hideTurtle
mx=0
my=0
x0=0
y0=0
mw=0
mh=0
w0=0
h0=0
zx=1
zy=1
zx0=1
zy0=1
c=0
p0=[0 0 0]
p=[0 0 0]
pz=[0 0 0]
mouseleft=false
mouseright=false
inmotion=false
yminh=ymax
ymax=ymin
ymin=yminh
xminh=xmax
xmax=xmin
xmin=xminh
xmin0=xmin
xmax0=xmax
ymin0=ymin
ymax0=ymax
maxx=330
maxy=250
be init
WindowOnLeftDown g
[
if not mouseleft
[ GraphSetCurrent g
p0=MousePos+p
x0=p0.1+mx
y0=p0.2+my
mouseleft=true
]
]
WindowOnLeftUp g
[
mouseleft=false
GraphSetCurrent g
p0=p0-MousePos+p
mx=p.1
my=p.2
]
WindowOnRightDown g
[
if not mouseright
[ GraphSetCurrent g
p0=MousePos+pz
w0=p0.1+mw
h0=p0.2+mh
mouseright=true
]
]
WindowOnRightUp g
[
mouseright=false
GraphSetCurrent g
p0=p0-MousePos+pz
mw=p.1
mh=p.2
zx0=zx0*(1+mw/800)
zy0=zy0*(1+mh/600)
]
;comment [
WindowOnMotion g
[
if mouseleft and2 not inmotion
[ GraphSetCurrent g
p=p0-MousePos
mx=p.1
my=p.2
xmin=(xmin0+xmax0)/2-(xmax0-xmin0)/2*zx-mx/maxx*(xmax0-xmin0)
xmax=(xmin0+xmax0)/2+(xmax0-xmin0)/2*zx-mx/maxx*(xmax0-xmin0)
ymin=(ymin0+ymax0)/2-(ymax0-ymin0)/2*zy-my/maxy*(ymax0-ymin0)
ymax=(ymin0+ymax0)/2+(ymax0-ymin0)/2*zy-my/maxy*(ymax0-ymin0)
clearScreen
drawplot
updateGraph
]
if mouseright and2 not inmotion
[ GraphSetCurrent g
pz=p0-MousePos
mw=pz.1
mh=pz.2
zx=zx0*(1+mw/800)
zy=zy0*(1+mh/600)
xmin=(xmin0+xmax0)/2-(xmax0-xmin0)/2*zx-mx/maxx*(xmax0-xmin0)
xmax=(xmin0+xmax0)/2+(xmax0-xmin0)/2*zx-mx/maxx*(xmax0-xmin0)
ymin=(ymin0+ymax0)/2-(ymax0-ymin0)/2*zy-my/maxy*(ymax0-ymin0)
ymax=(ymin0+ymax0)/2+(ymax0-ymin0)/2*zy-my/maxy*(ymax0-ymin0)
clearScreen
drawplot
updateGraph
]
]
;]
end
be plot xa_ ya_ c_
xa=xa_
ya=ya_
c=c_
drawplot
end
be drawplot
local [x y y2 i h w hq wq hx hy rx ry]
WindowMode
setPenSize [.5 .5]
setPC 0
PenUp
setXY -maxx -maxy
PenDown
setY maxy
setX maxx
setY -maxy
setX -maxx
tick=10
txtx=25
setH 90
setLabelSize [25 25]
setLabelAlign 0 0
setPrintPrecision 4
setLabelFont "Times
h=ymax-ymin
hq=(ymax0-ymin0)*2^(round -0.5+(ln abs h/(abs ymax0-ymin0))/ln 2)
w=xmax-xmin
wq=(xmax0-xmin0)*2^(int -0.5+(ln abs w/(abs xmax0-xmin0))/ln 2)
for [y -ticksy ticksy]
[
hy=-hq*(y/ticksy-0.5)
hy=hy+(remainder -(ymax+ymin)/2 hq/ticksy)
ry=hy+(ymax+ymin)/2
hy=-hy*maxy*2/h
if hy > maxy or2 hy < -maxy [continueLoop]
;(pr maxy h hy)
;if this == xy::g3 [pr hy]
setY hy
PenDown
setX -maxx+tick
setX -maxx
PenUp
setX maxx-tick
PenDown
setX maxx
PenUp
setX -maxx-txtx
Label ry
;pr ry
setX -maxx
]
setY -maxy
for [x -ticksx 2*ticksx]
[ hx=-wq*(x/ticksx-0.5)
hx=hx+(remainder -(xmax+xmin)/2 wq/ticksx)
rx=hx+(xmax+xmin)/2
hx=-hx*maxx*2/w
if hx > maxx or2 hx < -maxx [continueLoop]
setX hx
PenDown
setY -maxy+tick
setY -maxy
PenUp
setY maxy-tick
PenDown
setY maxy
PenUp
setY -maxy-txtx
label rx
setY -maxy
]
setPenSize [0 0]
x=(xmax+xmin)/2
y=(ymax+ymin)/2
fx=2/(xmax-xmin)*maxx
fy=2/(ymax-ymin)*maxy
xxa=saturateBelow -maxx saturateAbove maxx (-xa+x)*fx
yya=saturateBelow -maxy saturateAbove maxy (-ya+y)*fy
ifelse mode == "pixels
[ setPixelXY xxa yya c
][
setPC c
PenUp
; x=maxx
; repeat count xxa
; [ j=repcount
; if xxa.j < x [x=xxa.j y=yya.j]
; ]
; setXY x y
setXY xxa.1 yya.1
PenDown
setXY xxa yya
PenUp
]
updateGraph
;pause
end
be clean
GraphSetCurrent g
clearScreen
end
end
to xy
imax=10000
tmax=1
dt=tmax/imax
tau=1000
Ohm=1
kOhm=1e3*Ohm
Volt=1
Farad=1
Coulomb=1
Ampere=1
uF=1e-6*Farad
UB=1.0*Volt
R1=10*Ohm
R2=1000*Ohm
R3=20000*Ohm
C=10*uF
Cf=sqrt C
t=rSeqFA 0 tmax imax
UR1=FloatArray imax
UR2=FloatArray imax
UR3=FloatArray imax
qr1a=0
qr1b=0
qr2a=0
qr2b=0
qr3a=0
qr3b=0
qca=0
qcb=0
for [i 1 imax]
[ qb=1.0*Coulomb*tau
q0=0.0*Coulomb*tau
qr1a=(qb*1000+qr1a+qr2a)/1002
qr2a=qr1a
qr1b=(q0*1000+qr1b+qr3b)/1002
qr3b=qr1b
qr2b=(qr2b+qr3a)/2
qr3a=qr2b
UR1.i=(qr1a-qr1b)
IR1=UR1.i/R1*tau
qr1a=qr1a-IR1
qr1b=qr1b+IR1
qr1b=(q0*1000+qr1b+qr3b)/1002
qr3b=qr1b
UR2.i=(qr2a-qr2b)
IR2=UR2.i/R2*tau
qr2a=qr2a-IR2
qr2b=qr2b+IR2
qr2b=(qr2b+qr3a)/2
qr3a=qr2b
; UR3.i=(qr3a-qr3b)
; IR3=UR3.i/R3*tau
;comment [
qR3=(qr3a-qr3b)/2
qCan=(qCa*Cf+qR3*dt)/(Cf+dt)
qCbn=(qCb*Cf-qR3*dt)/(Cf+dt)
IR3=(qCan-qCa)
qCa=qCan
qCb=qCbn
UR3.i=(qCa-qCb)
;]
qr3a=qr3a-IR3
qr3b=qr3b+IR3
]
UR1=UR1/tau
UR2=UR2/tau
UR3=UR3/tau
;R123=1/(1/(R2+R3)+1/R1)
;(pr R123 IR1 IR2 IR3 R123*(IR1+IR2))
;(pr IR2 IR3)
if not Name? "frm
[
frm=Frame [][MyFrame]
wxResize_Border+wxCaption+wxSystem_Menu+wxClose_Box
+wxFull_Repaint_on_Resize+wxminimize_box+wxmaximize_box
;wxDefault_Frame_Style+wxStay_on_Top)
[0 0][640 480]
g=XYGraph frm 640 480 0 tmax -2 2 8 4 "lines
; norefresh
g'init
WindowSetClientSize frm 640 480
running=true
stopping=false
; FrameOnClose frm
; [ running=false
; ]
; FrameOnChar frm
; [ running=false
; ]
]
GraphSetCurrent g'g
clean
(g'plot t UR1 HSBA 0 1 1 0.5)
(g'plot t UR2 HSBA 240 1 1 0.5)
(g'plot t UR3 HSBA 120 1 1 0.5)
; WindowSetFocus g'g
dispatchMessages
GC
end