-1

I want to get my code to loop so that every time it performs the calculation, it adds basically does a cumulative sum for my variable delta_omega. i.e. for every calculation, it takes the previous values in the delta_omega array, adds them together and uses that value to perform the calculation again and so on. I'm really not sure how to go about this as I want to plot these results too.

import numpy as np
import matplotlib.pyplot as plt
delta_omega = np.linspace(-900*10**6, -100*10**6, m) #Hz - range of frequencies
i = 0

while i<len(delta_omega):
    delta = delta_omega[i] - (k*v_cap) + (mu_eff*B)/hbar
    p_ee = (s0*L/2) / (1 + s0 + (2*delta/L)**2) #population of the excited state
    R = L * p_ee # scattering rate
    F = hbar*k*(R) #scattering force on atoms
    a = F/m_Rb #acceleration assumed constant
    
    vf_slower = (v_cap**2 - (2*a*z0))**0.5 #velocity at the end of the slower
    t_d = 1/a * (v_cap - vf_slower) #time taken during slower
    
    
    #       -------- After slower --------
    da = 0.1 #(m) distance from end of slower to the middle of the MOT
    vf_MOT = (vf_slower**2 - (2*a*da))**0.5 #(m/s) - velocity of the particles at MOT center
    t_a = da/vf_MOT #(s) time taken after slower
    
    r0 = 0.01 #MOT capture radius
    vr_max = r0/(t_b+t_d+t_a) #maximum transveral velocity
    
    
    vz_max = (v_cap**2 + 2*a_max*z0)**0.5 #m/s - maximum axial velocity
    
    
    #       -------- Flux of atoms captured --------
    P = 10**(4.312-(4040/T)) #vapour pressure for liquid phase (use 4.857 for solid phase)
    A = 5*10**-4 #area of the oven aperture
    n = P/(k_b*T) #atomic number density
    
    f_oven = ((n*A)/4) * (2/(np.pi)**0.5) * ((2*k_b*T)/m_Rb)**0.5
    f = f_oven * (1 - np.exp(-vr_max**2/vp**2))*(1 - np.exp(-vz_max**2/vp**2))
    i+=1
    
plt.plot(delta_omega, f)

1 Answers1

0

A simple cumulative sum would be defining the variable outside the loop and adding to it

i = 0
x = 0
while i < 10:
    x = x + 5 #do your work on the cumulative value here
    i += 1
print("cumulative sum: {}".format(x))

so define a variable that will contain the cumulative sum, and every loop, add to it

DCA-
  • 1,262
  • 2
  • 18
  • 33