Secant Method
PowerScript
A Classical root-finding method using secant approximation without explicit derivatives.
main
0 files
Secant Method..js
Secant Method..js
1298 bytes
var $ = {
"params": {
"target": 10,
"x0": 0,
"x1": 10,
"error": 1e-6,
"iteration": 0,
"maxIteration": 1000
},
"secantMethod": function (func) {
this.params.iteration = 0;
var target = this.params.target;
var error = this.params.error;
var x0 = this.params.x0;
var x1 = this.params.x1;
while (Math.abs(x1 - x0) > error && this.params.iteration < this.params.maxIteration) {
this.params.iteration += 1;
var f0 = func(x0) - target;
var f1 = func(x1) - target;
if (Math.abs(f1 - f0) < error) {
break;
}
var x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
x0 = x1;
x1 = x2;
}
this.params.x0 = x0;
this.params.x1 = x1;
return x1;
}
};
// Example Given
function testFunc(x) {
return x*x ;
}
var result = $.secantMethod(testFunc);
var target = $.params.target;
var estimate = testFunc(result);
var error = target - estimate;
print("result :" + result);
print("estimate :" + estimate);
print("target :" + target);
print("error :" + error);
print("iteration :" + $.params.iteration);
Comments (0)
No comments yet. Be the first to comment!
Snippet Information
Author:
jungyu.lee
Language: PowerScript
Created:
May 11, 2026
Updated:
0 minutes ago
Views: 5
Stars: 0