Γράψτε μια numpy συνάρτηση myfunction3 που επιλύει το γραμμικό σύστημα: $$…
- User
Γράψτε μια numpy συνάρτηση
myfunction3που επιλύει το γραμμικό σύστημα: - Assistant
Γράψτε μια numpy συνάρτηση myfunction3 που επιλύει το γραμμικό σύστημα:
όπου a,b,c τα 3 τελευταία ψηφία του αριθμού μητρώου σας (c=τελευταίο).
Χρησιμοποιήστε την μέθοδο Gauss-Seidel σταματώντας όταν όλα τα στοιχεία της καινούργιας εκτίμησης αποκλίνουν από την προηγούμενη λιγότερο από 2ΟE-4 κατά απόλυτη τιμή. Η συνάρτηση θα δέχεται ως όρισμα ένα διάνυσμα με τις αρχικές τιμές και θα επιστρέφει ένα διάνυσμα 3 στοιχείων με τα τελικά αποτελέσματα.
Ελέγξτε τα αποτελέσματα της συνάρτησης για και .
Υπόδειξη: Χρησιμοποιήστε .copy(), όταν θέλετε να αντιγράψετε τις τιμές σε διαφορετικό χώρο μνήμης και όχι να δημιουργήσετε ένα δείκτη σε υπάρχοντα πίνακα.
To solve the linear system using the Gauss-Seidel method in Python with NumPy, we create a function myfunction3 that accepts an initial guess vector x_init and returns the solution vector [x1, x2, x3].
Below is the implementation of the function:
import numpy as np
def myfunction3(x_init):
# Replace these with the last three digits of your student number
a, b, c = 1, 2, 3 # Example values for testing
# Define matrix A and vector b
A = np.array([[3, -1, -2],
[2, 5, -3],
[3, -2, 5]], dtype=float)
b_vector = np.array([a + 1, b + 2, c + 3], dtype=float)
# Initialize variables
x = x_init.copy()
tol = 2e-4 # Convergence tolerance
max_iter = 10000 # Maximum number of iterations to prevent infinite loops
for iteration in range(max_iter):
x_old = x.copy()
# Gauss-Seidel iterations (using the latest available values)
x[0] = (1 / 3) * (b_vector[0] + x[1] + 2 * x[2])
x[1] = (1 / 5) * (b_vector[1] - 2 * x[0] + 3 * x[2])
x[2] = (1 / 5) * (b_vector[2] - 3 * x[0] + 2 * x[1])
# Check for convergence
if np.all(np.abs(x - x_old) < tol):
break
else:
print("The method did not converge within the maximum number of iterations.")
return x
# Test the function with x_init = [0., 0., 0.]
x_init1 = np.array([0., 0., 0.])
result1 = myfunction3(x_init1)
print("Result with x_init = [0., 0., 0.]:", result1)
# Test the function with x_init = [100., 100., 100.]
x_init2 = np.array([100., 100., 100.])
result2 = myfunction3(x_init2)
print("Result with x_init = [100., 100., 100.]:", result2)
Make sure to replace a, b, and c with the last three digits of your student number (with c being the last digit).
Explanation:
In the function myfunction3:
Initialization:
A and vector b_vector using the given equations.x with the provided initial guess x_init.tol to 2e-4 and define a maximum number of iterations max_iter to prevent infinite loops.Gauss-Seidel Iteration:
x[0], x[1], and x[2] using the Gauss-Seidel formulas derived from the equations:
.copy() to ensure that x_old is a separate copy of x, not just a reference.Convergence Check:
x are all less than tol.max_iter iterations, we print a message.Testing the Function:
[0., 0., 0.] and [100., 100., 100.].Note: This function assumes that the values of a, b, and c are known and provided. Replace these with your specific digits when running the code.
Not yet included in a published release.