Skip to content

Numerical

CalcpadCE treats numerical methods as first-class citizens: built-in operators such as $Slope, $Derivative, $Integral and $Find, paired with vector and matrix primitives, let you build classical algorithms from the ground up and watch every intermediate step.

This category collects recurring themes of applied analysis β€” root finding and eigenvalue extraction, quadrature and differentiation, regression and stochastic sampling β€” each captured in a self-contained, parametric worksheet. Iteration is treated as a visible artefact: Newton's method animates its convergence step-by-step, while Monte-Carlo experiments replace deterministic refinement with statistical accumulation. Comparison sheets pit competing formulas against analytical references, exposing the accuracy-versus-cost trade-off behind every algorithm.

Sample size, tolerances and iteration counts are exposed at the top of every sheet, so each worksheet doubles as a numerical playground for convergence, stability and round-off experiments.

Monte Carlo Pi

Estimates \(\pi\) by sampling random points in the unit square and counting those falling inside the quarter circle. Includes a live SVG plot of the sample cloud coloured by inside-versus-outside membership, so accuracy and variance can be eyeballed at any sample size.

Code:
'Number of points -'n = 10000
'Point coordinates:
x = random(fill(vector_hp(n); 1))
y = random(fill(Vector_hp(n); 1))
'Point radii -'r = sqrt(x^2 + y^2)
'Result -'n_in = count(floor(r); 0; 1)', 'Ο€_MC = 4*n_in/n
#val
#round 15
'<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="-0.1 -0.1 1.2 1.2">
#for i = 1 : n
    '<circle cx="'x.i'" cy="'1 - y.i'" r="0.002" stroke="none"
    #if r.i < 1
        'fill = "red"/>
    #else
        'fill = "blue"/>
    #end if
#loop
'<rect x="0" y="0" width="1" height="1" fill="none" stroke="black" stroke-width="0.005"/>
'<path d="M 0 0 A 1 1 0 0 1 1 1" fill="none" stroke="black" stroke-width="0.005"/>
'</svg>
Rendered Output:

Number of points - n = 10000

Point coordinates:

βƒ—x = randomβ€Š(β€Šfillβ€Š(β€Švectorhpβ€Š(β€Šnβ€Š)β€Š; 1β€Š)β€Šβ€Š)β€Š = randomβ€Š(β€Šfillβ€Š(β€Švectorhpβ€Š(β€Š10000β€Š)β€Š; 1β€Š)β€Šβ€Š)β€Š = [0.0543 0.183 0.254 0.164 0.207 0.471 0.815 0.575 0.612 0.762 0.814 0.256 0.597 0.152 0.0309 0.82 0.039 0.546 0.278 0.902 ... 0.146]

βƒ—y = randomβ€Š(β€Šfillβ€Š(β€ŠVectorhpβ€Š(β€Šnβ€Š)β€Š; 1β€Š)β€Šβ€Š)β€Š = randomβ€Š(β€Šfillβ€Š(β€ŠVectorhpβ€Š(β€Š10000β€Š)β€Š; 1β€Š)β€Šβ€Š)β€Š = [0.806 0.45 0.33 0.0765 0.322 0.297 0.466 0.445 0.781 0.936 0.449 0.95 0.256 0.266 0.546 0.419 0.271 0.405 0.381 0.941 ... 0.896]

Point radii - βƒ—r =    βˆšβƒ—xβŠ™β€‰2 + βƒ—yβŠ™β€‰2 = [0.808 0.486 0.416 0.181 0.383 0.557 0.939 0.728 0.992 1.21 0.929 0.984 0.65 0.306 0.547 0.921 0.274 0.679 0.472 1.3 ... 0.908]

Result - nin = countβ€Š(β€Šfloorβ€Š(β€Šβƒ—rβ€Š)β€Š; 0; 1β€Š)β€Š = 7903 , Ο€MC = 4 · ninn = 4 · 790310000 = 3.16

Newton's Method 🎬

Solves \(x^x = 10\) with Newton iteration and renders an animated SVG that highlights each tangent step. The frame loop turns convergence into a visual sequence rather than a printed iteration table.

Code:
#md on
#noc
'**Problem:** Given the following equation:'x^x = 10'. Find'x'= ?              </p><p>
#equ
'**Solution:** Transform the equation in the form:'f(x) = x^x - 10'= 0</p>
'The first derivative is'fβ€²(x) = x^x*(ln(x) + 1)
'Initial guess -'x_0 = 4', target precision -'Ξ΄ = 10^-14
'#### Animation
'<style>#start{height:1.8em; padding-top:0;} .fr{display:none;} .Series3, .Series6{opacity:0;} .Series7{stroke-width:6pt;}</style>
#hide
#val
PlotWidth = 500','PlotHeight = 250','PlotSVG = 1','PlotAdaptive = 0
n = 100','x = x_0','a = 2','b = 4
#for i = 1 : n
    y_0 = f(x_0)
    #if iβ¦Ό2 ≑ 0
        x = x_0 - f(x_0)/fβ€²(x_0)
        y = f(x)
        y(x) = y_0 + (x - x_0)*fβ€²(x_0)
        #show
        '<div class="fr" id="fr-'i'">
        $Plot{f(ΞΎ) & ΞΎ|if(ΞΎ β‰₯ x ∧ ΞΎ ≀ x_0; y(ΞΎ); 1/0) & 2|-50 & x|(ΞΎ - a)*y/(b - a) & x|y & x_0|y_0 & x_0|y_0 @ ΞΎ = a : b}
        '</div>
        #hide
        x_0 = x
    #else
        #if abs(y_0) < Ξ΄
            n = i - 4
            #break
        #end if
        #show
        '<div class="fr" id="fr-'i'">
        $Plot{f(ΞΎ) & x_0|y_0 & 2|-50 & x_0|(ΞΎ - a)*y_0/(b - a) & x_0|y_0 & x_0|y_0 & x_0|y_0 @ ΞΎ = a : b}
        '</div>
        #hide
    #end if
#loop
#show
#equ
'After 'n' iterations we get:'x
'Check -'x^x', Error -'abs(x^x - 10)'<'Ξ΄
#val
'<script>(function(){$("#fr-1").show();var i=1;var fr=$("#fr-1");setInterval(function(){fr.hide();if(++i>'n')i=1;fr=$("#fr-"+i);fr.show();},1000);})();</script>
Rendered Output:

Problem: Given the following equation: xx = 10 . Find x = ?

Solution: Transform the equation in the form: fβ€Š(β€Šxβ€Š)β€Š = xx βˆ’ 10 = 0

The first derivative is fβ€²β€Š(β€Šxβ€Š)β€Š = xxβ€†Β·β€†β€Š(β€Šlnβ€Š(β€Šxβ€Š)β€Š + 1β€Š)β€Š

Initial guess - x0 = 4 , target precision - Ξ΄ = 10-14

Animation
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]
-50 -25 0 25 50 75 100 125 150 175 200 225 250 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 x [2; -50] [4; 246]

After n = 13 iterations we get: x = 2.51

Check - xx = 2.512.51 = 10 , Error - |xx βˆ’ 10 | = | 2.512.51 βˆ’ 10 | = 8.88Γ—10-15 < Ξ΄ = 10-14

Least Squares Fitting

Fits a 10-point dataset with linear, quadratic and quintic polynomial models built from the normal equations \(X^T\, X\, a = X^T y\). All three curves are then plotted together so the residual pattern of each model is immediately visible.

Code:
'Point coordinates
x = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9]
y = [400; 450; 440; 400; 380; 330; 250; 200; 150; 160]
'Number of points -'n = len(x)
e = fill(vector(n); 1)
'<h4>Linear fit</h4>
X = [e|x]
A = copy(X*transp(X); symmetric(n_rows(X)); 1; 1)
b = X*y
a_L = clsolve(A; b)
P_L(x) = dot(a_L; [1; x])
'<h4>Quadratic fit</h4>
X = [e|x|x^2]
A = copy(X*transp(X); symmetric(n_rows(X)); 1; 1)
b = X*y
a_Q = clsolve(A; b)
P_Q(x) = dot(a_Q; [1; x; x^2])
'<h4>Poly(5) fit</h4>
X = [e|x|x^2|x^3|x^4|x^5]
A = copy(X*transp(X); symmetric(n_rows(X)); 1; 1)
b = X*y
a_C = clsolve(A; b)
P_C(x) = dot(a_C; [1; x; x^2; x^3; x^4; x^5])
'<h4>Plot</h4>
#hide
PlotWidth = 250
PlotHeight = 150
#def P$ = x.1|y.1 & x.2|y.2 & x.3|y.3 & x.4|y.4 & x.5|y.5 & x.6|y.6 & x.7|y.7 & x.8|y.8 & x.9|y.9 & x.10|y.10
#show
$Plot{ΞΎ|P_L(ΞΎ) & P$ @ ΞΎ = x.1 : x.n}
$Plot{ΞΎ|P_Q(ΞΎ) & P$ @ ΞΎ = x.1 : x.n}
$Plot{ΞΎ|P_C(ΞΎ) & P$ @ ΞΎ = x.1 : x.n}
Rendered Output:

Point coordinates

βƒ—x = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] = [0 1 2 3 4 5 6 7 8 9]

βƒ—y = [400; 450; 440; 400; 380; 330; 250; 200; 150; 160] = [400 450 440 400 380 330 250 200 150 160]

Number of points - n = lenβ€Š(β€Šβƒ—xβ€Š)β€Š = 10

βƒ—e = fillβ€Š(β€Švectorβ€Š(β€Šnβ€Š)β€Š; 1β€Š)β€Š = fillβ€Š(β€Švectorβ€Š(β€Š10β€Š)β€Š; 1β€Š)β€Š = [1 1 1 1 1 1 1 1 1 1]

Linear fit

X = [βƒ—e | βƒ—x] = 1111111111 0123456789

A = copyβ€Š(β€ŠX · transpβ€Š(β€ŠXβ€Š)β€Š; symmetricβ€Š(β€Šnrowsβ€Š(β€ŠXβ€Š)β€Šβ€Š)β€Š; 1; 1β€Š)β€Š = 1045 45285

βƒ—b = X · ⃗y = [3160 11240]

βƒ—aL = clsolveβ€Š(β€ŠA; βƒ—bβ€Š)β€Š = [478.55 -36.12]

PLβ€Š(β€Šxβ€Š)β€Š = dotβ€Š(β€Šβƒ—aL; [1; x]β€Š)β€Š

Quadratic fit

X = [βƒ—e | βƒ—x | βƒ—xβŠ™β€‰2] = 1111111111 0123456789 0149162536496481

A = copyβ€Š(β€ŠX · transpβ€Š(β€ŠXβ€Š)β€Š; symmetricβ€Š(β€Šnrowsβ€Š(β€ŠXβ€Š)β€Šβ€Š)β€Š; 1; 1β€Š)β€Š = 1045285 452852025 285202515333

βƒ—b = X · ⃗y = [3160 11240 61500]

βƒ—aQ = clsolveβ€Š(β€ŠA; βƒ—bβ€Š)β€Š = [439 -6.46 -3.3]

PQβ€Š(β€Šxβ€Š)β€Š = dotβ€Š(β€Šβƒ—aQ; [1; x; x2]β€Š)β€Š

Poly(5) fit

X = [βƒ—e | βƒ—x | βƒ—xβŠ™β€‰2 | βƒ—xβŠ™β€‰3 | βƒ—xβŠ™β€‰4 | βƒ—xβŠ™β€‰5] = 1111111111 0123456789 0149162536496481 0182764125216343512729 0116812566251296240140966561 0132243102431257776168073276859049

A = copyβ€Š(β€ŠX · transpβ€Š(β€ŠXβ€Š)β€Š; symmetricβ€Š(β€Šnrowsβ€Š(β€ŠXβ€Š)β€Šβ€Š)β€Š; 1; 1β€Š)β€Š = 1045285202515333120825 45285202515333120825978405 2852025153331208259784058080425 202515333120825978405808042567731333 15333120825978405808042567731333574304985 1208259784058080425677313335743049854914341925

βƒ—b = X · ⃗y = [3160 11240 61500 396380 2811780 21200540]

βƒ—aC = clsolveβ€Š(β€ŠA; βƒ—bβ€Š)β€Š = [401.17 88.36 -54.04 12.51 -1.54 0.0731]

PCβ€Š(β€Šxβ€Š)β€Š = dotβ€Š(β€Šβƒ—aC; [1; x; x2; x3; x4; x5]β€Š)β€Š

Plot
PlotPlotPlot

Numerical Differentiation

Compares the $Slope operator (Richardson extrapolation) against $Derivative (complex-step method) on \(e^x\) and \(\sin(x^2)\) at large arguments. Exposes the round-off blow-up of finite differences and the machine-precision accuracy of the complex-step trick.

Code:
#md on
#rad
'Function -'f(x) = e^x', Point - 'a = 100
'Theoretical value -'fβ€²(x) = e^x', 'yβ€²_a = fβ€²(a)
'**$Slope** function (Richardson extrapolation) -'yβ€²_aS = $Slope{f(x) @ x = a}
'> Error -'Ξ΄ = abs(yβ€²_a - yβ€²_aS)/yβ€²_a|%'- <span class="err">Approx.</span>
'**$Derivative** function (Complex step method) -'yβ€²_aD = $Derivative{f(x) @ x = a}
'> Error -'Ξ΄ = abs(yβ€²_a - yβ€²_aD)/yβ€²_a|%'- <span class="ok">Exact!!!</span>
'Function -'f(x) = sin(x^2)', Point -'a = 1000
'Theoretical value -'fβ€²(x) = 2*x*cos(x^2)', 'yβ€²_a = fβ€²(a)
'**$Slope** function (Richardson extrapolation) -'yβ€²_aS = $Slope{f(x) @ x = a}
'> Error -'Ξ΄ = abs(yβ€²_a - yβ€²_aS)/yβ€²_a|%'- <span class="err">Approx.</span>
'**$Derivative** function (Complex step method) -'yβ€²_aD = $Derivative{f(x) @ x = a}
'> Error -'Ξ΄ = abs(yβ€²_a - yβ€²_aD)/yβ€²_a|%'- <span class="ok">Exact!!!</span>
Rendered Output:

Function - fβ€Š(β€Šxβ€Š)β€Š = ex , Point - a = 100

Theoretical value - fβ€²β€Š(β€Šxβ€Š)β€Š = ex , yβ€²a = fβ€²β€Šβ€Š(β€Šaβ€Š)β€Š = fβ€²β€Šβ€Š(β€Š100β€Š)β€Š = 2.69Γ—1043

$Slope function (Richardson extrapolation) - yβ€²aS = ddβ€Šx fβ€Šβ€Š(β€Šxβ€Š)β€Š|xβ€Š=β€Ša = 2.69Γ—1043

Error - Ξ΄ = |yβ€²a βˆ’ yβ€²aS|yβ€²a = | 2.69Γ—1043 βˆ’ 2.69Γ—1043|2.69Γ—1043 = 5.92Γ—10-10 % - Approx.

$Derivative function (Complex step method) - yβ€²aD = ddβ€Šx fβ€Šβ€Š(β€Šxβ€Š)β€Š|xβ€Š=β€Ša = 2.69Γ—1043

Error - Ξ΄ = |yβ€²a βˆ’ yβ€²aD|yβ€²a = | 2.69Γ—1043 βˆ’ 2.69Γ—1043|2.69Γ—1043 = 0 % - Exact!!!

Function - fβ€Š(β€Šxβ€Š)β€Š = sinβ€Š(β€Šx2β€Š)β€Š !!! , Point - a = 1000

Theoretical value - fβ€²β€Š(β€Šxβ€Š)β€Š = 2 · x · cosβ€Š(β€Šx2β€Š)β€Š , yβ€²a = fβ€²β€Šβ€Š(β€Šaβ€Š)β€Š = fβ€²β€Šβ€Š(β€Š1000β€Š)β€Š = 1873.5

$Slope function (Richardson extrapolation) - yβ€²aS = ddβ€Šx fβ€Šβ€Š(β€Šxβ€Š)β€Š|xβ€Š=β€Ša = 1873.5

Error - Ξ΄ = |yβ€²a βˆ’ yβ€²aS|yβ€²a = | 1873.5 βˆ’ 1873.5 |1873.5 = 1.15Γ—10-8 % - Approx.

$Derivative function (Complex step method) - yβ€²aD = ddβ€Šx fβ€Šβ€Š(β€Šxβ€Š)β€Š|xβ€Š=β€Ša = 1873.5

Error - Ξ΄ = |yβ€²a βˆ’ yβ€²aD|yβ€²a = | 1873.5 βˆ’ 1873.5 |1873.5 = 0 % - Exact!!!

Numerical Integration

Evaluates \(\int_{-1}^{1}\sqrt{1-x^2}\, dx\) with three quadrature rules β€” Boole (Newton-Cotes), Gauss-Legendre and tanh-sinh β€” at five nodes. Tabulates the relative error of each scheme against the analytical result \(\pi/2\).

Code:
'Function -'f(x) = sqrt(1 - x^2)
'Theoretical value of the integral - 'I = Ο€/2
'Number of nodes -'n = 5
'<h4>Booleβ€²s rule (Newton-Cotes for n = 4)</h4>
'<hr/>
'Step -'h = 0.5
'<p><strong>Abscissas,  Ordinates</strong></p>
x_1 = -1',  'y_1 = f(x_1)
x_2 = -0.5', 'y_2 = f(x_2)
x_3 = 0',  'y_3 = f(x_3)
'<p><strong>Integral</strong></p>
'Value -'I_B = 2*h/45*(2*7*y_1 + 2*32*y_2 + 12*y_3)
'Error -'Ξ΄_B = abs(I_B - I)/I|%
#hide
PlotWidth = 300
PlotHeight = 150
#show
'<p><strong>Scheme</strong></p>
$Plot{f(x) & x_1|0 & x_2|0 & x_3|0 & -x_2|0 & -x_1|0 @ x = -1 : 1}
'<h4>Gauss-Legendre rule</h4>
'<hr/>
'<p><strong>Abscissas, Ordinates and weights</strong></p>
x_1 = -(1/3)*sqrt(5 + 2*sqrt(10/7))', 'y_1 = f(x_1)', 'w_1 = (322 - 13*sqrt(70))/900
x_2 = -(1/3)*sqrt(5 - 2*sqrt(10/7))', 'y_2 = f(x_2)', 'w_2 = (322 + 13*sqrt(70))/900
x_3 = 0','y_3 = f(x_3)','w_3 = 128/225
'<p><strong>Integral</strong></p>
'Value -'I_G = 2*w_1*y_1 + 2*w_2*y_2 + w_3*y_3
'Error -'Ξ΄_G = abs(I_G - I)/I|%
'<p><strong>Scheme</strong></p>
$Plot{f(x) & x_1|0 & x_2|0 & x_3|0 & -x_2|0 & -x_1|0 @ x = -1 : 1}
'<h4>Tanh-sinh rule</h4>
'<hr/>
'Boundary of the interval -'t_a = 1
'Step -'h = 2*t_a/(n - 1)
'Parameters -'t(k) = -t_a + (k - 1)*h
'Function for abscissas -'x(k) = tanh(Ο€/2*sinh(t(k)))
'Weight function -'w(k) = Ο€*h*cosh(t(k))/(2*cosh(Ο€/2*sinh(t(k)))^2)
'<p><strong>Abscissas,       Ordinates,         Weights</strong></p>
x_1 = x(1)',  'y_1 = f(x_1)',  'w_1 = w(1)
x_2 = x(2)',  'y_2 = f(x_2)',  'w_2 = w(2)
x_3 = x(3)',      'y_3 = f(x_3)',    'w_3 = w(3)
'<p><strong>Integral</strong></p>
'Value -'I_DE = 2*w_1*y_1 + 2*w_2*y_2 + w_3*y_3
'Error -'Ξ΄_DE = abs(I_DE - I)/I|%
'<p><strong>Scheme</strong></p>
$Plot{f(x) & x_1|0 & x_2|0 & x_3|0 & -x_2|0 & -x_1|0 @ x = -1 : 1}
'<h4>CalcpadCE</h4>
'<hr/>
'Adaptive Tanh-Sinh -'I_ATH = $Integral{f(x) @ x = -1 : 1}
'Error -'Ξ΄_ATH = abs(I_ATH - I)/I|%
'Adaptive Gauss-Lobatto -'I_AGL = $Area{f(x) @ x = -1 : 1}
'Error -'Ξ΄_AGL = abs(I_AGL - I)/I|%
Rendered Output:

Function - fβ€Š(β€Šxβ€Š)β€Š =    √ 1 βˆ’ x2

Theoretical value of the integral - I = Ο€2 = 3.142 = 1.57

Number of nodes - n = 5

Booleβ€²s rule (Newton-Cotes for n = 4)

Step - h = 0.5

Abscissas,  Ordinates

x1 = -1 ,   y1 = fβ€Šβ€Š(β€Šx1β€Š)β€Š = fβ€Šβ€Š(β€Š-1β€Š)β€Š = 0

x2 = -0.5 ,  y2 = fβ€Šβ€Š(β€Šx2β€Š)β€Š = fβ€Šβ€Š(β€Š-0.5β€Š)β€Š = 0.866

x3 = 0 ,   y3 = fβ€Šβ€Š(β€Šx3β€Š)β€Š = fβ€Šβ€Š(β€Š0β€Š)β€Š = 1

Integral

Value - IB = 2 · h45β€†Β·β€†β€Š(β€Š2 · 7 · y1 + 2 · 32 · y2 + 12 · y3β€Š)β€Š = 2 · 0.545β€†Β·β€†β€Š(β€Š2 · 7 · 0 + 2 · 32 · 0.866 + 12 · 1β€Š)β€Š = 1.5

Error - Ξ΄B = |IB βˆ’ I|I = | 1.5 βˆ’ 1.57 |1.57 = 4.61 %

Scheme

Plot
Gauss-Legendre rule

Abscissas, Ordinates and weights

x1 = - 13 ·   5 + 2 ·   107 = -0.906 , y1 = fβ€Šβ€Š(β€Šx1β€Š)β€Š = fβ€Šβ€Š(β€Š-0.906β€Š)β€Š = 0.423 , w1 = 322 βˆ’ 13 ·    √ 70900 = 0.237

x2 = - 13 ·   5 βˆ’ 2 ·   107 = -0.538 , y2 = fβ€Šβ€Š(β€Šx2β€Š)β€Š = fβ€Šβ€Š(β€Š-0.538β€Š)β€Š = 0.843 , w2 = 322 + 13 ·    √ 70900 = 0.479

x3 = 0 , y3 = fβ€Šβ€Š(β€Šx3β€Š)β€Š = fβ€Šβ€Š(β€Š0β€Š)β€Š = 1 , w3 = 128225 = 0.569

Integral

Value - IG = 2 · w1 · y1 + 2 · w2 · y2 + w3 · y3 = 2 · 0.237 · 0.423 + 2 · 0.479 · 0.843 + 0.569 · 1 = 1.58

Error - Ξ΄G = |IG βˆ’ I|I = | 1.58 βˆ’ 1.57 |1.57 = 0.325 %

Scheme

Plot
Tanh-sinh rule

Boundary of the interval - ta = 1

Step - h = 2 · tan βˆ’ 1 = 2 · 15 βˆ’ 1 = 0.5

Parameters - tβ€Š(β€Škβ€Š)β€Š = -ta + β€Š(β€Šk βˆ’ 1β€Š)β€Šβ€†Β·β€†h

Function for abscissas - xβ€Š(β€Škβ€Š)β€Š = tanh(Ο€2 · sinhβ€Š(β€Štβ€Šβ€Š(β€Škβ€Š)β€Šβ€Š)β€Š)

Weight function - wβ€Š(β€Škβ€Š)β€Š = π · h · coshβ€Š(β€Štβ€Šβ€Š(β€Škβ€Š)β€Šβ€Š)β€Š2 · cosh(Ο€2 · sinhβ€Š(β€Štβ€Šβ€Š(β€Škβ€Š)β€Šβ€Š)β€Š)2

Abscissas,       Ordinates,       Weights

x1 = xβ€Šβ€Š(β€Š1β€Š)β€Š = -0.951 ,   y1 = fβ€Šβ€Š(β€Šx1β€Š)β€Š = fβ€Šβ€Š(β€Š-0.951β€Š)β€Š = 0.308 ,   w1 = wβ€Šβ€Š(β€Š1β€Š)β€Š = 0.115

x2 = xβ€Šβ€Š(β€Š2β€Š)β€Š = -0.674 ,   y2 = fβ€Šβ€Š(β€Šx2β€Š)β€Š = fβ€Šβ€Š(β€Š-0.674β€Š)β€Š = 0.738 ,   w2 = wβ€Šβ€Š(β€Š2β€Š)β€Š = 0.483

x3 = xβ€Šβ€Š(β€Š3β€Š)β€Š = 0 ,      y3 = fβ€Šβ€Š(β€Šx3β€Š)β€Š = fβ€Šβ€Š(β€Š0β€Š)β€Š = 1 ,     w3 = wβ€Šβ€Š(β€Š3β€Š)β€Š = 0.785

Integral

Value - IDE = 2 · w1 · y1 + 2 · w2 · y2 + w3 · y3 = 2 · 0.115 · 0.308 + 2 · 0.483 · 0.738 + 0.785 · 1 = 1.57

Error - Ξ΄DE = |IDE βˆ’ I|I = | 1.57 βˆ’ 1.57 |1.57 = 0.0751 %

Scheme

Plot
CalcpadCE

Adaptive Tanh-Sinh - IATH =   1∫-1 fβ€Šβ€Š(β€Šxβ€Š)β€Š dx = 1.57

Error - Ξ΄ATH = |IATH βˆ’ I|I = | 1.57 βˆ’ 1.57 |1.57 = 2.83Γ—10-14 %

Adaptive Gauss-Lobatto - IAGL =   1∫-1 fβ€Šβ€Š(β€Šxβ€Š)β€Š dx = 1.57

Error - Ξ΄AGL = |IAGL βˆ’ I|I = | 1.57 βˆ’ 1.57 |1.57 = 0 %

Rayleigh Quotient Iteration

Computes the dominant eigenpair of a \(3\times 3\) symmetric matrix via Rayleigh-quotient iteration. Each step solves a shifted linear system, normalises the iterate and rebuilds the eigenvalue from the quotient, achieving cubic convergence within a handful of steps.

Code:
'Input matrix
A = copy( _
    [4; 12; -16| _
    12; 37; -43| _
    - 16; -43; 98]; _
    symmetric(3); 1; 1)
#hide
A_1 = A
n = n_rows(A)', 'I = identity(n)', 'o = vector(n)', 'Ξ»_0 = 1', 'x = [1; 0; 0]
#show
'Initial guess: eigenvalue -'Ξ»_0', eigenvector -'x
#def rqi$
    #for i = 1 : 10
        #val
        '<h4>Iteration - 'i'</h4>
        #equ
        x = unit(inverse(A - Ξ»_0*I)*x)
        Ξ» = transp(x)*A*x
        #if abs(Ξ» - Ξ»_0) < 10^-3
            #break
        #end if
        '<!--'Ξ»_0 = Ξ»'-->
    #loop
#end def
rqi$
'<!--'Ξ»_1 = Ξ»', 'x_1 = x'-->
'<h4>Results</h4>
'Eigenvalue 1 -'Ξ»_1
'Eigenvector 1 -'x_1
'Deflation -'A = A - Ξ»*x*transp(x)
#hide
Ξ»_0 = 10', 'x = [0; 1; 0]
rqi$
Ξ»_2 = Ξ»', 'x_2 = x
#show
'Eigenvalue 2 -'Ξ»_2
'Eigenvector 2 -'x_2
'Deflation -'A = A - Ξ»*x*transp(x)
#hide
Ξ»_0 = 100', 'x = [0; 0; 1]
rqi$
Ξ»_3 = Ξ»', 'x_3 = x
#show
'Eigenvalue 3 -'Ξ»_3
'Eigenvector 3 -'x_3
'Solution with CalcpadCE -'E = eigen(A_1)
'(symmetric QL algorithm with implicit shifts)
'Difference
R = E - [Ξ»_1; -x_1|Ξ»_2; -x_2|Ξ»_3; x_3]
mnorm_e(R)
Rendered Output:

Input matrix

A = copyβ€Š(β€Š[4; 12; -16 | 12; 37; -43 | -16; -43; 98]; symmetricβ€Š(β€Š3β€Š)β€Š; 1; 1β€Š)β€Š = 412-16 1237-43 -16-4398

Initial guess: eigenvalue - Ξ»0 = 1 , eigenvector - βƒ—x = [1 0 0]

Iteration - 1

βƒ—x = unitβ€Š(β€Šinverseβ€Š(β€ŠA βˆ’ Ξ»0 · Iβ€Š)β€Šβ€†Β·β€†βƒ—xβ€Š)β€Š = unitβ€Š(β€Šinverseβ€Š(β€ŠA βˆ’ 1 · Iβ€Š)β€Šβ€†Β·β€†βƒ—xβ€Š)β€Š = [-0.96 0.278 -0.0351]

Ξ» = transpβ€Š(β€Šβƒ—xβ€Š)β€Šβ€†Β·β€†A · ⃗x = 0.0225

Iteration - 2

βƒ—x = unitβ€Š(β€Šinverseβ€Š(β€ŠA βˆ’ Ξ»0 · Iβ€Š)β€Šβ€†Β·β€†βƒ—xβ€Š)β€Š = unitβ€Š(β€Šinverseβ€Š(β€ŠA βˆ’ 0.0225 · Iβ€Š)β€Šβ€†Β·β€†βƒ—xβ€Š)β€Š = [0.963 -0.265 0.0411]

Ξ» = transpβ€Š(β€Šβƒ—xβ€Š)β€Šβ€†Β·β€†A · ⃗x = 0.0188

Iteration - 3

βƒ—x = unitβ€Š(β€Šinverseβ€Š(β€ŠA βˆ’ Ξ»0 · Iβ€Š)β€Šβ€†Β·β€†βƒ—xβ€Š)β€Š = unitβ€Š(β€Šinverseβ€Š(β€ŠA βˆ’ 0.0188 · Iβ€Š)β€Šβ€†Β·β€†βƒ—xβ€Š)β€Š = [-0.963 0.265 -0.0411]

Ξ» = transpβ€Š(β€Šβƒ—xβ€Š)β€Šβ€†Β·β€†A · ⃗x = 0.0188

Results

Eigenvalue 1 - Ξ»1 = 0.0188

Eigenvector 1 - βƒ—x1 = [-0.963 0.265 -0.0411]

Deflation - A = A βˆ’ λ · ⃗x · transpβ€Š(β€Šβƒ—xβ€Š)β€Š = A βˆ’ 0.0188 · ⃗x · transpβ€Š(β€Šβƒ—xβ€Š)β€Š = 3.9812-16 1237-43 -16-4398

Eigenvalue 2 - Ξ»2 = 15.5

Eigenvector 2 - βƒ—x2 = [0.213 0.849 0.484]

Deflation - A = A βˆ’ λ · ⃗x · transpβ€Š(β€Šβƒ—xβ€Š)β€Š = A βˆ’ 15.5 · ⃗x · transpβ€Š(β€Šβƒ—xβ€Š)β€Š = 3.289.2-17.6 9.225.82-49.37 -17.6-49.3794.37

Eigenvalue 3 - Ξ»3 = 123.48

Eigenvector 3 - βƒ—x3 = [-0.163 -0.457 0.874]

Solution with CalcpadCE - E = eigenβ€Š(β€ŠA1β€Š)β€Š = 0.01880.963-0.2650.0411 15.5-0.213-0.849-0.484 123.48-0.163-0.4570.874

(symmetric QL algorithm with implicit shifts)

Difference

R = E βˆ’ [Ξ»1; -βƒ—x1 | Ξ»2; -βƒ—x2 | Ξ»3; βƒ—x3] = E βˆ’ [0.0188; -βƒ—x1 | 15.5; -βƒ—x2 | 123.48; βƒ—x3] = -7.22Γ—10-16-2.22Γ—10-16-3.33Γ—10-16-1.11Γ—10-16 1.01Γ—10-137.23Γ—10-8-1.98Γ—10-82.88Γ—10-9 2.84Γ—10-143.19Γ—10-98.94Γ—10-95.27Γ—10-9

mnormeβ€Š(β€ŠRβ€Š)β€Š = 7.58Γ—10-8

System of Nonlinear Equations

Solves a 4-variable nonlinear system three ways β€” fixed-point iteration, Newton-Raphson with the analytical Jacobian, and Broyden quasi-Newton β€” converging on the same root with very different iteration counts and step sizes.

Code:
'<!--'x = [1; 1; 1; 1]','g(x) = 0','g_k(x) = 0','fβ€²(x) = 0','J(x) = 0'-->
#noc
'Given the following real system of equations:
' <div style="border-left:solid 1pt; margin-left:2em; padding-left:4pt;">
5*x.1 + 4*x.2 + x.3 - x.4^2'='0
x.1^2 - x.2 + 2*x.3 - sqrt(x.4) - 3'='0
3*x.1^3 - 2*x.2^3 + 3*x.3 + 2*x.4 - 4'='0
4*x.1 - 3*x.2 + x.3^2 + x.4 - 11'='0
'</div>
'Find:'x.1'= ?,'x.2'= ?,'x.3'= ?,'x.4'= ?
'<h3>Solution</h3>
'<h4>1. Fixed point iteration method</h4>
'Fixed point iteration is the slowest, but also the most simple and intuitive method for solving nonlinear equations of type'f(x) = 0'. First, the equation is transformed into the following expression:'x'='g(x)'. Then, starting from an initial guess'x_⁰', it is repeatedly executed until it hopefully converges, as follows:
'  'x_ⁿ⁺¹ = g(x_ⁿ)
'In the multidimensional case, the solution is performed in the following way:
'Rearrange the equations in the form:'x.k'='g_k(x)', so that'g_k(x)'is contractive. We will use the following expressions for the functions'g_k(x)':
#equ
'  'g_4(x) = sqrt(abs(5*x.1 + 4*x.2 + x.3))
'  'g_1(x) = sqrt(abs(x.2 - 2*x.3 + sqrt(abs(x.4)) + 3))
'  'g_2(x) = cbrt((3*x.1^3 + 3*x.3 + 2*x.4 - 4)/2)
'  'g_3(x) = sqrt(abs(-4*x.1 + 3*x.2 - x.4 + 11))
'Select an initial guess -'x = [2; 2; 2; 2]
'Set the relative precision -'Ξ΅_max = 10^-15
#hide
#for n = 1 : 1000
    x⁰ = join(x)
    x.1 = g_1(x)','x.2 = g_2(x)
    x.3 = g_3(x)','x.4 = g_4(x)
    δx = x - x⁰
    Ξ΅ = norm_e(Ξ΄x)/norm_e(x)
    #if Ξ΅ < Ξ΅_max
        #break
    #end if
#loop
#show
'Calculate the following expressions iteratively (the last results are displayed only):
'  'x⁰ = join(x)' - store a copy of the previous vector <var>x</var>
'  Update the solution vector with a new approximation
'  'x.1 = g_1(x)','x.2 = g_2(x)','x.3 = g_3(x)','x.4 = g_4(x)
'  'Ξ΄x = x - x⁰'- difference
'  'Ξ΅ = norm_e(Ξ΄x)/norm_e(x)'- relative error
#noc
'Loop until'Ξ΅'&lt;'Ξ΅_max'or'n'&le;'1000'(guard limit in case of divergence).
#equ
'Completed in'n'iterations at'Ξ΅'&lt;'Ξ΅_max'.
'Result -'x
'Check:
'  '5*x.1 + 4*x.2 + x.3 - x.4^2
'  'x.1^2 - x.2 + 2*x.3 - sqrt(x.4) - 3
'  '3*x.1^3 - 2*x.2^3 + 3*x.3 + 2*x.4 - 4
'  '4*x.1 - 3*x.2 + x.3^2 + x.4 - 11
'<h4>2. Newton-Raphson’s method</h4>
'In the one-dimensional case, we obtain the solution iteratively by constructing the tangent line to the function at the current point and intersecting it with the abscissa to obtain the next point.
'<img style="height:100pt; width:160pt;" src="./Newton.png" alt="Newton.png">
#noc
'This process can be represented by the following equation -'x_ⁿ⁺¹ = x_ⁿ - f(x_ⁿ)/fβ€²(x_ⁿ)
'In the multidimensional case, 'x' is a vector, 'f' is a vector-valued function, and 'fβ€²' is replaced by the matrix of the partial derivatives, called "Jacobian" ('J'). So, the above equation is transformed to the expression:'x_ⁿ⁺¹ = x_ⁿ - J(x_ⁿ)^-1*f(x_ⁿ)', where'J_ij = df_i/dx_j'. The solution is performed as follows:
#equ
'Define the equations as functions:
'  'f_1(x) = 5*x.1 + 4*x.2 + x.3 - x.4^2
'  'f_2(x) = x.1^2 - x.2 + 2*x.3 - sqrt(abs(x.4)) - 3
'  'f_3(x) = 3*x.1^3 - 2*x.2^3 + 3*x.3 + 2*x.4 - 4
'  'f_4(x) = 4*x.1 - 3*x.2 + x.3^2 + x.4 - 11
'Represent the whole system as a single vector-valued function:
'  'f(x) = [f_1(x); f_2(x); f_3(x); f_4(x)]
#noc
'Define the Jacobian matrix containing the partial derivatives of the function'f':
#equ
J(x) = [5; 4; 1; -2*x.4| _
       2*x.1; -1; 2; -1/(2*sqrt(abs(x.4)))| _
       9*x.1^2; -6*x.2^2; 3; 2| _
       4; -3; 2*x.3; 1]
#equ
'Initial guess -'x = [2; 2; 2; 2]
#hide
#for n = 1 : 1000
    x⁰ = x
    δx = inverse(J(x⁰))*f(x⁰)
    Ξ΅ = norm_e(Ξ΄x)/norm_e(x)
    #if Ξ΅ < Ξ΅_max
        #break
    #end if
    x = x⁰ - δx
#loop
#show
'Calculate the following expressions iteratively (the last results are displayed only):
'  'x⁰ = x' - store the previous vector <var>x</var>
'  'Ξ΄x = inverse(J(x⁰))*f(x⁰)'- difference
'  'x = x⁰ - Ξ΄x'- update the solution vector
'  'Ξ΅ = norm_e(Ξ΄x)/norm_e(x)'- relative error
#noc
'Loop until'Ξ΅'&lt;'Ξ΅_max'or'n'&le;'1000'(guard limit in case of divergence).
#equ
'Completed in'n'iterations at'Ξ΅'&lt;'Ξ΅_max'.
'Result -'x
'Check -'f(x)
'<h4>3. Broyden’s method</h4>
'The Broyden’s method is the mulidimensional equivalent to the secant method. It is similar to Newton’s method, but instead of calculating the inverse Jacobian on each iteration, it uses the secant between two consecutive approximations.
'<img style="height:100pt; width:160pt;" src="./Secant.png" alt="Secant.png">
'Although its convergence is slower, it avoids the heavy operation on computing and inverting the Jacobian each time. The solution is performed as follows:
#equ
'Initial guess -'x⁰ = [2; 2; 2; 2]
'Find the first approximation by using the Jacobian as in Newton-Raphson’s method
y⁰ = f(x⁰)
J_inv = inverse(J(x⁰))
x¹ = x⁰ - J_inv*y⁰
yΒΉ = f(xΒΉ)
'Correction factor to improve stability -'Ξ± = 0.9
#hide
#for n = 1 : 1000
    Ξ”x = xΒΉ - x⁰
    Ξ”y = yΒΉ - y⁰
    Ξ”xT = transp(Ξ”x)
    Ξ”xTJΞ”y = Ξ”xT*J_inv*Ξ”y
    J_inv = J_inv + (Ξ”x - J_inv*Ξ”y)/Ξ”xTJΞ”y*Ξ”xT*J_inv
    Ξ΄x = J_inv*yΒΉ
    Ξ΅ = norm_e(Ξ΄x)/norm_e(x)
    #if Ξ΅ < Ξ΅_max
        #break
    #end if
    xΒ² = xΒΉ - Ξ±*Ξ΄x
    yΒ² = f(xΒ²)
    x⁰ = x¹','y⁰ = y¹
    xΒΉ = xΒ²','yΒΉ = yΒ²
#loop
#show
'Calculate the following expressions iteratively (the last results are displayed only):
'  'y⁰ = f(x⁰)'- function values at prev. step
'  'xΒΉ = x⁰ - J_inv*y⁰'- current step approximation
'  'yΒΉ = f(xΒΉ)'- function values at current step
'  'Ξ”x = xΒΉ - x⁰'- difference in approximations
'  'Ξ”y = yΒΉ - y⁰'- difference in function values
'  Update the inverse of the Jacobian using the Sherman–Morrison formula:
'  'Ξ”xTJ_inv = transp(Ξ”x)*J_inv
'  'Ξ”xTJΞ”y = Ξ”xTJ_inv*Ξ”y
'  'J_inv = J_inv + (Ξ”x - J_inv*Ξ”y)/Ξ”xTJΞ”y*Ξ”xTJ_inv
'  'Ξ΄x = J_inv*yΒΉ'- difference
'  'x⁰ = xΒΉ - Ξ±*Ξ΄x'- next approximation
'  'Ξ΅ = norm_e(Ξ΄x)/norm_e(x)'- relative error
#noc
'Loop until'Ξ΅'&lt;'Ξ΅_max'or'n'&le;'1000'(guard limit in case of divergence).
#equ
'Completed in'n'iterations at'Ξ΅'&lt;'Ξ΅_max'.
'Result -'x⁰
'Check -'f(x⁰)
Rendered Output:

Given the following real system of equations:

5 · x1 + 4 · x2 + x3 βˆ’ x42 = 0

βƒ—x12 βˆ’ x2 + 2 · x3 βˆ’    √x4 βˆ’ 3 = 0

3 · x13 βˆ’ 2 · x23 + 3 · x3 + 2 · x4 βˆ’ 4 = 0

4 · x1 βˆ’ 3 · x2 + x32 + x4 βˆ’ 11 = 0

Find: βƒ—x1 = ?, βƒ—x2 = ?, βƒ—x3 = ?, βƒ—x4 = ?

Solution
1. Fixed point iteration method

Fixed point iteration is the slowest, but also the most simple and intuitive method for solving nonlinear equations of type fβ€Š(β€Šxβ€Š)β€Š = 0 . First, the equation is transformed into the following expression: βƒ—x = gβ€Šβ€Š(β€Šβƒ—xβ€Š)β€Š . Then, starting from an initial guess x⁰ , it is repeatedly executed until it hopefully converges, as follows:

   xⁿ⁺¹ = gβ€Šβ€Š(β€ŠxβΏβ€Š)β€Š

In the multidimensional case, the solution is performed in the following way:

Rearrange the equations in the form: βƒ—xk = gkβ€Šβ€Š(β€Šβƒ—xβ€Š)β€Š , so that gkβ€Šβ€Š(β€Šβƒ—xβ€Š)β€Š is contractive. We will use the following expressions for the functions gkβ€Šβ€Š(β€Šβƒ—xβ€Š)β€Š :

   g4β€Š(β€Šxβ€Š)β€Š =    √| 5 · x1 + 4 · x2 + x3|

   g1β€Š(β€Šxβ€Š)β€Š =    √|x2 βˆ’ 2 · x3 +    √|x4| + 3 |

   g2β€Š(β€Šxβ€Š)β€Š =  3  3 · x13 + 3 · x3 + 2 · x4 βˆ’ 42

   g3β€Š(β€Šxβ€Š)β€Š =    √| -4 · x1 + 3 · x2 βˆ’ x4 + 11 |

Select an initial guess - βƒ—x = [2; 2; 2; 2] = [2 2 2 2]

Set the relative precision - Ξ΅max = 10-15

Calculate the following expressions iteratively (the last results are displayed only):

   βƒ—x⁰ = joinβ€Š(β€Šβƒ—xβ€Š)β€Š = [1 2 3 4] - store a copy of the previous vector x

  Update the solution vector with a new approximation

   βƒ—x1 = g1β€Šβ€Š(β€Šβƒ—xβ€Š)β€Š = 1 , βƒ—x2 = g2β€Šβ€Š(β€Šβƒ—xβ€Š)β€Š = 2 , βƒ—x3 = g3β€Šβ€Š(β€Šβƒ—xβ€Š)β€Š = 3 , βƒ—x4 = g4β€Šβ€Š(β€Šβƒ—xβ€Š)β€Š = 4

   βƒ—Ξ΄x = βƒ—x βˆ’ βƒ—x⁰ = [-3.55Γ—10-15 -1.33Γ—10-15 1.78Γ—10-15 -2.66Γ—10-15] - difference

   Ξ΅ = normeβ€Š(β€Šβƒ—Ξ΄xβ€Š)β€Šnormeβ€Š(β€Šβƒ—xβ€Š)β€Š = 9.06Γ—10-16 - relative error

Loop until Ξ΅ < Ξ΅max or n1000 (guard limit in case of divergence).

Completed in n = 256 iterations at Ξ΅ = 9.06Γ—10-16 < Ξ΅max = 10-15 .

Result - βƒ—x = [1 2 3 4]

Check:

   5 · ⃗x1 + 4 · ⃗x2 + βƒ—x3 βˆ’ βƒ—x42 = 3.55Γ—10-15

   βƒ—x12 βˆ’ βƒ—x2 + 2 · ⃗x3 βˆ’    βˆšβƒ—x4 βˆ’ 3 = 5.77Γ—10-15

   3 · ⃗x13 βˆ’ 2 · ⃗x23 + 3 · ⃗x3 + 2 · ⃗x4 βˆ’ 4 = 0

   4 · ⃗x1 βˆ’ 3 · ⃗x2 + βƒ—x32 + βƒ—x4 βˆ’ 11 = -2.66Γ—10-15

2. Newton-Raphson’s method

In the one-dimensional case, we obtain the solution iteratively by constructing the tangent line to the function at the current point and intersecting it with the abscissa to obtain the next point.

Newton.png

This process can be represented by the following equation - xⁿ⁺¹ = xⁿ βˆ’ fβ€Šβ€Š(β€ŠxβΏβ€Š)β€Šfβ€²β€Šβ€Š(β€ŠxβΏβ€Š)β€Š

In the multidimensional case, βƒ—x is a vector, f is a vector-valued function, and fβ€² is replaced by the matrix of the partial derivatives, called "Jacobian" ( J ). So, the above equation is transformed to the expression: xⁿ⁺¹ = xⁿ βˆ’ Jβ€Šβ€Š(β€ŠxβΏβ€Š)β€Š-1 · fβ€Šβ€Š(β€ŠxβΏβ€Š)β€Š , where Jij = dfidxj . The solution is performed as follows:

Define the equations as functions:

   f1β€Š(β€Šxβ€Š)β€Š = 5 · x1 + 4 · x2 + x3 βˆ’ x42

   f2β€Š(β€Šxβ€Š)β€Š = x12 βˆ’ x2 + 2 · x3 βˆ’    √|x4| βˆ’ 3

   f3β€Š(β€Šxβ€Š)β€Š = 3 · x13 βˆ’ 2 · x23 + 3 · x3 + 2 · x4 βˆ’ 4

   f4β€Š(β€Šxβ€Š)β€Š = 4 · x1 βˆ’ 3 · x2 + x32 + x4 βˆ’ 11

Represent the whole system as a single vector-valued function:

   fβ€Š(β€Šxβ€Š)β€Š = [f1β€Šβ€Š(β€Šxβ€Š)β€Š; f2β€Šβ€Š(β€Šxβ€Š)β€Š; f3β€Šβ€Š(β€Šxβ€Š)β€Š; f4β€Šβ€Š(β€Šxβ€Š)β€Š] !!!

Define the Jacobian matrix containing the partial derivatives of the function f :

Jβ€Š(β€Šxβ€Š)β€Š = [5; 4; 1; -2 · x4 | 2 · x1; -1; 2; -12 ·    √|x4| | 9 · x12; -6 · x22; 3; 2 | 4; -3; 2 · x3; 1] !!!

Initial guess - βƒ—x = [2; 2; 2; 2] = [2 2 2 2]

Calculate the following expressions iteratively (the last results are displayed only):

   βƒ—x⁰ = βƒ—x = [1 2 3 4] - store the previous vector x

   βƒ—Ξ΄x = inverseβ€Š(β€ŠJβ€Šβ€Š(β€Šβƒ—xβ°β€Š)β€Šβ€Š)β€Šβ€†Β·β€†fβ€Šβ€Š(β€Šβƒ—xβ°β€Š)β€Š = [3.21Γ—10-15 8.03Γ—10-16 -1.84Γ—10-15 2.4Γ—10-15] - difference

   βƒ—x = βƒ—x⁰ βˆ’ βƒ—Ξ΄x = [1 2 3 4] - update the solution vector

   Ξ΅ = normeβ€Š(β€Šβƒ—Ξ΄xβ€Š)β€Šnormeβ€Š(β€Šβƒ—xβ€Š)β€Š = 8.19Γ—10-16 - relative error

Loop until Ξ΅ < Ξ΅max or n1000 (guard limit in case of divergence).

Completed in n = 12 iterations at Ξ΅ = 8.19Γ—10-16 < Ξ΅max = 10-15 .

Result - βƒ—x = [1 2 3 4]

Check - fβ€Šβ€Š(β€Šβƒ—xβ€Š)β€Š = [1.78Γ—10-15 0 1.78Γ—10-15 0]

3. Broyden’s method

The Broyden’s method is the mulidimensional equivalent to the secant method. It is similar to Newton’s method, but instead of calculating the inverse Jacobian on each iteration, it uses the secant between two consecutive approximations.

Secant.png

Although its convergence is slower, it avoids the heavy operation on computing and inverting the Jacobian each time. The solution is performed as follows:

Initial guess - βƒ—x⁰ = [2; 2; 2; 2] = [2 2 2 2]

Find the first approximation by using the Jacobian as in Newton-Raphson’s method

βƒ—y⁰ = fβ€Šβ€Š(β€Šβƒ—xβ°β€Š)β€Š = [16 1.59 14 -3]

Jinv = inverseβ€Š(β€ŠJβ€Šβ€Š(β€Šβƒ—xβ°β€Š)β€Šβ€Š)β€Š = -0.4772.3-0.0514-0.993 -0.8163.87-0.135-1.63 0.265-1.02-0.0002650.696 -1.66.49-0.2-2.69

βƒ—xΒΉ = βƒ—x⁰ βˆ’ Jinv · ⃗y⁰ = [3.73 5.94 1.49 11.97]

βƒ—yΒΉ = fβ€Šβ€Š(β€Šβƒ—xΒΉβ€Š)β€Š = [-99.47 4.47 -239.78 0.265]

Correction factor to improve stability - Ξ± = 0.9

Calculate the following expressions iteratively (the last results are displayed only):

   βƒ—y⁰ = fβ€Šβ€Š(β€Šβƒ—xβ°β€Š)β€Š = [-1.78Γ—10-15 2.44Γ—10-15 7.37Γ—10-14 8.88Γ—10-15] - function values at prev. step

   βƒ—xΒΉ = βƒ—x⁰ βˆ’ Jinv · ⃗y⁰ = [1 2 3 4] - current step approximation

   βƒ—yΒΉ = fβ€Šβ€Š(β€Šβƒ—xΒΉβ€Š)β€Š = [0 -2.22Γ—10-16 8.88Γ—10-16 1.78Γ—10-15] - function values at current step

   βƒ—Ξ”x = βƒ—xΒΉ βˆ’ βƒ—x⁰ = [3Γ—10-15 4.22Γ—10-15 -1.78Γ—10-15 3.55Γ—10-15] - difference in approximations

   βƒ—Ξ”y = βƒ—yΒΉ βˆ’ βƒ—y⁰ = [1.78Γ—10-15 -2.66Γ—10-15 -7.28Γ—10-14 -7.11Γ—10-15] - difference in function values

  Update the inverse of the Jacobian using the Sherman–Morrison formula:

   Ξ”xTJinv = transpβ€Š(β€Šβƒ—Ξ”xβ€Š)β€Šβ€†Β·β€†Jinv = -3.06Γ—10-151.88Γ—10-142.62Γ—10-16-1.27Γ—10-14

   Ξ”xTJΞ”y = Ξ”xTJinv · ⃗Δy = 1.53Γ—10-29

   Jinv = Jinv + βƒ—Ξ”x βˆ’ Jinv · ⃗ΔyΞ”xTJΞ”y · ΔxTJinv = Jinv + βƒ—Ξ”x βˆ’ Jinv · ⃗Δy1.53Γ—10-29 · ΔxTJinv = -0.9986.070.114-4.11 -0.3122.5-0.00482-1.56 0.674-3.68-0.09312.75 -0.8484.80.068-3.21

   βƒ—Ξ΄x = Jinv · ⃗yΒΉ = [-8.55Γ—10-15 -3.33Γ—10-15 5.63Γ—10-15 -6.7Γ—10-15] - difference

   βƒ—x⁰ = βƒ—xΒΉ βˆ’ α · ⃗δx = βƒ—xΒΉ βˆ’ 0.9 · ⃗δx = [1 2 3 4] - next approximation

   Ξ΅ = normeβ€Š(β€Šβƒ—Ξ΄xβ€Š)β€Šnormeβ€Š(β€Šβƒ—xβ€Š)β€Š = 2.32Γ—10-15 - relative error

Loop until Ξ΅ < Ξ΅max or n1000 (guard limit in case of divergence).

Completed in n = 28 iterations at Ξ΅ = 2.32Γ—10-15 < Ξ΅max = 10-15 .

Result - βƒ—x⁰ = [1 2 3 4]

Check - fβ€Šβ€Š(β€Šβƒ—xβ°β€Š)β€Š = [0 1.33Γ—10-15 -3.55Γ—10-15 -8.88Γ—10-16]

Spotted an error? Edit these examples.