### live coding examles

parent a7b39867
 import scala.math._ def limit4(f:Double=>Double,dx:Double,dy:Double,a:Double):Double = { val f1 = f(a + dx) val f2 = f(a + dx / 2) if (abs(f1 - f2) < dy) f2 else limit4(f, dx / 2, dy, a) } limit4(cos,0.1,0.0001,0.0) limit4(sin,0.1,0.0001,0.0) limit4(cos,0.1,0.00001,Pi/4) def limit(dx: Double, dy: Double) = { limit4(_,dx,dy,_) } val limit2a = limit(0.1, 0.0001) val limit2b = limit(0.1, 0.000001) val limit2c = limit(0.1, 0.00000001) limit2a(sin , Pi) limit2b(sin , Pi) limit2c(sin , Pi) def f(x:Double):Double = { (x * x - 1) / (x - 1) } f(0) f(0.999999) limit2c(f,1.0) \ No newline at end of file
 import scala.math._ def almostEqual(epsilon:Double)(a:Double,b:Double):Boolean = { abs(a-b) < epsilon } def limit(f:Double=>Double,dx:Double,test:(Double,Double)=>Boolean)(a:Double):Double = { def recur(dx:Double):Double = { val f1 = f(a + dx) val f2 = f(a + dx / 2) if (test(f1,f2)) f2 else recur(dx / 2) } recur(dx) } limit(cos,0.1, almostEqual(0.0001))( 0.0) limit(sin,0.1, almostEqual(0.0001))( 0.0) limit(cos,0.1, almostEqual(0.00001))( Pi/4) val limit2a:(Double=>Double,Double)=>Double = limit(_,0.1, almostEqual(0.0001))(_) val limit2b = limit(_,0.1, almostEqual(0.0001)) _ val limit2c:(Double=>Double,Double)=>Double = limit(_,0.1, almostEqual(0.000001))(_) val limit2d:(Double=>Double,Double)=>Double = limit(_,0.1, almostEqual(0.00000001))(_) limit2a(sin , Pi) limit2b(sin , Pi) limit2c(sin , Pi) def f(x:Double):Double = { (x * x - 1) / (x - 1) } f(0) f(0.999999) limit2c(f,1.0) \ No newline at end of file
 import scala.math._ def almostEqual(epsilon:Double)(a:Double,b:Double):Boolean = { abs(a-b) < epsilon } def limit(f:Double=>Double,dx:Double,test:(Double,Double)=>Boolean)(a:Double):Double = { def recur(dx:Double):Double = { val f1 = f(a + dx) val f2 = f(a + dx / 2) if (test(f1,f2)) f2 else recur(dx / 2) } recur(dx) } def derivative(f:Double=>Double,dx:Double,test:(Double,Double)=>Boolean)(x:Double):Double = { def estimate(h:Double):Double = { (f(x+h) - f(x) ) / h } limit(estimate,dx:Double,test)(0) } val cos_est:Double=>Double = derivative(sin,0.01,almostEqual(0.000001)) // cos val poly_est:Double=>Double = derivative(x => x*x + 3*x + 1,0.01,almostEqual(0.00001)) // 2*x + 3 for {n <- 0 to 20 x = -Pi + n * 2 * Pi / 20 c1 = cos(x) c2 = cos_est(x) delta = c1 - c2 } println(s"\$x delta= \$delta") //derivative(sin)(Pi/2) //derivative(x => x*x + 3*x + 1)(1) \ No newline at end of file
 import scala.math._ def almostEqual(epsilon:Double)(a:Double,b:Double):Boolean = { abs(a-b) < epsilon } def limit(f:Double=>Double,dx:Double,test:(Double,Double)=>Boolean)(a:Double):Double = { def recur(dx:Double):Double = { val f1 = f(a + dx) val f2 = f(a + dx / 2) if (test(f1,f2)) f2 else recur(dx / 2) } recur(dx) } def derivative(f:Double=>Double,dx:Double,test:(Double,Double)=>Boolean)(x:Double):Double = { def estimate(h:Double):Double = { (f(x+h) - f(x) ) / h } limit(estimate,dx:Double,test)(0) } def integral(f:Double=>Double,left:Double,right:Double,test:(Double,Double)=>Boolean):Double = { def sumRectangles(partitionWidth: Double): Double = { val numPartitions = ((right - left) / partitionWidth).floor.toInt (1 to numPartitions).foldLeft(0.0)( (acc, i) => acc + f(left + i * partitionWidth) * partitionWidth ) } limit(sumRectangles,(right - left)/2,test)(0) } for {n <- 0 to 20 x = -Pi + n * 2 * Pi / 20 s1 = sin(x) s2 = integral(cos,0,x,almostEqual(0.00001)) delta = s1 - s2 } println(s"\$x delta= \$delta") \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!