Skip to content

Instantly share code, notes, and snippets.

@Humberd
Last active June 2, 2018 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Humberd/19805a4a7c4f2837c1851f32eb5d68c5 to your computer and use it in GitHub Desktop.
Save Humberd/19805a4a7c4f2837c1851f32eb5d68c5 to your computer and use it in GitHub Desktop.
fun main(args: Array<String>) {
val firstSection:Double = 1.0;
val lastSection:Double = 2.0;
val epsilon:Double = 0.00000001;
val b = Bisekcja(firstSection,lastSection,epsilon).also { }
println("Miejsce zerowe funkcji: " + b.zero() + ". Liczba iteracji: " + b.zero())
}
class Bisekcja(val poczatekPrzedzialu: Double, val koniecPrzedzialu: Double, val epsilon: Double) {
fun zero(): Double {
licznikIteracji++
if (Math.abs(f((poczatekPrzedzialu + koniecPrzedzialu) / 2)) < epsilon) {
return (poczatekPrzedzialu + koniecPrzedzialu) / 2
}
if (f(poczatekPrzedzialu) * f((poczatekPrzedzialu + koniecPrzedzialu) / 2) < 0) {
koniecPrzedzialu = (poczatekPrzedzialu + koniecPrzedzialu) / 2
return zero()
}
poczatekPrzedzialu = (poczatekPrzedzialu + koniecPrzedzialu) / 2
return zero()
}
fun f(x: Double): Double {
return Math.pow(x, 3.0) + Math.pow(x, 2.0) - 3 * x - 3.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment