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.
'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>
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.
#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>
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
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.
'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}
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]
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]β)β
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]β)β
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]β)β
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.
#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>
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\).
'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|%
Function - fβ(βxβ)β =   β 1 β x2
Theoretical value of the integral - I = Ο2 = 3.142 = 1.57
Number of nodes - n = 5
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
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
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
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.
'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)
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]
β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
β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
β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
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.
'<!--'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'Ξ΅'<'Ξ΅_max'or'n'≤'1000'(guard limit in case of divergence).
#equ
'Completed in'n'iterations at'Ξ΅'<'Ξ΅_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'Ξ΅'<'Ξ΅_max'or'n'≤'1000'(guard limit in case of divergence).
#equ
'Completed in'n'iterations at'Ξ΅'<'Ξ΅_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'Ξ΅'<'Ξ΅_max'or'n'≤'1000'(guard limit in case of divergence).
#equ
'Completed in'n'iterations at'Ξ΅'<'Ξ΅_max'.
'Result -'xβ°
'Check -'f(xβ°)
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 = ?
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 n ≤ 1000 (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
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.
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 n ≤ 1000 (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]
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.
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 n ≤ 1000 (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.