Skip to content

Instantly share code, notes, and snippets.

@Qyoom
Last active August 29, 2015 14:01
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 Qyoom/df95874f4da11db6cd7a to your computer and use it in GitHub Desktop.
Save Qyoom/df95874f4da11db6cd7a to your computer and use it in GitHub Desktop.
Calculates slope based on delta of x and slope function as x2 goes toward x1 and delta goes toward 0
object DerivativesLab {
import math._
def main(args: Array[String]): Unit = {
// Two different functions to derive derviatives for
def f1 = (x: Double) => pow(x,2)
def f2 = (x: Double) => pow(x,2) * 1.5
// series of test inputs, x1 and x2
val params = List((3,7), (7,3), (5,12), (-5,12))
// run and print results
params.foreach { x =>
val x1 = x._1
val x2 = x._2
// slope derivative results for each
println("derive slope for f1 with x1=" + x1 + " x2=" + x2)
println("final slope for f1: " + deriveSlope(f1)(x1, x2) + "\n")
println("derive slope for f2 with x1=" + x1 + " x2=" + x2)
println("final slope for f2: " + deriveSlope(f2)(x1, x2) + "\n")
}
}
val deltaThreshold = 0.000001
/* Calculates slope based on delta x and slope function as
* x2 goes toward x1 and delta goes toward 0
*/
def deriveSlope(f: Double => Double) (x1: Double, x2: Double): Double = {
def delta(x1: Double, x2: Double): Double = x2 - x1
def dx = delta(x1, x2)
val slope = (f(x1 + dx) - f(x1)) / dx
println("initial slope: " + slope)
// Returns reduced delta
def moveCloser(x1: Double, x2: Double): Double = {
val dif = abs(x1 - x2)
if(x2 > x1) x2 - dif * .99999
else x2 + dif * .99999
}
// Returns slope with reduced delta
def deriveSlope(x1: Double, x2: Double): Double = {
val newX2 = moveCloser(x1, x2)
val dx = delta(x1, newX2)
val slope = (f(x1 + dx) - f(x1)) / dx
println("derivative step: " + slope)
if(abs(dx) > deltaThreshold) deriveSlope(x1, newX2) // inner recursion
else slope
}
// Kick off deriveSlope
if(abs(dx) > deltaThreshold) deriveSlope(x1, x2)
else slope
} // end outer deriveSlope
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment