Question: This is for Python. Although if you are more familiar with MATLAB I'm sure I can convert over. I've started off the code like this:

 This is for Python. Although if you are more familiar with

This is for Python. Although if you are more familiar with MATLAB I'm sure I can convert over. I've started off the code like this:

n = 20

b = np.linspace(1, n, n)/(n+1)**3

x0 = np.zeros(n)

def gs_solve(x0,b,n,tol = 1E-6): l, d, u = create_A_vecs(n)

# diff_matrix constructs A but only returns (3) nx1 numpy arrays, l for below diag, d for diag, u for above diag (where l and u are padded with zero at the beginning and end respectively)

x_old = x0 x_new = x0 x_sample = 0 it = 0 # I shouldn't need this, but otherwise it won't enter the while loop

while it == 0 or np.linalg.norm(x_new-x_old)

x_old = x_new

x_new[0] = (1/d[0])*(b[0] - (u[0]*x_old[1])) for i in range(1,n-1): x_new[i] = (1/d[i])*(b[i] - (l[i]*x_new[i-1]) - (u[i]*x_old[i+1])) if i == int(n/2): x_sample[it] = x_new[i] x_new[n-1] = (1/d[n-1])*(b[n-1] - (l[n-1]*x_new[n-2]))

it += 1 return x_new, x_sample

My problem is, this code runs forever. This code should be specific to this A matrix given!!! It should NOT be a general GS method code. My code can actually be simplified in this respect, since the lower and upper entries will always be (-1). But that doesn't address the infinite loop. Could you suggest some help please?

Will rate immediately, thank you!

: 0 -1 0 Consider our familiar example, the tridiagonal n x n matrix 2 -1 0 0 -1 2 -1 : A= 0 0 2 -1 -1 2 The goal of this problem is to see how an iterative method can be used when the matrix is large and sparse, and to study convergence numerically. a) Write a function gs_solve(x0,b,n, tol) that solves At = b using the Gauss-Seidel method without constructing the matrix A. Use ||2(x+1) 2-(K) ||

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!