Bisection Method

PowerScript

A classical numerical root-finding method based on bisection, with a maximum search complexity of O(logN).

main 0 files
Bisection Method..js
Bisection Method..js 1352 bytes
var $ = {

    "params": {
        "target": 10,
        "left": 0,
        "right": 10,
        "error": 1e-6,
        "mid": 0,
        "iteration": 0,
        "maxIteration": 1000
    },

    "bisectionMethod": function (func) {
        this.params.iteration = 0;

        var left = this.params.left;
        var right = this.params.right;
        var target = this.params.target;
        var error = this.params.error;
        var mid = 0;

        while ((right - left) > error && this.params.iteration < this.params.maxIteration) {
            this.params.iteration += 1;
            mid = (left + right) / 2;
            var value = func(mid);
            if (value < target) {
                left = mid;
            } else {
                right = mid;
            }
        }

        this.params.left = left;
        this.params.right = right;
        this.params.mid = (left + right) / 2;

        return this.params.mid;
    }
};

// Example Given

function testFunc(x) {
    return x*x ;
}

var result  = $.bisectionMethod(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: 2
Stars: 0