10
10
11
11
/**
12
12
* Function to solve a system of linear equations using the Jacobi iterative method
13
- * @param {array } A - The coefficient matrix (must be square)
14
- * @param {array } b - The right-hand side vector
15
- * @param {array } x0 - Initial guess for solution vector
13
+ * @param {array } jacobianMatrix - The coefficient matrix (must be square)
14
+ * @param {array } residualVector - The right-hand side vector
15
+ * @param {array } initialGuess - Initial guess for solution vector
16
16
* @param {object } [options] - Options for the solver
17
17
* @param {number } [options.maxIterations=1000] - Maximum number of iterations
18
18
* @param {number } [options.tolerance=1e-6] - Convergence tolerance
19
19
* @returns {object } An object containing:
20
- * - solution : The solution vector
20
+ * - solutionVector : The solution vector
21
21
* - iterations: The number of iterations performed
22
22
* - converged: Boolean indicating whether the method converged
23
23
*/
24
- export function jacobiMethod ( A , b , x0 , options = { } ) {
24
+ export function jacobiMethod ( jacobianMatrix , residualVector , initialGuess , options = { } ) {
25
25
const { maxIterations = 1000 , tolerance = 1e-6 } = options ;
26
- const n = A . length ; // Size of the square matrix
27
- let x = [ ...x0 ] ; // Current solution (starts with initial guess)
26
+ const n = jacobianMatrix . length ; // Size of the square matrix
27
+ let x = [ ...initialGuess ] ; // Current solution (starts with initial guess)
28
28
let xNew = new Array ( n ) ; // Next iteration's solution
29
29
30
30
for ( let iteration = 0 ; iteration < maxIterations ; iteration ++ ) {
31
31
// Perform one iteration
32
32
for ( let i = 0 ; i < n ; i ++ ) {
33
33
let sum = 0 ;
34
- // Calculate sum of A [i][j] * x[j] for j ≠ i
34
+ // Calculate sum of jacobianMatrix [i][j] * x[j] for j ≠ i
35
35
for ( let j = 0 ; j < n ; j ++ ) {
36
36
if ( j !== i ) {
37
- sum += A [ i ] [ j ] * x [ j ] ;
37
+ sum += jacobianMatrix [ i ] [ j ] * x [ j ] ;
38
38
}
39
39
}
40
40
// Update xNew[i] using the Jacobi formula
41
- xNew [ i ] = ( b [ i ] - sum ) / A [ i ] [ i ] ;
41
+ xNew [ i ] = ( residualVector [ i ] - sum ) / jacobianMatrix [ i ] [ i ] ;
42
42
}
43
43
44
44
// Check convergence
@@ -53,7 +53,7 @@ export function jacobiMethod(A, b, x0, options = {}) {
53
53
// Successfully converged if maxDiff is less than tolerance
54
54
if ( maxDiff < tolerance ) {
55
55
return {
56
- solution : x ,
56
+ solutionVector : x ,
57
57
iterations : iteration + 1 ,
58
58
converged : true ,
59
59
} ;
@@ -62,7 +62,7 @@ export function jacobiMethod(A, b, x0, options = {}) {
62
62
63
63
// maxIterations were reached without convergence
64
64
return {
65
- solution : x ,
65
+ solutionVector : x ,
66
66
iterations : maxIterations ,
67
67
converged : false ,
68
68
} ;
0 commit comments