How to resolve the algorithm Runge-Kutta method step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Runge-Kutta method step by step in the Scala programming language

Table of Contents

Problem Statement

Given the example Differential equation: With initial condition: This equation has an exact solution:

Demonstrate the commonly used explicit   fourth-order Runge–Kutta method   to solve the above differential equation.

Starting with a given

y

n

{\displaystyle y_{n}}

and

t

n

{\displaystyle t_{n}}

calculate: then:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Runge-Kutta method step by step in the Scala programming language

Source code in the scala programming language

object Main extends App {
   val f = (t: Double, y: Double) => t * Math.sqrt(y) // Runge-Kutta solution
   val g = (t: Double) => Math.pow(t * t + 4, 2) / 16 // Exact solution
   new Calculator(f, Some(g)).compute(100, 0, .1, 1)
}

class Calculator(f: (Double, Double) => Double, g: Option[Double => Double] = None) {
   def compute(counter: Int, tn: Double, dt: Double, yn: Double): Unit = {
      if (counter % 10 == 0) {
         val c = (x: Double => Double) => (t: Double) => {
            val err = Math.abs(x(t) - yn)
            f" Error: $err%7.5e"
         }
         val s = g.map(c(_)).getOrElse((x: Double) => "") // If we don't have exact solution, just print nothing
         println(f"y($tn%4.1f) = $yn%12.8f${s(tn)}") // Else, print Error estimation here
      }
      if (counter > 0) {
         val dy1 = dt * f(tn, yn)
         val dy2 = dt * f(tn + dt / 2, yn + dy1 / 2)
         val dy3 = dt * f(tn + dt / 2, yn + dy2 / 2)
         val dy4 = dt * f(tn + dt, yn + dy3)
         val y = yn + (dy1 + 2 * dy2 + 2 * dy3 + dy4) / 6
         val t = tn + dt
         compute(counter - 1, t, dt, y)
      }
   }
}


  

You may also check:How to resolve the algorithm Abelian sandpile model step by step in the J programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the Forth programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Maxima programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Haskell programming language