-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjacobiSolverScript.js
More file actions
56 lines (49 loc) · 2.09 KB
/
jacobiSolverScript.js
File metadata and controls
56 lines (49 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* ════════════════════════════════════════════════════════════════
* FEAScript Core Library
* Lightweight Finite Element Simulation in JavaScript
* Version: 0.3.0 (RC) | https://un5peqtmyvbr2nu3.julianrbryant.com
* MIT License © 2023–2026 FEAScript
* ════════════════════════════════════════════════════════════════
*/
/**
* Function to solve a system of linear equations using the Jacobi iterative method (CPU synchronous version)
* @param {array} A - The system matrix
* @param {array} b - The right-hand side vector
* @param {array} x0 - Initial guess for solution vector
* @param {object} [options] - Optional parameters for the solver, such as `maxIterations` and `tolerance`
* @returns {object} An object containing:
* - solutionVector: The solution vector
* - iterations: The number of iterations performed
* - converged: Boolean indicating whether the method converged
*/
export function jacobiSolver(A, b, x0, options = {}) {
// Extract options
const { maxIterations, tolerance } = options;
const n = A.length;
let x = [...x0];
let xNew = new Array(n);
// Jacobi update: xNew[i] = (b[i] - sum(A[i][j] * x[j] for j != i)) / A[i][i]
for (let iter = 0; iter < maxIterations; iter++) {
for (let i = 0; i < n; i++) {
let sum = 0;
for (let j = 0; j < n; j++) {
if (i !== j) {
sum += A[i][j] * x[j];
}
}
xNew[i] = (b[i] - sum) / A[i][i];
}
// Check convergence based on maximum difference in solution vector
let maxDiff = 0;
for (let i = 0; i < n; i++) {
maxDiff = Math.max(maxDiff, Math.abs(xNew[i] - x[i]));
}
// Copy new solution for the next iteration
x = [...xNew];
if (maxDiff < tolerance) {
return { solutionVector: x, iterations: iter + 1, converged: true };
}
}
return { solutionVector: x, iterations: maxIterations, converged: false };
}