Skip to content

Commit fea4839

Browse files
committed
Refactor Jacobi and Newton-Raphson methods to standardize solution vector naming
1 parent b50df68 commit fea4839

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/methods/jacobiMethodScript.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,35 @@
1010

1111
/**
1212
* 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
1616
* @param {object} [options] - Options for the solver
1717
* @param {number} [options.maxIterations=1000] - Maximum number of iterations
1818
* @param {number} [options.tolerance=1e-6] - Convergence tolerance
1919
* @returns {object} An object containing:
20-
* - solution: The solution vector
20+
* - solutionVector: The solution vector
2121
* - iterations: The number of iterations performed
2222
* - converged: Boolean indicating whether the method converged
2323
*/
24-
export function jacobiMethod(A, b, x0, options = {}) {
24+
export function jacobiMethod(jacobianMatrix, residualVector, initialGuess, options = {}) {
2525
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)
2828
let xNew = new Array(n); // Next iteration's solution
2929

3030
for (let iteration = 0; iteration < maxIterations; iteration++) {
3131
// Perform one iteration
3232
for (let i = 0; i < n; i++) {
3333
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
3535
for (let j = 0; j < n; j++) {
3636
if (j !== i) {
37-
sum += A[i][j] * x[j];
37+
sum += jacobianMatrix[i][j] * x[j];
3838
}
3939
}
4040
// 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];
4242
}
4343

4444
// Check convergence
@@ -53,7 +53,7 @@ export function jacobiMethod(A, b, x0, options = {}) {
5353
// Successfully converged if maxDiff is less than tolerance
5454
if (maxDiff < tolerance) {
5555
return {
56-
solution: x,
56+
solutionVector: x,
5757
iterations: iteration + 1,
5858
converged: true,
5959
};
@@ -62,7 +62,7 @@ export function jacobiMethod(A, b, x0, options = {}) {
6262

6363
// maxIterations were reached without convergence
6464
return {
65-
solution: x,
65+
solutionVector: x,
6666
iterations: maxIterations,
6767
converged: false,
6868
};

src/methods/linearSystemScript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function solveLinearSystem(solverMethod, jacobianMatrix, residualVector,
5454
debugLog(`Jacobi method did not converge after ${jacobiResult.iterations} iterations`);
5555
}
5656

57-
solutionVector = jacobiResult.solution;
57+
solutionVector = jacobiResult.solutionVector;
5858
converged = jacobiResult.converged;
5959
iterations = jacobiResult.iterations;
6060
} else {

src/methods/newtonRaphsonScript.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { basicLog, debugLog, errorLog } from "./utilities/loggingScript.js";
1818
* @param {number} [maxIterations=100] - Maximum number of iterations
1919
* @param {number} [tolerance=1e-7] - Convergence tolerance
2020
* @returns {object} An object containing:
21-
* - solution: The solution vector
21+
* - solutionVector: The solution vector
2222
* - iterations: The number of iterations performed
2323
* - converged: Boolean indicating whether the method converged
2424
*/
@@ -28,21 +28,21 @@ export function newtonRaphson(assembleMat, context, maxIterations = 100, toleran
2828
let converged = false;
2929
let iterations = 0;
3030
let deltaX = [];
31-
let solution = [];
31+
let solutionVector = [];
3232
let jacobianMatrix = [];
3333
let residualVector = [];
3434
let nodesCoordinates = {};
3535

3636
// Initialize solution and deltaX
3737
for (let i = 0; i < residualVector.length; i++) {
38-
solution[i] = 0;
38+
solutionVector[i] = 0;
3939
deltaX[i] = 0;
4040
}
4141

42-
while (iterations <= maxIterations && !converged) {
42+
while (iterations < maxIterations && !converged) {
4343
// Update solution
44-
for (let i = 0; i < solution.length; i++) {
45-
solution[i] = solution[i] + deltaX[i];
44+
for (let i = 0; i < solutionVector.length; i++) {
45+
solutionVector[i] = solutionVector[i] + deltaX[i];
4646
}
4747

4848
// Compute Jacobian and Residual matrices
@@ -80,7 +80,7 @@ export function newtonRaphson(assembleMat, context, maxIterations = 100, toleran
8080
debugLog(`Newton-Raphson ${converged ? "converged" : "did not converge"} in ${iterations} iterations`);
8181

8282
return {
83-
solution,
83+
solutionVector,
8484
converged,
8585
iterations,
8686
jacobianMatrix,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy