0

次の仕様で二分法を実装します。

Input: Function f, values low and high, error range epsilon.
`enter code here`Precondition: low<high, f(low) and f(high) diffs on their signs; that is,
either: f(low)>0 and f(high)<0
or:     f(low)<0 and f(high)>0.
Output: x where |f(x)| < epsilon.

Test your implementation with the following input values:
f(x) = 2x^3-3x^2-17x-50
low = -10
high = 10
epsilon = 1*10^(-6)  

Run your program, print out the solution (approximation) one each iteration

私のコード、これが正しいかどうかわからない:

object IntervalHalving {

def main(args: Array[String]) {
  val low = -10
  val high = 10
  val epsilon = 1*10^(-6)

//top part seems correct//

//Not sure if i defined the function correctly//

  val f(x) = (x: Double) => x*x*x + x*x - 3*x-3

  val answer= halveTheInterval(f(x), low, high, epsilon)

   // print the answer
   println(answer)
}
4

2 に答える 2

0

ご了承ください

scala> val epsilon = 1e-6
epsilon: Double = 1.0E-6

上記のepsilon定義Intは値で評価されます-16 :)

scala> val f = (x: Double) => x*x*x + x*x - 3*x-3
f: Double => Double = <function1>

の代わりにval f(x) = ...、インスタンスも検討してくださいMath.pow(x,3)

の右中括弧を追加することを思い出してくださいobject IntervalHalving

于 2014-04-29T08:12:07.590 に答える
0

私はscalaについてあまり知りませんが、val epsilon = 1*10^(-6)おそらくあなたが考えていることではなく、xor(10,-6) = -16. 代わりに科学表記法を使用してください: val epsilon = 1.0e-6.

于 2014-04-29T00:35:06.710 に答える