How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the Tcl programming language
How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the Tcl programming language
Table of Contents
Problem Statement
For this, we first need to calculate the nodes and the weights, but after we have them, we can reuse them for numerious integral evaluations, which greatly speeds up the calculation compared to more simple numerical integration methods.
Task description Similar to the task Numerical Integration, the task here is to calculate the definite integral of a function
f ( x )
{\displaystyle f(x)}
, but by applying an n-point Gauss-Legendre quadrature rule, as described here, for example. The input values should be an function f to integrate, the bounds of the integration interval a and b, and the number of gaussian evaluation points n. An reference implementation in Common Lisp is provided for comparison. To demonstrate the calculation, compute the weights and nodes for an 5-point quadrature rule and then use them to compute:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the Tcl programming language
Source code in the tcl programming language
package require Tcl 8.5
package require math::special
package require math::polynomials
package require math::constants
math::constants::constants pi
# Computes the initial guess for the root i of a n-order Legendre polynomial
proc guess {n i} {
global pi
expr { cos($pi * ($i - 0.25) / ($n + 0.5)) }
}
# Computes and evaluates the n-order Legendre polynomial at the point x
proc legpoly {n x} {
math::polynomials::evalPolyn [math::special::legendre $n] $x
}
# Computes and evaluates the derivative of an n-order Legendre polynomial at point x
proc legdiff {n x} {
expr {$n / ($x**2 - 1) * ($x * [legpoly $n $x] - [legpoly [incr n -1] $x])}
}
# Computes the n nodes for an n-point quadrature rule. (i.e. n roots of a n-order polynomial)
proc nodes n {
set x [lrepeat $n 0.0]
for {set i 0} {$i < $n} {incr i} {
set val [guess $n [expr {$i + 1}]]
foreach . {1 2 3 4 5} {
set val [expr {$val - [legpoly $n $val] / [legdiff $n $val]}]
}
lset x $i $val
}
return $x
}
# Computes the weight for an n-order polynomial at the point (node) x
proc legwts {n x} {
expr {2.0 / (1 - $x**2) / [legdiff $n $x]**2}
}
# Takes a array of nodes x and computes an array of corresponding weights w
proc weights x {
set n [llength $x]
set w {}
foreach xi $x {
lappend w [legwts $n $xi]
}
return $w
}
# Integrates a lambda term f with a n-point Gauss-Legendre quadrature rule over the interval [a,b]
proc gausslegendreintegrate {f n a b} {
set x [nodes $n]
set w [weights $x]
set rangesize2 [expr {($b - $a)/2}]
set rangesum2 [expr {($a + $b)/2}]
set sum 0.0
foreach xi $x wi $w {
set y [expr {$rangesize2*$xi + $rangesum2}]
set sum [expr {$sum + $wi*[apply $f $y]}]
}
expr {$sum * $rangesize2}
}
puts "nodes(5) = [nodes 5]"
puts "weights(5) = [weights [nodes 5]]"
set exp {x {expr {exp($x)}}}
puts "int(exp,-3,3) = [gausslegendreintegrate $exp 5 -3 3]"
You may also check:How to resolve the algorithm Calculating the value of e step by step in the Python programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Events step by step in the Clojure programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the Factor programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Factor programming language