How to resolve the algorithm Numerical integration step by step in the Kotlin programming language
How to resolve the algorithm Numerical integration step by step in the Kotlin programming language
Table of Contents
Problem Statement
Write functions to calculate the definite integral of a function ƒ(x) using all five of the following methods: Your functions should take in the upper and lower bounds (a and b), and the number of approximations to make in that range (n). Assume that your example already has a function that gives values for ƒ(x) . Simpson's method is defined by the following pseudo-code:
Demonstrate your function by showing the results for:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numerical integration step by step in the Kotlin programming language
This Kotlin code implements numerical integration using different methods. Numerical integration is a technique used to estimate the area under the curve of a function.
Here's a detailed breakdown of the code:
-
Function Type Alias:
typealias Func = (Double) -> Double
This type alias defines a type
Func
that represents a function that takes aDouble
as an input and returns aDouble
as an output. This type alias is used to represent the function to be integrated. -
integrate
Function:fun integrate(a: Double, b: Double, n: Int, f: Func) {
The
integrate
function is the main integration function. It takes four parameters:a
: Lower bound of the integration interval.b
: Upper bound of the integration interval.n
: Number of subintervals to use for integration.f
: The function to be integrated, represented as aFunc
instance.
-
Initialization of
h
andsum
:val h = (b - a) / n val sum = DoubleArray(5)
h
is the step size or width of each subinterval.sum
is an array of doubles used to store the results of different integration methods.
-
Loop for Integration:
for (i in 0 until n) {
This loop iterates over each subinterval and calculates the values of the function at different points within the subinterval.
-
Calculation of Function Values:
val x = a + i * h sum[0] += f(x) sum[1] += f(x + h / 2.0) sum[2] += f(x + h) sum[3] += (f(x) + f(x + h)) / 2.0 sum[4] += (f(x) + 4.0 * f(x + h / 2.0) + f(x + h)) / 6.0
For each subinterval, it calculates the function values at different points and adds them to the corresponding elements of the
sum
array. These values are used to calculate the integrals using different methods. -
Printing the Results:
val methods = listOf("LeftRect ", "MidRect ", "RightRect", "Trapezium", "Simpson ") for (i in 0..4) println("${methods[i]} = ${"%f".format(sum[i] * h)}")
After the loop completes, it prints the results of the integration using different methods.
LeftRect
is the left-hand Riemann sum,MidRect
is the midpoint rule,RightRect
is the right-hand Riemann sum,Trapezium
is the trapezoidal rule, andSimpson
is Simpson's rule. -
main
Function:fun main(args: Array<String>) {
The
main
function is the entry point of the program. -
Integration Examples:
integrate(0.0, 1.0, 100) { it * it * it } integrate(1.0, 100.0, 1_000) { 1.0 / it } integrate(0.0, 5000.0, 5_000_000) { it } integrate(0.0, 6000.0, 6_000_000) { it }
These are examples of integration for different functions and different numbers of subintervals.
Overall, this code demonstrates how to numerically integrate a function using different methods and provides the results for specified intervals and functions.
Source code in the kotlin programming language
// version 1.1.2
typealias Func = (Double) -> Double
fun integrate(a: Double, b: Double, n: Int, f: Func) {
val h = (b - a) / n
val sum = DoubleArray(5)
for (i in 0 until n) {
val x = a + i * h
sum[0] += f(x)
sum[1] += f(x + h / 2.0)
sum[2] += f(x + h)
sum[3] += (f(x) + f(x + h)) / 2.0
sum[4] += (f(x) + 4.0 * f(x + h / 2.0) + f(x + h)) / 6.0
}
val methods = listOf("LeftRect ", "MidRect ", "RightRect", "Trapezium", "Simpson ")
for (i in 0..4) println("${methods[i]} = ${"%f".format(sum[i] * h)}")
println()
}
fun main(args: Array<String>) {
integrate(0.0, 1.0, 100) { it * it * it }
integrate(1.0, 100.0, 1_000) { 1.0 / it }
integrate(0.0, 5000.0, 5_000_000) { it }
integrate(0.0, 6000.0, 6_000_000) { it }
}
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the Factor programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the Objeck programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the FreeBASIC programming language