Skip to content

Commit

Permalink
modified: readme.md
Browse files Browse the repository at this point in the history
	modified:   vis2.html
  • Loading branch information
Your Name committed May 8, 2024
1 parent 0b1b4f7 commit 5b85955
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
14 changes: 9 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Getting Started
```bash
npm install levenbergmarquardt.js@1.0.3
npm config set registry https://registry.npmjs.org/
npm i levenbergmarquardt.js
```

```html
<script src="https://cdn.jsdelivr.net/gh/pterodactylus/levenbergmarquardt.js@latest/js/lm.min.js"></script>
<script type="module">
import LevenbergMarquardtSolver from "https://cdn.jsdelivr.net/gh/pterodactylus/levenbergmarquardt.js@latest/js/lm.min.js"
//Your code here....
</script>
```

# Levenberg Marquardt Solver Examples
Expand All @@ -31,7 +35,7 @@ const equations = (vars) => [
];

const initialGuess = [0, 0];
const {solution, status, iterationData} = MultiFunctionSolver.solve(equations, initialGuess);
const {solution, status, iterationData} = LevenbergMarquardtSolver.solve(equations, initialGuess);
console.log('Solution:', solution);
```

Expand All @@ -57,7 +61,7 @@ const nonlinearEquations = (vars) => [
];

const initialGuessNonlinear = [1, 1];
const {solution, status, iterationData} = MultiFunctionSolver.solve(nonlinearEquations, initialGuessNonlinear);
const {solution, status, iterationData} = LevenbergMarquardtSolver.solve(nonlinearEquations, initialGuessNonlinear);
console.log('Nonlinear Solution:', solution);
```

Expand All @@ -72,6 +76,6 @@ const yData = [1, 2.1, 3.9, 8, 15.9];
const fittingFunction = (params) => xData.map((x, idx) => params[0] * x**2 + params[1] * x + params[2] - yData[idx]);

const initialGuessFitting = [1, 1, 1];
const {solution, status, iterationData} = MultiFunctionSolver.solve(fittingFunction, initialGuessFitting);
const {solution, status, iterationData} = LevenbergMarquardtSolver.solve(fittingFunction, initialGuessFitting);
console.log('Fitting Parameters:', solution);
```
30 changes: 12 additions & 18 deletions vis2.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@
<meta charset="UTF-8">
<title>Combined 3D Visualization with Plotly.js</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/gh/pterodactylus/levenbergmarquardt.js@latest/js/lm.min.js"></script>
</head>
<body>

<div id="combinedPlot" style="width: 800px; height: 600px;"></div>

<script>
const xData = [0, 1, 2, 3, 4];
const yData = [1, 2.1, 3.9, 8, 15.9];
<script type="module">
import LevenbergMarquardtSolver from "https://cdn.jsdelivr.net/gh/pterodactylus/levenbergmarquardt.js@latest/js/lm.min.js"

const nonlinearEquations = (vars) => [
vars[0]**2 + vars[1]**2 - 4,
Math.exp(vars[0]) + vars[1] - 1
];

const fittingFunction = (params) => xData.map((x, idx) => params[0] * x**2 + params[1] * x + params[2] - yData[idx]);

const initialGuessFitting = [1, 1, 1];
const {solution, status, iterationData} = MultiFunctionSolver.solve(fittingFunction, initialGuessFitting);
console.log('Fitting Parameters:', solution);

// Data for 3D Scatter (representing the line chart in a 3D format)
//const iter = [0, 1, 2, 3, 4, 5, 6, 7, 8];
//const fNorm = [3, 3, 3, 3, 1.682763522367352, 0.3160487989924193, 0.00616001239234269, 0.0000027751707191603792, 1.3991977785363838e-11];
//const xCoords = [1, 1, 1, 1, 1.176470589147818, 0.6120191709783461, 0.599549375302035, 0.5995281496062017, 0.5995282992838651];
//const yCoords = [1, 1, 1, 1, 1.7058823556624063, 1.7029528779641305, 1.6687104762232532, 1.6679783312915795, 1.6679779773227792];
const initialGuessNonlinear = [1, 1];
const {solution, status, iterationData} = LevenbergMarquardtSolver.solve(nonlinearEquations, initialGuessNonlinear);

const fNorm = iterationData.map(point => point.fNorm);
const xCoords = iterationData.map(point => point.x[0]);
const yCoords = iterationData.map(point => point.x[1]);

function make2DSurfaceArr(startX, stopX, pointsX, startY, stopY, pointsY, fNorm) {
function make2DSurfaceArr(startX, stopX, pointsX, startY, stopY, pointsY, f) {
let arrX = [];
let arrY = [];
let arrZ = [];
Expand All @@ -47,7 +41,7 @@
for (let j = 0; j < pointsY; j++) {
arrZ.push([])
for (let i = 0; i < pointsX; i++) {
let z = fNorm([arrX[i], arrY[j]])
let z = LevenbergMarquardtSolver.norm(f([arrX[i], arrY[j]]))
arrZ[arrZ.length-1].push(z);
//console.log("x[", i,"]=", arrX[i], "y[", j,"]=", arrY[j], "z[", j,", ", i,"]=", arrZ[j][i])
}
Expand All @@ -62,7 +56,7 @@
let d = 0.2;
let rangeX = (maxx - minx)*d;
let rangeY = (maxy - miny)*d;
let arr = make2DSurfaceArr(minx-rangeX, maxx+rangeX, 100, miny-rangeY, maxy+rangeY, 100, fn);
let arr = make2DSurfaceArr(minx-rangeX, maxx+rangeX, 100, miny-rangeY, maxy+rangeY, 100, nonlinearEquations);

const scatter3d = {
x: xCoords,
Expand Down

0 comments on commit 5b85955

Please sign in to comment.