Beispiel #1
0
def idcst2(b):
    """ Takes iDST along y, then iDCT along x
    IN: b, the input 2D numpy array
    OUT: f, the 2D inverse-transformed array """
    M = b.shape[0]  # Number of rows
    N = b.shape[1]  # Number of columns
    a = np.zeros((M, N))  # Intermediate array
    f = np.zeros((M, N))  # Final array
    # Take inverse transform along y
    for i in range(M):
        a[i, :] = idst(b[i, :])
    # Take inverse transform along x
    for j in range(N):
        f[:, j] = idct(a[:, j])
    return f
Beispiel #2
0
#plotting for animation inside the while loop
plt.ion()
fig  = plt.figure()
ax   = plt.axes(xlim=(0,1e-8),ylim=(-5e-8,5e-8))
line = ax.plot(x,si_initial(x),'-b')
plt.show()

#an empty list to hold si values
si_list = []
t    = 0
tend = 1e-15 #s
while t<tend+h:
    
    first    = alpha*(np.cos(vals*t)) #first expression (alpha*cos(args))
    first    = idst(first)            #inverse fourier transform of first expression
    second   = eta*(np.sin(vals*t))   #second expression (eta*sin(args))
    second   = idst(second)           #inverse fourier transform of second expression
    tot      = (first-second)*(np.sin((np.pi*k*x))/N)  # expression on page 443
    si_list.append(tot)               #appending wave function values to this list
    
    #plotting an animation
    line[0].set_ydata(tot)
    plt.draw()
    ax.set_title("time={0}".format(t)) #plot title
    t +=h  #adding h value to the time each time




impsi = zeros(N, float)

x = linspace(0, L, N)
#Set initial conditions
for n in range(1, N):
    xn = n * a
    x[n] = xn
    gauss = exp(-(xn - x0)**2 / (2 * (sigma**2)))
    repsi[n] = gauss * cos(kappa * xn)
    impsi[n] = gauss * sin(kappa * xn)

lines = plt.plot(x, ndarray.tolist(repsi))

#determine fourier coefficients
alpha = dst(repsi)
eta = dst(impsi)
b = empty(N, float)

i = 0
for t in arange(0.0, tmax, tstep):
    i += 1
    for k in range(1, N):
        angle = C * k * k * t
        b[k] = alpha[k] * cos(angle) + eta[k] * sin(angle)
    repsi = idst(b)

    if (i % 20 == 0):
        lines[0].set_ydata(ndarray.tolist(repsi[:]))
        plt.pause(0.0000000001)
        plt.draw()
Beispiel #4
0
from pylab import plot, show, legend, subplot, xlabel, ylabel
from numpy import zeros, empty, linspace, exp, arange,minimum, pi, sin,cos, array
from dcst import dst, idst, dct, idct

N = 256
# x = pi*n/N
x = arange(N)*pi/N
#function is a sine series
f = sin(x)-2*sin(4*x)+3*sin(5*x)-4*sin(6*x)
#do fourier sine series
fCoeffs = dst(f)
print('Original series: f = sin(x)-2sin(4x)+3sin(5x)-4sin(6x)')
for j in range(7):
    print( 'Coefficient of sin(%ix):'%j, fCoeffs[j]/N)

print('See Figure for calculating second derivative')
#second derivative is also a sine series
d2f_dx2_a = -sin(x)+32*sin(4*x)-75*sin(5*x)+144*sin(6*x)
#second derivative using fourier transform
DerivativeCoeffs = -arange(N)**2*fCoeffs
d2f_dx2_b = idst(DerivativeCoeffs)
subplot(2,1,1) 
plot(x,d2f_dx2_a, x, d2f_dx2_b,'+')
xlabel('x')
legend(('analytic','using DST'))
subplot(2,1,2)
plot(x,abs(d2f_dx2_a-d2f_dx2_b))
xlabel('x')
ylabel('abs(diff)')
show()
Beispiel #5
0
ax.set_xlabel('$x [m]$', fontsize=20)
ax.set_ylabel('$\psi$', fontsize=20)

# BEGIN COMPUTING PSI AND ANIMATING THE RESULT

psi_saves = []  # Array to hold snapshots

t = 0
while t < h * Nt:
    line[0].set_ydata(psi.real)
    ax.set_title('Time = {0} s'.format(t))
    plt.draw()
    alphan = alpha * np.cos(
        arg * t)  # Compute one set of sine series coefficients
    etan = eta * np.sin(arg * t)  # Compute the other set of S.S coefficients
    psi = idst(alphan) - idst(
        etan)  # Perform an inverse discrete sine transformation
    psi[0] = psi0  # Impose boundary conditions
    psi[-1] = psi1
    if t < eps or abs(t - 1e-16) < eps or abs(t - 1e-15) < eps:
        psi_saves.append(psi)
    t += h

plt.figure()
plt.subplot(311)
plt.plot(x, psi_saves[0].real)
plt.ylabel('$\psi$', fontsize=20)
plt.title('$0$ s')
plt.subplot(312)
plt.plot(x, psi_saves[1].real)
plt.ylabel('$\psi$', fontsize=20)
Beispiel #6
0
line = ax.plot(x,psi.real)
ax.set_xlabel('$x [m]$',fontsize = 20)
ax.set_ylabel('$\psi$',fontsize = 20)

# BEGIN COMPUTING PSI AND ANIMATING THE RESULT 

psi_saves = [] # Array to hold snapshots

t = 0
while t < h*Nt:
    line[0].set_ydata(psi.real)
    ax.set_title('Time = {0} s'.format(t))
    plt.draw()
    alphan = alpha * np.cos(arg*t) # Compute one set of sine series coefficients
    etan = eta * np.sin(arg*t) # Compute the other set of S.S coefficients
    psi = idst(alphan) - idst(etan) # Perform an inverse discrete sine transformation
    psi[0] = psi0 # Impose boundary conditions
    psi[-1] = psi1
    if t < eps or abs(t-1e-16) < eps or abs(t-1e-15) < eps:
    	psi_saves.append(psi)
    t+=h


plt.figure()
plt.subplot(311)
plt.plot(x,psi_saves[0].real)
plt.ylabel('$\psi$',fontsize = 20)
plt.title('$0$ s')
plt.subplot(312)
plt.plot(x,psi_saves[1].real)
plt.ylabel('$\psi$',fontsize = 20)
Beispiel #7
0
#array of times to be plotted
t = np.array([2, 4, 6, 12, 100]) / 1000

#container for the solutions at the desired times
solutions = []

#loops over the desired times and compute the solution to the wave equation
for t_i in t:
    #compute the omega values for each coefficient
    k = np.arange(0, N, 1)
    omega = np.pi * v * k / L
    #compute the coefficients of our solution
    sol_coeff = phi_coeff * np.cos(omega * t_i) + psi_coeff / omega * np.sin(
        omega * t_i)
    #compute the solution using the coefficients
    phi = dcst.idst(np.real(sol_coeff))
    solutions.append(phi)


#plotting fucntion
def plot_fig(x, phi, label, save, file_name):
    plt.figure()
    plt.plot(x, phi)
    plt.xlim([0, 1])
    plt.ylim([-0.0005, 0.0005])
    plt.xlabel("Position, $x$")
    plt.ylabel("Displacement, $\phi(x)$")
    plt.title("Piano string displacement at " + label)
    plt.grid()
    if save:
        plt.savefig("../images/" + file_name, dpi=600)