2016-04-11 3 views
0

Ich möchte die numerischen Werte der beiden Parameter ka und kd eingeben und dann auf eine Schaltfläche klicken, um die Simulation zu starten. Dann ändern Sie beide Parameter, klicken Sie auf eine Schaltfläche und starten Sie eine weitere Simulation. Jetzt scheint es, dass nur die Standardwerte, die im Widget "BoundedFloatText" angegeben sind, in das Hauptprogramm übertragen werden. Danke.Wie Eingabe numerischer Werte mit IPython-Widget BoundedFloatText?

%matplotlib inline 
import numpy as np 
from scipy.integrate import ode 
import matplotlib.pyplot as plt 
from ipywidgets import * 
from IPython.display import clear_output, display, HTML 
# 
def fmain (ka, kd): 
# 
    num_steps = 10 
# 
# Prepare plots 
    fig, ax = plt.subplots() 
#  
    τ = np.zeros((num_steps, 1)) 
    F = np.zeros(num_steps) 
    τ[0] = 0 
    F[0] = 0.0 
# 
# Integrate the system of ODEs across each delta_t timestep 
    kk = 1 
    while kk < num_steps:    
     τ[kk] = τ[kk-1] + 0.01  
     F[kk] = ka + kd*τ[kk] 
#     
     clear_output(wait=True) 
     ax.cla() 
     ax.plot(τ[0:kk], F[0:kk], color="blue", linestyle="solid", linewidth=2) 
     ax.set_ylabel(r'Dimensionless exit flow rate, $\bar F_A$ [-]') 
     ax.set_xlabel('Dimensionless time, $τ$ [-]') 
     ax.set_title('Numerical Simulation ka= %s, kd = %s'%(ka, kd)) 
     display(fig) 
#   
# end of time cycle  
     kk += 1   
#  
plt.close() 
# Specify widgets 

form = widgets.VBox() 
ka_in = BoundedFloatText(description="$$k_a:$$", 
          value=5.0, min=0.0, max=30.0, 
          padding = 4) 
kd_in = BoundedFloatText(description="$$k_d:$$", 
          value=5.0, min=0.0, max=30.0, 
          padding = 4) 
button = widgets.Button(description="Click to Run Program", 
        color="red",background_color= "lightgray") 
form.children = [ka_in, kd_in, button ] 
display(form) 
ka = ka_in.value 
kd = kd_in.value 
h4 = widgets.HTML("<br><i> Simulation started, be patient!</i><br>") 
def on_button_clicked(b): 
    display(h4) 
    fmain(ka , kd) 

button.on_click(on_button_clicked) 

Antwort

0

Ich glaube, ich habe eine Antwort auf meine Frage gefunden.

%matplotlib inline 
import numpy as np 
from scipy.integrate import ode 
import matplotlib.pyplot as plt 
from ipywidgets import * 
from IPython.display import clear_output, display, HTML 
# 
def fmain (ka, kd): 
# 
    num_steps = 10 
# 
# Prepare plots 
    fig, ax = plt.subplots() 
#  
    τ = np.zeros((num_steps, 1)) 
    F = np.zeros(num_steps) 
    τ[0] = 0. 
    F[0] = ka + kd*τ[0] 
# 
# Integrate the system of ODEs across each delta_t timestep 
    kk = 1 
    while kk < num_steps:  
     τ[kk] = τ[kk-1] + 0.01  
     F[kk] = ka + kd*τ[kk] 
#     
     clear_output(wait=True) 
     ax.cla() 
     ax.plot(τ[0:kk], F[0:kk], color="blue", linestyle="solid",  linewidth=2) 
     ax.set_ylabel(r'Dimensionless exit flow rate, $\bar F_A$ [-]') 
     ax.set_xlabel('Dimensionless time, $τ$ [-]') 
     ax.set_title('Numerical Simulation ka= %s, kd = %s'%(ka, kd)) 
     display(fig) 
#   
# end of time cycle  
     kk += 1   
#  
plt.close() 
# Specify widgets 

form = widgets.VBox() 
ka = BoundedFloatText(description="$$k_a:$$", 
          value=5.0, min=0.0, max=30.0, 
          padding = 4) 
kd = BoundedFloatText(description="$$k_d:$$", 
          value=5.0, min=0.0, max=30.0, 
          padding = 4) 
button = widgets.Button(description="Click to Run Program", 
        color="red",background_color= "lightgray") 
form.children = [ka, kd, button] 
display(form) 

h4 = widgets.HTML("<br><i> Simulation started, be patient!</i><br>") 
def on_button_clicked(b): 
    display(h4) 
    fmain(ka.value, kd.value) 
    h4.close() 
button.on_click(on_button_clicked) 
Verwandte Themen