I am trying to write a program that will solve a differential equation for 
several values of one of the equations parameters in a for loop. After each 
iteration of the loop I would like to plot the mean value of the solution. 
What happens in practice is the the plot will only be generated after 
exiting the for loop. A minimal working example demonstrating this issue is 
pasted below and attached. Any advice on how to get this working is 
appreciated. 

Cheers!



# gui
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

# ODE
from scipy.integrate import RK45

win = pg.GraphicsWindow()
p1 = win.addPlot()
data = np.empty([100000,2])     # Holds time dep. solution

g = 0.5
cntr = 0

def EOM(t,y):
    global g
    return -g*y

def update():
    p1.plot(tcData[:cntr,:])

timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)

tC = np.linspace(-0.5,0.5,20)   # Sweep parameter
tcData = np.empty([len(tC),2])  # Holds mean solution (plot this data)
for idx,item in enumerate(tC):
    ode = RK45(EOM, 0, [5],t_bound=10,max_step=0.001)
    cntr = 0
    g = item
    while ode.status == 'running':
        ode.step()
        data[cntr,0] = ode.t
        data[cntr,1] = ode.y
        cntr+=1
    tcData[idx,0] = item
    tcData[idx,1] = np.mean(data[:,1])
    print( idx )


if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/d3751404-dc53-4e93-8413-c445a6faf20e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
# gui
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

# ODE
from scipy.integrate import RK45

win = pg.GraphicsWindow()
p1 = win.addPlot()
data = np.empty([100000,2])     # Holds time dep. solution

g = 0.5
cntr = 0

def EOM(t,y):
    global g
    return -g*y

def update():
    p1.plot(tcData[:cntr,:])

timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)

tC = np.linspace(-0.5,0.5,20)   # Sweep parameter
tcData = np.empty([len(tC),2])  # Holds mean solution (plot this data)
for idx,item in enumerate(tC):
    ode = RK45(EOM, 0, [5],t_bound=10,max_step=0.001)
    cntr = 0
    g = item
    while ode.status == 'running':
        ode.step()
        data[cntr,0] = ode.t
        data[cntr,1] = ode.y
        cntr+=1
    tcData[idx,0] = item
    tcData[idx,1] = np.mean(data[:,1])
    print( idx )


if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Reply via email to