

* have left it longer to make it more readable. * Note that the code could be written a little more concisely, but I * assuming the function f(x) crosses the x-axis between those two points. * method, the code will determine a root of the equation between x1 and x2, * named "fx", then provide two starting values (x1 and x2) in the "main"

* In short, if you define a continuous function in the method below * which is also known as the "Bisection method". * This is the numerical analysis technique known as "Halving the interval", Without any further introduction, here is the source code for the Interval Halving method, written in Scala, but in a Java/OOP/imperative coding style: The interval-halving (bisection) method, Java/OOP style Please don’t think of the following code as “good code.” But if you’ll think of the following Scala code as “starter code” that kinda-sorta shows a solution, that would be kind. Note: I wrote all of the following code a few years before I wrote the Scala Cookbook, and my coding standards at that time were all over the place.

I’ll show the source code for both versions here. Considering that Scala is similar to the Java programming language, if anyone else needs the Interval-Halving method in Java, this code can easily be adapted to Java as well.Īfter writing the code first in what I’d call a “Java style,” I then modified the code to be a little more Scala-like. I just picked up an old college textbook named Applied Numerical Analysis, and curious to see what the Interval Halving method (also known as the Bisection Method) would look like in Scala, I decided to code it up.
