A
A
Anya Frolova2020-12-15 00:28:53
Programming
Anya Frolova, 2020-12-15 00:28:53

What should the algorithm for the half-division method look like in a programming language?

Algorithm of the bisection method:

Enter the segment [a,b]

Enter eps (sufficiently small value)

Calculate f(a) and f(b)

Loop while abs(ab)>eps

c=(a+b)/2

If f(a )f(b)<0

b=c

f(b)=f(c)

Else

a=c

f(a)=f(c) Allif

Allloop

x

= c see? Any language interested in the possibility of reducing the amount of code while maintaining its performance.


Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-12-15
@vabka

I'm not sure what language you want, so I'll describe it in F#

open System

let epsilon = Double.Epsilon
let abs: float -> float = Math.Abs
let equals a b = abs (a - b) <= epsilon

let rec findRoot f segment =
    let bisect f (a, b) =
        let avg = (a + b) / 2.0
        if ((f a) * (f b)) < 0.0 then // знак f(a) != знак f(b)
            a, avg
        else
            avg, b

    let a, b = bisect f segment
    if equals a b then a else findZero f (a, b)

let segment = -10.0, 10.0 // Отрезок [-10,10]
let f x = x + 5.0 // Пусть f(x) = x + 5
let result = findRoot f segment
printfn "%f" result

Or even shorter in the Wolfram language
ResourceFunction["BisectionMethodFindRoot"][x + 5, {x, -10, 10}, 5, 100]

https://resources.wolframcloud.com/FunctionReposit...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question