Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.math._
- import util.control.Breaks._
- object Bisekcja extends App {
- def rownanie(x:Double):Double = sqrt(log(x+2))
- def oblicz(a:Double,b:Double): Unit ={
- var x1:Double = a
- var x2:Double = b
- var x:Double = 0
- var y:Double = 0
- var y1:Double = 0
- breakable {
- while (true) {
- println("x1 "+x1+" x2 "+x2)
- x = (x1 + x2) / 2
- y = rownanie(x)
- y1 = rownanie(x1)
- if (y.abs < 0.01) {
- println(x)
- break
- }
- if (y * y1 > 0)
- x1 = x
- else
- x2 = x
- }
- }
- }
- oblicz(-1.0,4.5)
- }
- -----------wynik-----------
- x1 -1.0 x2 4.5
- x1 -1.0 x2 1.75
- x1 -1.0 x2 0.375
- x1 -1.0 x2 -0.3125
- x1 -1.0 x2 -0.65625
- x1 -1.0 x2 -0.828125
- x1 -1.0 x2 -0.9140625
- x1 -1.0 x2 -0.95703125
- x1 -1.0 x2 -0.978515625
- x1 -1.0 x2 -0.9892578125
- x1 -1.0 x2 -0.99462890625
- x1 -1.0 x2 -0.997314453125
- x1 -1.0 x2 -0.9986572265625
- x1 -1.0 x2 -0.99932861328125
- x1 -1.0 x2 -0.999664306640625
- x1 -1.0 x2 -0.9998321533203125
- -0.9999160766601562
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement