Question: Why is my code running 0 iteration..I know this isn't correct..I'm trying to answer a tutorial question (attached) using Alg 11 & 12 (attached): my
Why is my code running 0 iteration..I know this isn't correct..I'm trying to answer a tutorial question (attached) using Alg 11 & 12 (attached): my code is below
import numpy as np# import timefrom numpy.linalg import normimport mathfrom scipy.fft import fftfrom numpy.lib.stride_tricks import as_strided# from scipy.linalg import sqrtm# from scipy.sparse import csr_matrixdef gamma(n): g = [] for k in range(n): cov = np.exp(-10 * k / n) * np.cos(-10 * math.pi * k / n) g.append(cov) return gdef toeplitz(c, r=None): c = np.asarray(c).ravel() if r is None: r = c.conjugate() else: r = np.asarray(r).ravel() # Form a 1-D array containing a reversed c followed by r[1:] that could be # strided to give us toeplitz matrix. vals = np.concatenate((c[::-1], r[1:])) out_shp = len(c), len(r) n = vals.strides[0] return as_strided(vals[len(c) - 1:], shape=out_shp, strides=(-n, n)).copy()def conjugate_grad(A, b, x=None): n = len(b) if not x: x=np.zeros([n,1]) def Ax(x): g_f = gamma(n) zero = [0] g_f_rev = g_f[::-1][:-1] g_f_comb = g_f + zero + g_f_rev g_array = np.array([g_f_comb]) x_f = x[:1] x_zero = [0] * 2 * (n) x_f_comb = x_f + x_zero z = np.fft.fft(x_f_comb.T) / 2 * n lamb = np.fft.fft(g_array.T) z_1 = np.dot(z, lamb.T) z_n = np.fft.fft(np.conjugate(z_1)) return np.real(z_n[:n, :n]) r = np.dot(Ax(x), x) - b p = - r r_k_norm = r_k_norm = np.dot(r.T,r) t_steps = [] tol = [] i=0 while True: Ap = np.dot(Ax(p), p) alpha = np.dot(r.T, r) / np.dot(p.T, Ap) x += alpha * p r += alpha * Ap r_kplus1_norm = np.dot(r.T,r) r_k_norm = r_kplus1_norm t_steps.append(i) tol.append(r_k_norm) if r_kplus1_norm 1e-4: print('Itr:', i) break else: beta = r_kplus1_norm / r_k_norm p = beta * p - r i+=1 return xif __name__ == '__main__': n=10 b=np.ones([n,1]) #A=toeplitz(gamma(n)) P = np.random.normal(size=[n, n]) A = np.dot(P.T, P) conjugate_grad(A, b, x=None)=====================================================#Output:C:\Python\Python39-64........Itr: 0Process finished with exit code 0



Algorithm 12 Circulant Embedding computing r - Ar in O(n Inn) time Require: x and first row y = [7(0), ..., y(n -1)] of Toeplitz A ( Rox y + [y , 0, y(n - 1), .... 7(2), 7(1)]T -x0....0TER z + FE/(2n) in O(alna) time using FFT algorithm A+ Fy in O(nlnn) time using FFT algorithm 2+ =0X in O(n) time z + FZ in O(n lnn) time using FFT algorithm return first n elements of z\fTime Series Tutorialeuiz Questions 1. Suppose we wish to solve the linear system Ta: = 1, where T E RM" with n = 215 is a the covariance matrix of a stationary process with automarianoe t} 2 eprI: ltfn] cos[1?rtfn} for t = , . . . ,n 1. 1Write your own implementation of Algorithms 11 | 12 in the lecture notes to compute 93 a T_11 with error at = \"Tmt h\" .5: 113"1 using an initial guess of D. Report how many conjugate gradient iterations i you needed to get to the desired accuracy and printfoutput a {logarithmic} plot of h1[et} {on the yaxis} versus t [on the xaxis}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
