Code: Alles auswählen
from numpy import pi, sin
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib.patches import Ellipse
from scipy.optimize import fsolve
import pylab
# Constants
w0=1.7
lamb=0.4119
f0=lamb*1.0/(299.792458)
#lamb=f0*1.0/(299.792458)
d_in=80.0
R1=46.672
R2=686.468
foc=1/((1/R1)+(1/R2))
c=299792458000.0
zR=np.pi*w0**(2)/(c/(f0*1000000000.0))
# function 1 and 2
def signal(w0,f0):
return w0*np.sqrt(1.0+(x/(np.pi*w0**(2)/(c/(f0*1000000000.0))))**(2))
def signal2(w0,f0):
return (-1.0)*w0*np.sqrt(1.0+(x/(np.pi*w0**(2)/(c/(f0*1000000000.0))))**(2))
# function 3 and 4
d_out=foc+(d_in-foc)/(((d_in)/foc-1.0)**(2)+(zR/foc)**(2))
w1=w0*np.sqrt(1.0/(((d_in)/foc-1.0)**(2)+(zR/foc)**(2)))
zR1=np.pi*w1**(2)/(c/(f0*1000000000.0))
def signal3(w0,d_in,f0):
return (w0*np.sqrt(1.0/(((d_in)/foc-1.0)**(2)+((np.pi*w0**(2)/(c/(f0*1000000000.0)))/foc)**(2))) )*np.sqrt(1.0+((x-d_in-(foc+(d_in-foc)/(((d_in)/foc-1.0)**(2)+((np.pi*w0**(2)/(c/(f0*1000000000.0)))/foc)**(2))))/(np.pi*(w0*np.sqrt(1.0/(((d_in)/foc-1.0)**(2)+((np.pi*w0**(2)/(c/(f0*1000000000.0)))/foc)**(2))) )**(2)/(c/(f0*1000000000.0))))**(2))
def signal4(w0,d_in,f0):
return (-1.0)*(w0*np.sqrt(1.0/(((d_in)/foc-1.0)**(2)+((np.pi*w0**(2)/(c/(f0*1000000000.0)))/foc)**(2))) )*np.sqrt(1.0+((x-d_in-(foc+(d_in-foc)/(((d_in)/foc-1.0)**(2)+((np.pi*w0**(2)/(c/(f0*1000000000.0)))/foc)**(2))))/(np.pi*(w0*np.sqrt(1.0/(((d_in)/foc-1.0)**(2)+((np.pi*w0**(2)/(c/(f0*1000000000.0)))/foc)**(2))) )**(2)/(c/(f0*1000000000.0))))**(2))
#############################################################################
axis_color = 'lightgoldenrodyellow'
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111)
# Adjust the subplots region to leave some space for the sliders and buttons
fig.subplots_adjust(left=0.25, bottom=0.35)
x = np.arange(0.0, 300, 0.01)
# Adjust Start Values
w0_0 = 1.7
f0_0 = 727.82825
w1_0=w0*np.sqrt(1.0/(((d_in)/foc-1.0)**(2)+(zR/foc)**(2)))
d_in_0=80.0
d_out_0=foc+(d_in-foc)/(((d_in)/foc-1.0)**(2)+(zR/foc)**(2))
# Draw the initial plot
# The 'line' variable is used for modifying the line later
[line] = ax.plot(x, signal(w0_0, f0_0), linewidth=2, color='red')
[line2] = ax.plot(x, signal2(w0_0, f0_0), linewidth=2, color='red')
[line3] = ax.plot(x, signal3(w0_0, d_in_0, f0_0), linewidth=2, color='blue')
[line4] = ax.plot(x, signal4(w0_0, d_in_0, f0_0), linewidth=2, color='blue')
ax.set_xlim([0, 300])
ax.set_ylim([-60, 60])
#Mirror
Spiegel = Ellipse(xy=(d_in, 0), width=2, height=73.2,
edgecolor='black', fc='#808080', lw=1)
ax.add_patch(Spiegel)
#Diamond
Diamond = Ellipse(xy=(140, 0), width=3, height=18,
edgecolor='black', fc='#FFCC99', lw=1)
ax.add_patch(Diamond)
#Trap
Trap = Ellipse(xy=(279.8, 0), width=3, height=6.6,
edgecolor='black', fc='#FFCC99', lw=1)
ax.add_patch(Trap)
# Add two sliders for tweaking the parameters
# Define an axes area and draw a slider in it
w0_slider_ax = fig.add_axes([0.25, 0.2, 0.65, 0.03], axisbg=axis_color)
w0_slider = Slider(w0_slider_ax, 'w0 [mm]', 0.1, 9.2, valinit=w0_0)
# Draw another slider
f0_slider_ax = fig.add_axes([0.25, 0.15, 0.65, 0.03], axisbg=axis_color)
f0_slider = Slider(f0_slider_ax, 'f0 [GHz]',23.0, 1100, valinit=f0_0)
#Draw d_in slider
d_in_slider_ax = fig.add_axes([0.25, 0.1, 0.65, 0.03], axisbg=axis_color)
d_in_slider = Slider(d_in_slider_ax, 'd_in [mm]', 1, 150, valinit=d_in)
# Draw f01 slider
f01_slider_ax = fig.add_axes([0.10, 0.9, 0.1, 0.03], axisbg=axis_color)
f01_slider = Slider(f01_slider_ax, '',750, 1100, valinit=7)
# Define an action for modifying the line when any slider's value changes
def sliders_on_changed(val):
line.set_ydata(signal(w0_slider.val, f0_slider.val))
line2.set_ydata(signal2(w0_slider.val, f0_slider.val))
line3.set_ydata(signal3(w0_slider.val,d_in_slider.val, f0_slider.val))
line4.set_ydata(signal4(w0_slider.val,d_in_slider.val, f0_slider.val))
# Spiegel.set_ydata(Spiegel(d_in_slider.val))
fig.canvas.draw_idle()
w0_slider.on_changed(sliders_on_changed)
f0_slider.on_changed(sliders_on_changed)
d_in_slider.on_changed(sliders_on_changed)
# Add a button for resetting the parameters
reset_button_ax = fig.add_axes([0.8, 0.025, 0.1, 0.04])
reset_button = Button(reset_button_ax, 'Reset', color=axis_color, hovercolor='0.975')
def reset_button_on_clicked(mouse_event):
f0_slider.reset()
w0_slider.reset()
d_in_slider.reset()
reset_button.on_clicked(reset_button_on_clicked)
#Add boxes
WR1_button_ax = fig.add_axes([0.025, 0.9, 0.05, 0.05])
WR1_button = Button(WR1_button_ax, 'WR1.0:', color=axis_color, hovercolor='0.975')
def WR1_button_on_clicked(mouse_event):
w0_slider.set_val(0.66)
f0_slider.set_val(750.0)
WR1_button.on_clicked(WR1_button_on_clicked)
WR2_button_ax = fig.add_axes([0.025, 0.7, 0.1, 0.10])
WR2_button = Button(WR2_button_ax, 'WR1.5:', color=axis_color, hovercolor='0.975')
def WR2_button_on_clicked(mouse_event):
w0_slider.set_val(0.99)
WR2_button.on_clicked(WR2_button_on_clicked)
WR3_button_ax = fig.add_axes([0.025, 0.6, 0.1, 0.10])
WR3_button = Button(WR3_button_ax, 'WR2.2:', color=axis_color, hovercolor='0.975')
def WR3_button_on_clicked(mouse_event):
w0_slider.set_val(1.5)
WR3_button.on_clicked(WR3_button_on_clicked)
def update(val):
Spiegel.center = (val,0)
d_in_slider.on_changed(update)
plt.show()
Ich will jedem Button (WR1.0, WR1.5, WR2.2) einen Schieberegler (f0,f1,f2) geben, der jeweils nur in einer bestimmten Range liegt, z.B: für WR1.0 von 750-1100.
Das ist kein Problem. Jedoch sollen die 4 Funktionen auf diese Regler reagieren. Ich habe jedoch die 4 Funktionen die alle abhängig von einem f0 sind und nicht
von einzelnen f0, f1,f2.
Daher dachte ich mir diese Durchnummerierung, sodass ich später bei '# Define an action for modifying the line when any slider's value changes' nur noch ein f