Advertisement
Guest User

Lab1-2

a guest
May 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 7.28 KB | None | 0 0
  1. import com.quantifind.charts.Highcharts._
  2.  
  3. trait InitialVals {
  4.   def x0: Double = 1
  5.   def x1: Double = 2
  6.   def y0: Double = 1
  7.   def z0: Double = 0.5
  8.   def makeXList(n: Int): List[Double] = {
  9.     val h = (x1 - x0) / n
  10.     val l = List(x0)
  11.     def process(i: Int, acc: List[Double]): List[Double] = {
  12.       if (i > 1) process(i - 1, acc :+ acc(n-i) + h) else acc
  13.     }
  14.     process(n, l)
  15.   }
  16. }
  17.  
  18. object DefEuler extends InitialVals {
  19.   def perform(n: Int): (List[Double], List[Double]) = {
  20.     val l     = (List[Double](), List[Double]())
  21.     val h     = (x1 - x0) / n
  22.     val xi    = x0 + (_: Int) * h
  23.     val evalY = (y: Double, z: Double) => y + h * z
  24.     val evalZ = (i: Int, y: Double, z: Double) =>
  25.       z + h * ((1 / (4 * scala.math.pow(y, 3))) - (y / (4 * scala.math.pow(xi(i), 2))))
  26.  
  27.     def process(i: Int, y: Double, z: Double, acc: (List[Double], List[Double])): (List[Double], List[Double]) = {
  28.       println(y + " " + z)
  29.       val nextY = evalY(y, z)
  30.       val nextZ = evalZ(i, y, z)
  31.       if (i < n) process(i + 1, nextY, nextZ, (acc._1 :+ nextY, acc._2 :+  nextZ)) else acc
  32.     }
  33.     process(0, y0, z0, l)
  34.   }
  35. }
  36.  
  37. object Euler extends InitialVals {
  38.   def perform(n: Int): (List[Double], List[Double]) = {
  39.     val l     = (List[Double](1.0), List[Double](0.5))
  40.     val h     = (x1 - x0) / n
  41.     val xi    = x0 + (_: Int) * h
  42.  
  43.     def process(i: Int,
  44.                 y: Double,
  45.                 z: Double,
  46.                 acc: (List[Double], List[Double]))
  47.               : (List[Double], List[Double]) = {
  48.       def newtone(y: Double, z: Double, x: Double, count: Int): (Double, Double) = {
  49.         println(count)
  50.  
  51.         val f1 = y - acc._1(i) - h * z
  52.         val f2 = z - acc._2(i) - h * (1 / (4 * scala.math.pow(y, 3)) - (y / (4 * scala.math.pow(x, 2))))
  53.  
  54.         val df1dy = 1
  55.         val df2dy = (h / 4) * ((3 / scala.math.pow(y, 4) + (1 / scala.math.pow(x, 2))))
  56.         val df1dz = -h
  57.         val df2dz = 1
  58.  
  59.         val detJ = df1dy * df2dz - df1dz * df2dy
  60.         val detA1 = f1 * df2dz - df1dz * f2
  61.         val detA2 = df1dy * f2 - f1 * df2dy
  62.  
  63.         val nextY = y - detA1 / detJ
  64.         val nextZ = z - detA2 / detJ
  65.  
  66.         def newtoneCheck(a: Double, nextA: Double): Boolean = {
  67.           val eps = 0.00000001
  68.           scala.math.abs((nextA - a) / a ) < eps
  69.         }
  70.  
  71.         if (newtoneCheck(y, nextY) && newtoneCheck(z, nextZ)) (nextY, nextZ) else newtone(nextY, nextZ, x, count + 1)
  72.       }
  73.  
  74.       val nextY = newtone(y, z, xi(i), 0)._1
  75.       val nextZ = newtone(y, z, xi(i), 0)._2
  76.  
  77.       if (i < n - 1) process(i + 1, nextY, nextZ, (acc._1 :+ nextY, acc._2 :+ nextZ)) else acc
  78.     }
  79.     process(0, y0, z0, l)
  80.   }
  81. }
  82.  
  83. object RungeFourth extends InitialVals {
  84.   def perform(n: Int): (List[Double], List[Double]) = {
  85.     val l     = (List[Double](), List[Double]())
  86.     val h     = (x1 - x0) / n
  87.  
  88.     //def k1(x: Double, y: Double, f: (Double, Double) => Double): Double = f(x, y)
  89.     //def k2(x: Double, y: Double, f: (Double, Double) => Double): Double = f(x + h / 2, y + h * k1(x, y, f) / 2)
  90.     //def k3(x: Double, y: Double, f: (Double, Double) => Double): Double = f(x + h / 2, y + h * k2(x, y, f) / 2)
  91.     //def k4(x: Double, y: Double, f: (Double, Double) => Double): Double = f(x + h, y + h * k3(x, y, f))
  92.     def k1(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x, y)
  93.     def k2(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x + h / 4, y + k1(x, y, f) / 4)
  94.     def k3(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x + h / 2, y + k2(x, y, f) / 2)
  95.     def k4(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x + h, y + k1(x, y, f) - 2 * k2(x, y, f) + k3(x, y, f))
  96.  
  97.     def fi(fPrevious: Double, x: Double, y: Double, f: (Double, Double) => Double): Double = {
  98.       //fPrevious + ((h / 6) * (k1(x, y, f) + 2 * k2(x, y, f) + 2 * k3(x, y, f) + k4(x, y, f)))
  99.       fPrevious + ((1.0 / 6) * (k1(x, y, f) + 4 * k3(x, y, f) + k4(x, y, f)))
  100.     }
  101.  
  102.     def process(i: Int, y: Double, z: Double, acc: (List[Double], List[Double])): (List[Double], List[Double]) = {
  103.       val xi    = x0 + i * h
  104.       val evalY = (y: Double, z: Double) => z
  105.       val evalZ = (y: Double, z: Double) => ((1 / (4 * scala.math.pow(y, 3))) - (y / (4 * scala.math.pow(xi, 2))))
  106.  
  107.       val nextY = fi(y, y, z, evalY)
  108.       val nextZ = fi(z, y, z, evalZ)
  109.  
  110.       if (i < n) process(i + 1, nextY, nextZ, (acc._1 :+ nextY, acc._2 :+  nextZ)) else acc
  111.     }
  112.     process(0, y0, z0, l)
  113.   }
  114. }
  115.  
  116. object RungeFifth extends InitialVals {
  117.   def perform(n: Int): (List[Double], List[Double]) = {
  118.     val l     = (List[Double](), List[Double]())
  119.     val h     = (x1 - x0) / n
  120.  
  121.     def k1(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x, y)
  122.     def k2(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x + h / 2, y + k1(x, y, f) / 2)
  123.     def k3(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x + h / 2, y + k1(x, y, f) / 4 + k2(x, y, f) / 4)
  124.     def k4(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x + h, y - k2(x, y, f) + 2 * k3(x, y, f))
  125.     def k5(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x + (2.0 / 3.0) * h, y + (7.0 / 27.0) * k1(x, y, f) + (10.0 / 27.0) * k2(x, y, f) + (1.0 / 27.0) * k4(x, y, f))
  126.     def k6(x: Double, y: Double, f: (Double, Double) => Double): Double = h * f(x + (1.0 / 5.0) * h, y - (1.0 / 625.0) * (28 * k1(x, y, f) - 125 * k2(x, y, f) + 546 * k3(x, y, f) + 54 * k4(x, y, f) + 378 * k5(x, y, f)))
  127.  
  128.     def fi(fPrevious: Double, x: Double, y: Double, f: (Double, Double) => Double): Double = {
  129.       fPrevious + ((1.0 / 336) * (14 * k1(x, y, f) + 35 * k4(x, y, f) + 162 * k5(x, y, f) + 125 * k6(x, y, f)))
  130.     }
  131.  
  132.     def process(i: Int, y: Double, z: Double, acc: (List[Double], List[Double])): (List[Double], List[Double]) = {
  133.       val xi    = x0 + i * h
  134.       val evalY = (y: Double, z: Double) => z
  135.       val evalZ = (y: Double, z: Double) => ((1 / (4 * scala.math.pow(y, 3))) - (y / (4 * scala.math.pow(xi, 2))))
  136.  
  137.       val nextY = fi(y, y, z, evalY)
  138.       val nextZ = fi(z, y, z, evalZ)
  139.  
  140.       if (i < n) process(i + 1, nextY, nextZ, (acc._1 :+ nextY, acc._2 :+  nextZ)) else acc
  141.     }
  142.     process(0, y0, z0, l)
  143.   }
  144. }
  145.  
  146. object Comparator {
  147.   def fetch(l1: List[Double], l2: List[Double], eps: Double): Unit = {
  148.     def process(i: Int, l1: List[Double], l2: List[Double]): Unit = {
  149.       if (l1.length > 0) {
  150.         if (l1.head - l2.head >= eps) println(i + " " + l1.head + " " + l2.head)
  151.         process(i + 1, l1.tail, l2.tail)
  152.       }
  153.     }
  154.     process(0, l1, l2)
  155.   }
  156. }
  157.  
  158. object Main extends App with InitialVals {
  159.   val num = args(0).toInt
  160.   val eps = args(1).toDouble
  161.   val result0 = RungeFourth.perform(num)
  162.   //val result = DefEuler.perform(num)
  163.   val result = RungeFifth.perform(num)
  164.   val resultX = makeXList(num)
  165.   val resultY = result._1
  166.   val resultY0 = result0._1
  167.   val resultZ0 = result0._2
  168.   val resultZ = result._2
  169.  
  170.   println("Comparing Y's results of two methods")
  171.   Comparator.fetch(result._1, result0._1, eps)
  172.   println("Comparing Z's results of two methods")
  173.   Comparator.fetch(result._2, result0._2, eps)
  174.  
  175.   line(resultX, resultY)
  176.   hold
  177.   line(resultX, resultY0)
  178.   unhold
  179.   line(resultX, resultZ)
  180.   hold
  181.   line(resultX, resultZ0)
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement