fork download
  1. fun f(x: Double): Double {
  2. return x * x + 2 * x
  3. }
  4.  
  5. fun trapezesMethod(a: Int, b: Int, n: Int): Double {
  6. val trapezeHeight = (b - a).toDouble() / n.toDouble()
  7. var area = 0.0
  8. var currentPoint = a.toDouble()
  9.  
  10. for (i in 1..n) {
  11. val trapezeFirstSide = f(currentPoint)
  12. currentPoint += trapezeHeight
  13. val trapezeSecondSide = f(currentPoint)
  14. area += ((trapezeFirstSide + trapezeSecondSide) * trapezeHeight) / 2
  15. }
  16.  
  17. return area
  18. }
  19.  
  20. fun main() {
  21. val a = 0
  22. val b = 10
  23. val n = 100
  24.  
  25. val area = trapezesMethod(a, b, n)
  26.  
  27. println(area)
  28. }
Success #stdin #stdout 0.08s 36648KB
stdin
Standard input is empty
stdout
433.34999999999906