Ich hab mir folgendes Skript gebastelt, dass ein GNUPlot File erstellt in welchem eine Cosinus Funktion überlagert wird. Der Hintergrund ist, dass so ein Wellenpaket modelliert werden kann.
Code: Alles auswählen
#!/usr/bin/env python3
# NOTE:
# - Write superpositions of wavefunction into a
# plotting script
# - The wavefunction is a linear combination according to
# psi(x) = sum[ (1/N)*cos(k*pi*x), k=1..N]
import os
# The primitive wavefunction
def wave(N, k):
return '1/{0}.0*cos({1}.0*pi*x)'.format(N,k)
# Superimposing the wavefunction
def loop(L):
function = ''
for i in range(1, L+1):
if i < L:
function += wave(L, i) + '+'
else:
function += wave(L, i)
return '(' + function + ')*(' + function + ')', L
# Generating the GNUPlot script
def writeGnuString(functions, lw='2'):
gs = ''
gs += 'set xrange[-1:1]\n'
gs += 'plot \\' + '\n'
i = 0
for func in functions:
if i < len(functions)-1:
gs += '{0} title "N={1}" lw {2}, \\'\
.format(func[0], func[1], lw) + '\n'
i+=1
else:
gs += '{0} title "N={1}" lw {2};\n'\
.format(func[0], func[1], lw)
gs += 'pause -1;'
return gs
# Work
functions = []
# Number of superpositions
for i in [2,5,10,25]:
functions.append(loop(i))
file = open('wave.gnu', 'w')
file.write(writeGnuString(functions))
file.close()
os.system('gnuplot wave.gnu')
Weiter bin ich auch offen für Hinweise zur sinnvollen Optimierung des Codes hinsichtlich den Formatting Konventionen, list comprehensions, lambda functions etc etc.
Freue mich auf Beiträge.