Gauss-Seidel Method, differences from key in Python

941 Views Asked by At

Here is the problem
equation 2: x1 = 1/7(6 + y0)
equation 3: y1 = 1/5(x1 + 4)

Define a function, called gs1_iteration, which accepts as input values for x and y (which we think of as being xn-1 and yn-1 respectively), and returns a list [new_x,new_y], where new_x is the updated value x_n and new_y is the updated value y_n using the Gauss-Seidel method and equations (2) and (3).

In other words, gs1_iteration(x,y) should return the results of performing one iteration of the Gauss-Seidel method for system (1) on the inputs x, y.

To test your function, note that gs1_iteration(3,5) should return
[1.5714285714285714, 1.1142857142857143].

So I know I have this first iteration right as
def gs1_iteration(x,y): new_x=0 new_y=0 new_x = 1/7*(6+y) new_y = 1/5*(new_x+4) y = new_y return [new_x,new_y] gs1_iteration(3,5) returns [1.5714285714285714, 1.1142857142857143]

However, the next problem states:
Define a function, called gs1_method, which accepts as input a single non- negative integer n, and returns a list [x_n,y_n], where x_n and y_n are the values of x_n and y_n respectively for the Gauss-Seidel method when applied to system (1) above. Use (x0, y0) = (0, 0) as your starting approximation. To test your function, note that gs1_method(2) should return the list [0.9959183673469388, 0.9991836734693879].

But my code returns a different value def gs1_method(n): x_n=0 y_n=0 for i in range(n): [x_n, y_n] = gs1_iteration(x_n,y_n) return [x_n,y_n] gs1_method(2) returns [0.9959183673469387, 0.9991836734693877], which is different in the last decimal place than the key shows

From what I understand, my equation should work, as according to the Gauss-Seidel method, I'm using the new value of x and y for the iterations. Because I'm getting a different value, I think there must be some error in my equation. What equation should I use for my Gauss-Seidel program, or what can I fix in my current equation to get the values provided in the key?

I wasn't sure if this would have been more appropriate on stack overflow, but I think my actual problem is with my math, which is why I posted it here.