SlideShare a Scribd company logo
FOXES TEAM
Reference for Xnumbers.xla
Numeric Calculus
in Excel
Volume
1x
R E F E R ENC E F O R XN U MB ER S . X L A
Numeric Calculus
in EXCEL
Oct 2007
Xnumbers Tutorial
2
Index
About this Tutorial ..................................................................................................................... 8
Array functions........................................................................................................................... 9
What is an array-function? ....................................................................................................... 9
How to insert an array function ................................................................................................ 9
How to get the help on line...................................................................................................... 12
Xnumbers installation.............................................................................................................. 13
How to install.......................................................................................................................... 13
How to uninstall...................................................................................................................... 14
Installation troubles ................................................................................................................ 14
Multiprecision Floating Point Arithmetic............................................................................... 15
Why using extended precision numbers? .............................................................................. 15
Multiprecision methods........................................................................................................... 17
How to store long number ...................................................................................................... 17
Functions .................................................................................................................................. 19
General Description................................................................................................................ 19
Using Xnumbers functions ..................................................................................................... 19
Using extended numbers in Excel.......................................................................................... 20
Functions Handbook .............................................................................................................. 21
Precision................................................................................................................................. 22
Formatting Result................................................................................................................... 22
Arithmetic Functions............................................................................................................... 23
Addition............................................................................................................................................. 23
Subtraction........................................................................................................................................ 23
Multiplication..................................................................................................................................... 24
Division ............................................................................................................................................. 24
Inverse.............................................................................................................................................. 24
Integer Division................................................................................................................................. 24
Integer Remainder ............................................................................................................................ 25
Sum .................................................................................................................................................. 27
Product ............................................................................................................................................. 27
Raise to power.................................................................................................................................. 27
Square Root...................................................................................................................................... 28
N
th
- Root ........................................................................................................................................... 28
Absolute............................................................................................................................................ 28
Change sign...................................................................................................................................... 28
Integer part ....................................................................................................................................... 29
Decimal part...................................................................................................................................... 29
Truncating......................................................................................................................................... 29
Rounding .......................................................................................................................................... 29
Relative Rounding ............................................................................................................................ 30
Extended Numbers manipulation........................................................................................... 31
Digits count....................................................................................................................................... 31
Significant Digits count...................................................................................................................... 31
Numbers comparison........................................................................................................................ 31
Extended number check ................................................................................................................... 32
Format extended number.................................................................................................................. 32
Check digits ...................................................................................................................................... 32
SortRange......................................................................................................................................... 33
Digits sum......................................................................................................................................... 33
Vector flip.......................................................................................................................................... 33
Scientific format ................................................................................................................................ 34
Split scientific format......................................................................................................................... 34
Converting multiprecision into double ............................................................................................... 34
Macros X-Edit ................................................................................................................................... 35
Macro X-Converter ........................................................................................................................... 36
Statistical Functions ............................................................................................................... 39
Factorial............................................................................................................................................ 39
Factorial with double-step................................................................................................................. 39
Xnumbers Tutorial
3
Combinations.................................................................................................................................... 39
Permutations..................................................................................................................................... 40
Arithmetic Mean................................................................................................................................ 40
Geometric Mean ............................................................................................................................... 40
Quadratic Mean ................................................................................................................................ 40
Standard Deviation ........................................................................................................................... 40
Variance............................................................................................................................................ 41
Probability distributions..................................................................................................................... 41
Univariate Statistic ............................................................................................................................ 46
Linear Regression Coefficients......................................................................................................... 47
Linear Regression - Standard Deviation of Estimates ...................................................................... 49
Linear Regression Formulas............................................................................................................. 50
Linear Regression Covariance Matrix............................................................................................... 51
Linear Regression Statistics ............................................................................................................. 52
Linear Regression Evaluation........................................................................................................... 53
Polynomial Regression - Coefficient ................................................................................................. 54
Polynomial Regression - Standard Deviation of Estimates............................................................... 54
Polynomial Regression Statistics...................................................................................................... 54
Macro - Regression .......................................................................................................................... 55
Linear Regression with Robust Method ............................................................................................ 59
Linear Regression Min-Max.............................................................................................................. 60
NIST Certification Test...................................................................................................................... 61
Transcendental Functions...................................................................................................... 62
Logarithm natural (Napier’s) ............................................................................................................. 62
Logarithm for any base ..................................................................................................................... 62
Exponential....................................................................................................................................... 62
Exponential for any base .................................................................................................................. 62
Constant e ....................................................................................................................................... 63
Constant Ln(2).................................................................................................................................. 63
Constant Ln(10)................................................................................................................................ 63
Hyperbolic Sine................................................................................................................................. 63
Hyperbolic ArSine ............................................................................................................................. 63
Hyperbolic Cosine............................................................................................................................. 64
Hyperbolic ArCosine ......................................................................................................................... 64
Hyperbolic Tangent........................................................................................................................... 64
Hyperbolic ArTangent ....................................................................................................................... 64
Euler constant γ ................................................................................................................................ 64
Trigonometric Functions......................................................................................................... 65
Sin .................................................................................................................................................... 65
Cos ................................................................................................................................................... 65
Tan ................................................................................................................................................... 66
Arcsine.............................................................................................................................................. 66
Arccosine.......................................................................................................................................... 66
Arctan ............................................................................................................................................... 66
Constant π ........................................................................................................................................ 66
Complement of right angle................................................................................................................ 67
Polynomial Rootfinder ............................................................................................................ 68
Input parameters............................................................................................................................... 69
Output............................................................................................................................................... 70
How to use rootfinder macros........................................................................................................... 71
Integer roots...................................................................................................................................... 74
Multiple roots .................................................................................................................................... 76
Polynomial Functions ............................................................................................................. 77
Polynomial evaluation....................................................................................................................... 77
Polynomial derivatives ...................................................................................................................... 78
Polynomial coefficients ..................................................................................................................... 79
Polynomial writing............................................................................................................................. 80
Polynomial addition........................................................................................................................... 80
Polynomial multiplication................................................................................................................... 80
Polynomial subtraction...................................................................................................................... 81
Polynomial division quotient.............................................................................................................. 81
Polynomial division remainder .......................................................................................................... 81
Polynomial shift................................................................................................................................. 85
Polynomial center ............................................................................................................................. 85
Polynomial roots radius..................................................................................................................... 86
Polynomial building from roots.......................................................................................................... 87
Polynomial building with multi-precision............................................................................................ 89
Polynomial Solving ........................................................................................................................... 90
Integer polynomial ............................................................................................................................ 90
Xnumbers Tutorial
4
Polynomial System of 2
nd
degree...................................................................................................... 91
Bivariate Polynomial ......................................................................................................................... 92
Orthogonal Polynomials evaluation .................................................................................................. 97
Weight of Orhogonal Polynomials..................................................................................................... 99
Zeros of Orthogonal Polynomials...................................................................................................... 99
Coefficients of Orthogonal Polynomials .......................................................................................... 100
Complex Arithmetic and Functions ...................................................................................... 101
How to insert a complex number .................................................................................................... 101
Complex Addition............................................................................................................................ 102
Complex Subtraction ...................................................................................................................... 102
Complex Multiplication.................................................................................................................... 102
Complex Division ............................................................................................................................ 102
Polar Conversion ............................................................................................................................ 103
Rectangular Conversion ................................................................................................................. 103
Complex absolute ........................................................................................................................... 103
Complex power............................................................................................................................... 104
Complex Roots ............................................................................................................................... 104
Complex Log................................................................................................................................... 105
Complex Exp .................................................................................................................................. 105
Complex inverse............................................................................................................................. 105
Complex negative ........................................................................................................................... 105
Complex conjugate ......................................................................................................................... 105
Complex Sin ................................................................................................................................... 106
Complex Cos .................................................................................................................................. 106
Complex Tangent ........................................................................................................................... 106
Complex ArcCos............................................................................................................................. 106
Complex ArcSin.............................................................................................................................. 106
Complex ArcTan ............................................................................................................................. 106
Complex Hyperbolic Sine................................................................................................................ 106
Complex Hyperbolic Cosine............................................................................................................ 107
Complex Hyperbolic Tan................................................................................................................. 107
Complex Inverse Hyperbolic Cos.................................................................................................... 107
Complex Inverse Hyperbolic Sin..................................................................................................... 107
Complex Inverse Hyperbolic Tan.................................................................................................... 107
Complex digamma.......................................................................................................................... 107
Complex Exponential Integral......................................................................................................... 107
Complex Error Function.................................................................................................................. 108
Complex Complementary Error Function........................................................................................ 108
Complex Gamma Function ............................................................................................................. 108
Complex Logarithm Gamma Function ............................................................................................ 108
Complex Zeta Function................................................................................................................... 108
Complex Quadratic Equation.......................................................................................................... 109
Number Theory .................................................................................................................... 110
Maximum Common Divisor............................................................................................................. 110
Minimum Common Multiple ............................................................................................................ 110
Rational Fraction approximation ..................................................................................................... 111
Continued Fraction ......................................................................................................................... 112
Continued Fraction of Square Root ................................................................................................ 113
Check Prime ................................................................................................................................... 113
Next Prime...................................................................................................................................... 113
Modular Addition............................................................................................................................. 114
Modular Subtraction........................................................................................................................ 114
Modular Multiplication ..................................................................................................................... 114
Modular Division ............................................................................................................................. 114
Modular Power................................................................................................................................ 114
Perfect Square................................................................................................................................ 116
Check odd/even.............................................................................................................................. 116
Check Integer ................................................................................................................................. 116
Macro - Factorize............................................................................................................................ 117
Batch Factorization with Msieve ..................................................................................................... 118
Factorization function...................................................................................................................... 120
Macro - Prime Numbers Generator................................................................................................. 121
Prime Test ...................................................................................................................................... 121
Diophantine Equation ..................................................................................................................... 123
Brouncker-Pell Equation ................................................................................................................. 124
Euler's Totient function ................................................................................................................... 125
Integer relation................................................................................................................................ 126
Macro Integer Relation Finder ........................................................................................................ 130
Linear Algebra Functions ..................................................................................................... 133
Matrix Addition................................................................................................................................ 133
Xnumbers Tutorial
5
Matrix Subtraction........................................................................................................................... 133
Matrix Multiplication ........................................................................................................................ 133
Matrix Inverse ................................................................................................................................. 133
Matrix Determinant ......................................................................................................................... 133
Matrix Modulus ............................................................................................................................... 134
Scalar Product ................................................................................................................................ 134
Similarity Transform........................................................................................................................ 134
Matrix Power................................................................................................................................... 134
Matrix LU decomposition ................................................................................................................ 135
Matrix LL
T
decomposition ............................................................................................................... 135
Vector Product................................................................................................................................ 136
Solve Linear Equation System........................................................................................................ 136
Square Delta Extrapolation............................................................................................................. 137
Macro for Multiprecision Matrix Operations..................................................................................... 139
Integrals & Series................................................................................................................. 141
Discrete Fourier Transform............................................................................................................. 141
Discrete Fourier Inverse Transform ................................................................................................ 143
Discrete Fourier Spectrum.............................................................................................................. 144
Inverse Discrete Fourier Spectrum ................................................................................................. 144
2D Discrete Fourier Transform ....................................................................................................... 145
2D Inverse Discrete Fourier Transform........................................................................................... 145
Macro DFT (Discrete Fourier Transform)........................................................................................ 146
Macro Sampler ............................................................................................................................... 149
Integral function .............................................................................................................................. 151
Zeros of integral function ................................................................................................................ 154
Function Integration (Romberg method) ......................................................................................... 155
Function Integration (Double Exponential method)......................................................................... 156
Function Integration (mixed method) .............................................................................................. 158
Complex Function Integration (Romberg method).......................................................................... 160
Data Integration (Newton-Cotes) .................................................................................................... 161
Function Integration (Newton-Cotes) .............................................................................................. 163
Integration: symbolic and numeric approaches............................................................................... 165
Integration of oscillating functions (Filon formulas) ......................................................................... 166
Integration of oscillating functions (Fourier transform) .................................................................... 168
Infinite Integration of oscillating functions ....................................................................................... 169
Double Integral ............................................................................................................................... 172
Macro for Double Integration .......................................................................................................... 172
Macro for Triple Integration............................................................................................................. 175
Double integration function ............................................................................................................. 178
Infinite integral ................................................................................................................................ 181
Double Data integration .................................................................................................................. 183
Series Evaluation............................................................................................................................ 186
Series acceleration with ∆^2 ........................................................................................................... 187
Complex Series Evaluation............................................................................................................. 188
Double Series ................................................................................................................................. 189
Trigonometric series ....................................................................................................................... 190
Trigonometric double serie ............................................................................................................. 192
Discrete Convolution....................................................................................................................... 193
Interpolation.......................................................................................................................... 195
Polynomial interpolation.................................................................................................................. 195
Interpolation schemas..................................................................................................................... 197
Interpolation with continued fraction ............................................................................................... 199
Interpolation with Cubic Spline........................................................................................................ 201
Cubic Spline 2nd derivatives........................................................................................................... 201
Cubic Spline Coefficients................................................................................................................ 203
2D Mesh Interpolation..................................................................................................................... 204
Macro Mesh Fill .............................................................................................................................. 205
Differential Equations ........................................................................................................... 211
ODE Runge-Kutta 4........................................................................................................................ 211
ODE Multi-Steps ............................................................................................................................. 215
Multi-step coefficents tables............................................................................................................ 216
Predictor- Corrector ........................................................................................................................ 218
PECE algorithm of 2
nd
order ........................................................................................................... 218
ODE Predictor-Corrector 4.............................................................................................................. 221
ODE Implicit Predictor-Corrector .................................................................................................... 223
Differential Systems........................................................................................................................ 226
OD Linear System .......................................................................................................................... 226
High order linear ODE..................................................................................................................... 227
ODE for integral function solving .................................................................................................... 230
Xnumbers Tutorial
6
Macro ODE Solver.......................................................................................................................... 232
Macro ODE - Slope Grid................................................................................................................. 233
Nonlinear Equations............................................................................................................. 235
Bisection ......................................................................................................................................... 235
Secant............................................................................................................................................. 236
Derivatives............................................................................................................................ 239
First Derivative................................................................................................................................ 239
Second Derivative........................................................................................................................... 240
Gradient.......................................................................................................................................... 240
Jacobian matrix............................................................................................................................... 241
Hessian matrix................................................................................................................................ 241
Non-linear equation solving with derivatives................................................................................... 243
Conversions ......................................................................................................................... 245
Decibel............................................................................................................................................ 245
Base conversion ............................................................................................................................. 245
Multiprecision Base Conversion...................................................................................................... 246
Log Relative Error........................................................................................................................... 247
Special Functions................................................................................................................. 249
Error Function Erf(x) ...................................................................................................................... 249
Exponential integral Ei(x)................................................................................................................ 249
Exponential integral En(x)............................................................................................................... 249
Euler-Mascheroni Constant γ......................................................................................................... 250
Gamma function Γ(x) ...................................................................................................................... 250
Log Gamma function ...................................................................................................................... 251
Gamma quotient ............................................................................................................................. 251
Gamma F-factor.............................................................................................................................. 252
Digamma function........................................................................................................................... 252
Beta function................................................................................................................................... 252
Incomplete Gamma function........................................................................................................... 253
Incomplete Beta function ................................................................................................................ 253
Combinations function .................................................................................................................... 253
Bessel functions of integer order ................................................................................................... 254
Cosine Integral Ci(x) ....................................................................................................................... 254
Sine Integral Si(x) ........................................................................................................................... 254
Fresnel sine Integral ....................................................................................................................... 255
Fresnel cosine Integral.................................................................................................................... 255
Fibonacci numbers ......................................................................................................................... 255
Hypergeometric function................................................................................................................. 256
Zeta function ζ(s) ........................................................................................................................... 256
Airy functions .................................................................................................................................. 257
Elliptic Integrals............................................................................................................................... 257
Kummer confluent hypergeometric functions.................................................................................. 258
Integral of sine-cosine power.......................................................................................................... 259
Spherical Bessel functions of integer order..................................................................................... 260
Formulas Evaluation............................................................................................................. 261
Multiprecision Expression Evaluation ............................................................................................. 261
Complex Expression Evaluation ..................................................................................................... 265
Multiprecision Excel Formula Evaluation ........................................................................................ 266
Math expression strings.................................................................................................................. 268
List of basic functions and operators .............................................................................................. 271
Function Optimization........................................................................................................... 274
Macros for optimization on site ....................................................................................................... 274
Example 1 - Rosenbrock's parabolic valley .................................................................................... 276
Example 2 - Constrained minimization ........................................................................................... 277
Example 3 - Nonlinear Regression with Absolute Sum................................................................... 279
Example 4 - Optimization of Integral function ................................................................................. 280
How to call Xnumbers functions from VBA ......................................................................... 281
References & Resources....................................................................................................... 284
Analytical index...................................................................................................................... 287
Xnumbers Tutorial
7
WHITE PAGE
Xnumbers Tutorial
8
About this tutorial
About this Tutorial
This document is the reference guide for all functions
and macros contained in the Xnumbers addin. It is a
printable version of the help-on-line, with a larger
collection of examples.
XNUMBERS.XLA is an Excel addin containing useful functions for numerical
calculus in standard and multiprecision floating point arithmetic up to 200
significant digits.
The main purpose of this document is to be a reference guide for the functions of this package,
showing how to work with multiprecision arithmetic in Excel. The most part of the material
contained in this document comes from the Xnumbers help-on-line. You may print it in order to
have a handle paper manual. Of course it speaks about math and numerical calculus but this is
not a math book. You rarely find here theorems and demonstrations. You can find, on the
contrary, many explaining examples.
Special thanks to everyone that have kindly collaborated.
Leonardo Volpi
Chapter
1
Xnumbers Tutorial
9
Array functions
What is an array-function?
A function that returns multiple values is called "array-function". Xnumbers contains many of
these functions. Each function returning a matrix or a vector is an array functions. Function
performing matrix operations such as inversion, multiplication, sum, etc. are examples of array-
functions. Also complex numbers are arrays of two cells. On the contrary, in the real domain,
the logarithm, the exponential, the trigonometric functions, etc. are scalar functions because
they return only one value.
In a worksheet, an array-function always returns a (n x m) rectangular range
of cells. To enter it, you must select this range, enter the function as usually
and and give the keys sequence CTRL+SHIFT+ENTER. Keep down both
keys CTRL and SHIFT (do not care the order) and then press ENTER.
How to insert an array function
The following example explains, step-by-step, how to insert an array-function
The System Solution
Assume to have to solve a 3x3 linear system. The solution is a vector of 3 values.
Ax = b
Where:
The function SYSLIN returns the vector solution x, that is an (3 x 1) array.
To see the three values you must select
before the area where you want to insert
these values.
Now insert the function either from menu or
by the icon
Select the area of the matrix A "A5:C7" and
the constant vector b "E5:E7"










=
431
221
111
A










=
3
2
4
b
Xnumbers Tutorial
10
Now - attention! - give the "magic" keys sequence CTRL+SHIFT+ENTER
That is:
• Press and keep down the CTRL and SHIFT keys
• Press the ENTER key
All the values will fill the cells that you have selected.
Note that Excel shows the function around two braces { }. These symbols mean that the
function return an array (you cannot insert them by hand).
An array function has several constrains. Any cell of the array cannot be modified or deleted.
To modify or delete an array function you must selected before the entire array cells.
Adding two matrices
The CTRL+SHIFT+ENTER rule is valid for any function and/or operation returning a matrix or a
vector
Example - Adding two matrices






+




 −
10
01
12
21
We can use directly the addition operator "+". We can do this following these steps.
1) Enter the matrices into the spreadsheet.
2) Select the B8:C9 empty cells so that a 2 × 2 range is highlighted.
3) Write a formula that adds the two ranges. Either write =B4:C5+E4:F5 Do not press
<Enter>. At this point the spreadsheet should look something like the figure below. Note
that the entire range B8:C9 is selected.
Xnumbers Tutorial
11
4) Press and hold down <CTRL> + <SHIFT>
5) Press <ENTER>.
If you have correctly followed the procedure, the spreadsheet should now look something like
this
This trick can also work for matrix subtraction and for the scalar-matrix multiplication, but not for
the matrix-matrix multiplication.
Let's see this example that shows how to calculate the linear combination of two vectors
Functions returning optional values
Some function, such as for example the definite integral of a real function f(x), can return one
single value and optional extra data (iterations, error estimation, etc...)
If you do not want to see this additional information simply select one cell and insert the
function with the standard procedure. On the contrary, if you want to see also the extra
information, you must select the extra needed cells and insert it as an array-function
Xnumbers Tutorial
12
How to get the help on line
Xnumbers provides the help on line that can be recalled in the same way of any other Excel
function. When you have selected the function that you need, press the F1 key or click on the
“guide hyperlink”
There is also another way to get the help-on-line. It is from the Xnumbers Function Handbook
Select the function that you want and press the Help button
You can also recall the help guide from the function wizard window
Of course you can open the help on-line from the Xnumber menu
or directly by double clicking on the Xnumbers.hlp file
Xnumbers Tutorial
13
Xnumbers installation
How to install
This addin for Excel XP is composed by the following files:
Addin file
(It contains the Excel macros
and functions)
Help file
(It contains the help notes)
Handbook file
(It contains the macros and
functiosn description list for the
Xnumbers Handbook)
This installation is entirely contained in the folder that you specify.
Put all these files in a same directory as you like.
Open Excel and follow the usually operations for the addin installation:
1) Select <addins...> from <tools> menu,
2) Excel shows the Addins Manager,
3) Search for the file xnumbers.xla,
4) Press OK,
After the first installation, Xnumbers.xla will be add to the Addin Manager
By this tool, you can load or unload the addins that you want, simply switching on/off the check-
boxes.
At the starting, the checked addins will be automatically loaded
If you want to stop the automatic loading of xnumbers.xla simply deselect the check box before
closing Excel
If all goes right you should see the welcome
popup of Xnumbers. This appears only when
you activate the check box of the Addin
Manager. When Excel automatically loads
Xnumbers, this popup is hidden
.
Xnumbers Tutorial
14
How to uninstall
If you want to uninstall this package, simply delete its folder. Once you have cancelled the
Xnumbers.xla file, to remove the corresponding entry in the Addin Manager, follow these steps:
1) Open Excel
2) Select <Addins...> from the <Tools> menu.
3) Once in the Addins Manager, click on the Xnumbers entry
4) Excel will inform you that the addin is missing and ask you if you want to remove it from
the list. Give "yes".
Installation troubles
If you do not see the Xnumbers icon or the welcome popup the installation has gone wrong and
probably it is due to same configuration parameter of your Windows / Excel environment
The default security setting of Excel XP 2002/2003 doesn't allowe to install Xnumbers as is. To
prevent errors it is necessary to open the Excel menu option:
Tools > Options > Security > Macro Security, and set: "Average Level security" and "Trust
Access to Visual Basic Project".
Do not worry about it. Xnumbers contains only safe macros; they do not alter your PC in any
way. If you want to remove Xnumbers simply delete all the files contained in the package.
In addition we have to point out that, nowadays, nobody use to hide virus, spy-software, trojan
and related things in a macro Excel: this things belong to the prehistory of informatics!
Nowadays, E-mail and Internet are by far the most important media.
Xnumbers Tutorial
15
Multiprecision Floating Point Arithmetic
Any computer having hardware at 32-bit can perform arithmetic operations with 15 significant
digits1
. The only way to overcome this finite fixed precision is to adopt special software that
extends the accuracy of the native arithmetic
Why using extended precision numbers?
First of all, for example, to compute the following operation:
90000000002341 x
8067 =
--------------------
726030000018884847 .
Any student, with a little work, can do it. Excel, as any 32-bit machine, cannot! It always gives
the (approximate) result 726030000018885000 , with a difference of +153.
But do not ask Excel for the difference. It replies 0!
The second, deeper, example regards numeric analysis.
Suppose we have to find the roots of the following 9th
degree polynomial.
∑=
=
n
i
i
i xaxP
0
)(
There are excellent algorithms for finding a
numerical solution of this problem. We can
use the Newton-Raphson method: starting
from x = 32 and operating with 15 significant
digits (the maximum for Excel), we have:
Coefficients
a9 1
a8 -279
a7 34606
a6 -2504614
a5 116565491
a4 -3617705301
a3 74873877954
a2 -996476661206
a1 7738306354988
a0 -26715751812360
xn P(x) (15 digit) P' (x) (15 digit) -P/P' |xn-x|
32 120 428 0,280373832 1
31,71962617 43,77734375 158,9873047 0,275351191 0,7196262
31,44427498 15,69921875 60,93164063 0,257652979 0,444275
31,186622 4,78125 29,46289063 0,162280411 0,186622
31,02434159 0,65625 24,10644531 0,02722301 0,0243416
30,99711858 -0,07421875 24,01953125 -0,003089933 0,0028814
31,00020851 0,23828125 24,04980469 0,009907825 0,0002085
30,99030069 -0,52734375 23,98925781 -0,021982495 0,0096993
31,01228318 0,2421875 24,02050781 0,01008253 0,0122832
31,00220065 -0,03515625 23,99023438 -0,00146544 0,0022007
As we can see, the iteration approaches the solution x = 31 but the error |xn - x| remains too
high. Why? Multiple roots? No, because P'(x) >> 0. Algorithm failed? Of course not. This
method is very well tested. The only explanation is the finite precision of the computing. In fact,
repeating the calculus of P(x) and P'(x) with 25 significant digits, we find the excellent
convergence of this method.
1
The basic structure is the IEEE-754 which allows for a 64 bit standard floating point double precision number. The
later has a mantissa of 53 bits (one is the implied bit) which becomes equivalent to 15.4 digits. Excel reports a
maximum of 15 digits. For a good, accurate note see "Excel Computation and Display Issue" by David. A. Heiser,
http://www.daheiser.info/excel/frontpage.html
Xnumbers Tutorial
16
xn P(x) (25 digit) P' (x) (25 digit) -P/P' |xn-x|
32 120 428 0,28037383 1
31,71962617 43,71020049043 158,979858019937 0,27494175 0,719626
31,44468442 15,71277333004 61,059647049872 0,25733482 0,444684
31,1873496 4,83334748037 29,483621556222 0,1639333 0,18735
31,02341629 0,56263326884 24,082301045236 0,02336294 0,023416
31,00005336 0,00128056327 24,000000427051 5,3357E-05 5,34E-05
31 0,00000000053 23,999999999984 2,2083E-11 1,54E-11
31 0,00000000004 23,999999999995 1,6667E-12 6,66E-12
The graph below resumes the effect of computation with 15 and 25 significant digits.
1E-12
1E-10
1E-08
1E-06
0,0001
0,01
1
1 2 3 4 5 6 7 8 9 10
|Xn -X|
15 digits
25 digits
Iteration
The application field of multi-precision computation is wide. Especially it is very useful for
numeric algorithms testing. In the above example, we had not doubt about the Newton-
Raphson method, but what about the new algorithm that you are studying? This package helps
you in this work.
Xnumbers Tutorial
17
Multiprecision methods
Several methods exist for simulating variable multi-precision floating point arithmetic. The basic
concept consists of breaking down a long number into two or more sub-numbers, and repeating
cyclic operations with them. The ways in which long numbers are stored vary from one method
to another. The two most popular methods use the "string" conversion and the "packing"
How to store long number
String Extended Numbers
In this method, long numbers are stored as vectors of characters, each representing a digit in
base 256. Input numbers are converted from decimal to 256 base and vice versa for output. All
internal computations are in 256 base. this requires only 16 bit for storing and a 32 bit
accumulator for computing. Here is an example of how to convert the number 456789 into
string
( ) ( )25610 85248,,6456789 ≡
String = chr(6)&chr(248)&chr(85)
This method is very fast, and efficient algorithms for the input-output conversion have been
realized. A good explanation of this method can be found in "NUMERICAL RECIPES in C - The
Art of Scientific Computing", Cambridge University Press, 1992, pp. 920-928. In this excellent
work you can also find efficient routines and functions to implement an arbitrary-precision
arithmetic.
Perhaps the most critical factor of this method is the debug and test activity. It will be true that
the computer does not care about the base representation of numbers, but programmers
usually do it. During debugging, programmers examine lots and lots of intermediate results,
and they must always translate them from base 256 to 10. For this kind of programs, the
debugging and tuning activity usually takes 80 - 90% of the total develop time.
Packet Extended Numbers
This method avoids converting the base representation of long numbers and stores them as
vectors of integers. This is adopted in all FORTRAN77 routines of "MPFUN: A MULTIPLE
PRECISION FLOATING POINT COMPUTATION PACKAGE" by NASA Ames Research
Center. For further details we remand to the refined work of David H. Bailey published in
"TRANSACTIONS ON MATHEMATICAL SOFTWARE", Vol. 19, No. 3, SEPTEMBER, 1993,
pp. 286-317.
Of course this add-in does not have the performance of the mainframe package (16 million
digits) but the method is substantially the same. Long numbers are divided into packets of 6 or
7 digits.
For example, the number 601105112456789 in packet form of 6 digits becomes the following
integer vector:
456789
105112
601
As we can see, the sub-packet numbers are in decimal base and the original long number is
perfectly recognizable. This a great advantage for the future debugging operation.
An example of arithmetic operation - the multiplication A x B = C - between two packet numbers
is shown in the following:
A B
456789 654321
105112 X
601
The schema below illustrates the algorithm adopted:
Xnumbers Tutorial
18
carry A B C' C
0 + 456789 x 654321 = 298886635269 => 635269
298886 + 105112 x 654321 = 68777287838 => 287838
68777 + 601 x 654321 = 393315698 => 315697
393 + 0 x 654321 = 393 => 393
The numbers in the accumulator C' are split into two numbers. The last 6 digits are stored in C,
the remaining left digits are copied into the carry register of the next row.
As we can see, the maximum number of digits is reached in accumulator C'. In the other
vectors, the numbers require only six digits at most. The maximum number of digits for a single
packet depends of the hardware accumulator. Normally, for a 32-system, is 6 digits.. This is
equivalent to conversion from a decimal to a 10^6 representation base. This value is not critical
at all. Values from 4 to 7 affect the computation speed of about 30 %. But it does not affect the
precision of the results in any case.
Xnumbers Tutorial
19
Functions
General Description
Xnumbers is an Excel addin (xla) that performs multi-precision floating point arithmetic.
Perhaps the first package providing functions for Excel with precision from 15 up to 200
significant digits. It is compatible with Excel XP and consists of a set of more than 270 functions
for arithmetic, complex, trigonometric, logarithmic, exponential and matrix calculus covering the
following main subjects.
The basic arithmetical functions: addition, multiplication, and division were developed at the
first. They form the basic kernel for all other functions.
All functions perform multiprecision floating point computations for up to 200 significant digits.
You can set a precision level separately for each function by an optional parameter. By default,
all functions use the precision of 30 digits, but the numerical precision can easily be regulated
continually from 1 to 200 significant digits. In advance some useful constants like π, Log(2),
Log(10) are provided with up to 400 digits.
Using Xnumbers functions
These functions can be used in an Excel worksheet as any other built-in function. After the
installation, look up in the functions library or click on the icon
Upon "user's" category you will find the functions of this package.
From version 2.0 you can manage functions also by the Function Handbook. It starts by the
Xnumbers menu
All the functions for multi-precision computation begin with "x". The example below shows two
basic functions for the addition and subtraction.
As any other functions they can also be nested to build complex expressions. In the example
below we compute x^4 with 30 digits precision
As we can see, Xnumbers is powerful, but it does slow down the computation considerably
Therefore, use the multiprecision x-functions only when they are really necessary.
Xnumbers Tutorial
20
Using extended numbers in Excel
If you try to enter a long number with more than 15 digits in a worksheet cell,
Excel automatically converts it in standard precision eliminating the extra
digits. The only way to preserve the accuracy is to convert the number in a
string. It can be done by prefixing it with the hyphen symbol ' .
This symbol is invisible in a cell but avoid the conversion.
Example: enter in a cell the number 1234567890123456789.
We have inserted the same
number with the hyphen in B2
and without the hyphen in B3.
Excel treats the first number as a
string and the second as a
numbers
Note also the different alignment
We have inserted a long
numbers with full precision as a
string in B2
If we try to multiply the cell B2 for
another number, example for 2,
Excel converts the string into
number before performing the
multiplication. In this way the
originally accuracy is destroyed
The only way to perform arithmetic operations
preserving the precision is to use the
multiprecision functions of the Xnumbers
library.
In that case we use the function xmult
Note from the alignment that the result is still
a string
You can also insert extended numbers directly in the function. Only remember that, for
preserving Excel to convert them, you must insert extended numbers like string, within quote
"...".
2469135780246913578 =xmult( "1234567890123456789" , 2 )
Xnumbers Tutorial
21
Functions Handbook
Xnumbers includes an application for searching and pasting the Xnumbers functions,
cataloged by subject. This application can also submit the Xnumbers macros.
You can activate the Functions Handbook from the menu bar Help > Function manager.
Category: you can filter macros by category (Arithmetic, Statistical, Trigonometric, etc.)
Macro Type: filters by macro Functions, by macro Subroutines, or both
Paste Into: choose the cell you want to paste a function, default is the active cell
Search: searches macros by words or sub-words contained into the name or description. For
example, if you input "div" you list all macros that match words like (div, divisor, division,...)
You can associate more words in AND/OR. Separate words with comma "," for OR, with plus
"+", for AND. For example, if you type "+div +multi" you will get all the rows containing words
like (div, divisor, division,...) and words like (multi, multiprecision,...). On the contrary, if you
type "div, multi", you get all the rows that contain words like (div, divisor, division,...) or also the
words like (multi, multiprecision,...). Remember to choose also the Category and Macro Type.
Example, if you enter the word “hyperbolic”, setting the Category “complex”, you find the
hyperbolic functions restricted to the complex category.
Help: recalls the help-on-line for the selected function.
OK: insert the selected function into the worksheet ". This activates the standard Excel function
wizard panel. If the macro selected is a "sub", the OK button activates the macro.
Xnumbers Tutorial
22
Precision
Most functions of this package have an optional parameter - Digit_Max - setting the maximum
number of significant digits for floating point computation, from 1 to 200 (default is 30). The
default can be changed from the menu X-EditDefaut Digits
This parameter also determines how the output is automatically formatted. If the result has
fewer integer digits than Digit_Max, then the output is in the plain decimal format
( 123.45, -0.0002364, 4000, etc.), otherwise, if the number of integer digits exceeds the
maximum number of digits allowed (significant or not), the output is automatically converted in
exponential format (1.23456789E+94).
The exponent can reach the extreme values of +/- 2,147,483,.647.
The output format is independent of the input format.
In synthesis, the Digit_Max parameter limits:
The significant digits of internal floating point computation
The maximum number output digits, significant or not.
The default of Digit_Max can be changed from the X-Edit menu . It affects all multiprecision
functions and macros.
Formatting Result
The user can not format an extended number with standard Excel number format tools,
because, it is a string for Excel. You can only change the alignment. To change it you can use
the usual standard Excel format tools.
It is possible to separate the digits of a x-numbers in groups, by the user function xFormat()
and xUnformat() 1
.
It work similar at the built-in function Format(x, "#,##0.00")
2,469,135,780,246,913,578 = xformat("2469135780246913578",3)
.
1
These functions were original developed by Ton Jeursen for the add-in XNUMBER95, the downgrade version of
XNUMBERS for Excel 5. Because they are very useful for examining long string of number, we have imported them in
this package
Xnumbers Tutorial
23
Arithmetic Functions
Addition
xadd(a, b, [Digit_Max])
Performs the addition of two extended numbers: xadd(a, b) = a + b.
Subtraction
xsub(a, b, [Digit_Max])
Performs the subtraction of two extended numbers: xsub(a, b) = a − b.
NB. Do not use the operation xadd(a, -b) if “b” is an extended number. Excel converts “b” into
double, then changes its sign, and finally calls the xadd routine. By this time the original
precision of “b” is lost. If you want to change sign at an extended number and preserve its
precision use the function xneg()
Accuracy lack by subtraction
The subtraction is a critical operation from the point of view of numeric calculus. When the
operands are very near each others, this operation can cause a lack of accuracy. Of course
this can happen for addition when the operands are near and have opposit signs. Let’s see this
example. Assume one performs the following subtraction where the first operand has a
precision of 30 significant digits
(digits)
800000.008209750361424423316366 30
800000 6
0.008209750361424423316366 25
The subtraction is exact (no approximation has been entered). But the final result have 25 total
digits, of wich only 22 are significant. 8 significant digits are lost in this subtraction. We cannot
do anything about this phenomenon, except to increase the precision of the operands, when
possible.
Xnumbers Tutorial
24
Multiplication
xmult(a, b, [Digit_Max])
Performs the multiplication of two extended numbers: xmult(a, b) = a x b.
The product can often lead to long extended numbers. If the result has more integer digits than
the ones set by Digit_Max, then the function automatically converts the result into the
exponential format.
Division
xdiv(a, b, [Digit_Max])
Performs the division of two extended numbers: xdiv(a, b) = a / b.
If b = 0 the function returns “?”. The division can return long extended numbers even when the
operands are small. In the example below we see the well-known periodic division 122 / 7 =
17,428571 …. with 30 significant digits.
Inverse
xinv(x, [Digit_Max])
It returns the inverse of an extended number. If x = 0, the function returns “?”.
xinv(x) = 1 / x
Integer Division
xdivint(a, b)
Returns the quotient of the integer division for a>0 , b>0.
If b = 0 the function returns “?”.
xdivint(a, b)= q , where: a = b*q + r , with 0 ≤ r < b
Xnumbers Tutorial
25
Integer Remainder
xdivrem(a, b)
Returns the remainder of the integer division for a>0 , b>0. If b = 0 the function returns “?”.
xdivrem(a, b)= r , where: a = b*q + r , with 0 ≤ r < b
This function is also called "a mod b "
How to test multiprecision functions ?
This test is the most important problem in developing multiprecision arithmetic. This activity,
absorbs almoust the 60% of the totally realization effort.
Apart the first immediate random tests, we can use many known formulas and algorithms. The
general selecting criterions are:
1. Formulas should be iterative
2. Formulas should have many arithmetic elementary operations
3. Final results should be easyly verified
4. Intermediate results should be easily verified
5. Algorithms should be stable
6. Efficency is not important
For example, a good arithmetic test is the Newton algoritm to compute the square root of a
number. The iterative formula:
n
nn
n
n
n
x
xx
x
x
x
⋅
+⋅
=+=+
2
21
2
1
converges to 21/2
, starting from x0 = 1.
We have rearranged the formula in order to increase the number of operations (remember: the
efficency is not important). In this way we can test mutliplication, division and addition.
x0 = 1
x1 = 1.5
x2 = 1.41…
x3 = 1.41421……
Here is a possible Excel arrangement. We have limited the number of the significant digits to
100 only for the picture dimensions, but there is no difficult to repeat it with the maximum digits.
For each iterate only the blu digits are exacts. We see the progressive convergence. By the
way, we note that this algorithm is also very efficent. The rate of convergence is quadratic. The
number of digits approximately doubles at each iteration (In fact this is just the algorithm used
by the xsqr multiprecision function)
But, as said, for testing, the efficiency has no influence. It is important only that the algorithm
involves the most multiprecision functions as possible.
Xnumbers Tutorial
26
Another algorithm quite suitable for testing multiprecision accuracy is the π approximatation by
continuous fraction1
.
Initialize
X = 31/2
, Y = 1/2, T = 6
Iteration
X = (2 + X)1/2
Y = Y/X
T = 2T
P = Y*T*(5Y6
/112 + 3Y4
/40 + Y2
/6 + 1)
Accuracy: approximately 12 decimal digits every 5 iterations)
Below, step by step, a possible Excel arrangement:
The Digit_Max parameter is in the A1 cell. By this parameter we can modulate the arithmetic
accuracy. We have set 30 digits only for the picture dimensions. But you can try with 60, 100 or
more.
Note that, in order to have a more compact form, we have used the xeval function for
calculating the X and P formulas that are inserted into the cells B3 and E3 rispectively.
Selecting the last row (range A6:F6) and dragging it down, we get the following iteration table
The convergence to π is evident.
1
This version, studied by David Sloan (2003), full of many arithmetic operations, permitted us to detect a very hidden
bug in Xnumbers
Xnumbers Tutorial
27
Sum
xsum(v, [Digit_Max])
This is the extended version of the Excel built-in function SUM. It returns the sum of a vector of
numbers. The argument is a standard range of cells.
ni i vvvv ...21 ++=∑
Note that you can not use the standard function SUM, because it recognizes extended
numbers as strings and it excludes them from the calculus.
Product
xprod(v, [Digit_Max])
Returns the product of a vector of numbers.
nii vvvv ...21 ⋅⋅=∏
Note that the result is an extended number
even if all the factors are in standard
precision
Raise to power
xpow(x, n, [Digit_Max])
Returns the integer power od an extended number. xpow(x, n) = x n
xpow("0.39155749636098981077147016011",90) = 1.9904508921478176508981155284E-7
xpow(5,81,60) = 5
81
= 413590306276513837435704346034981426782906055450439453125
xpow(122.5 ,1000) = 122.5
1000
= 1.36800819983562905361091390093E+2088
Note the exponent +2088 of the third result. Such kind of numbers can be managed only with
extended precision functions because they are out side of the standard limits for 32bit double
precision.
For not integer power see the exponential functions xexp and xexpa
Xnumbers Tutorial
28
Square Root
xsqr(x, [Digit_Max])
Returnts the square root of an extended number xsqr(x) = x
The example below shows how to compute the 2 with 30 and 60 significant digits:
xsqr(2) = 1.41421356237309504880168872420969807
xsqr(2, 60) = 1.41421356237309504880168872420969807856967187537694807317667973799
Nth
- Root
xroot(x, n, [Digit_Max])
Returns the nth
root of an extended number xroot(x, n)= n
x
The root's index must be a positive integer.
The example below shows how to compute the 9
100 with 30 and 60 significant digits:
xroot(100,9) = 1.66810053720005875359979114908
xroot(100,9,60) = 1.66810053720005875359979114908865584747919268415239470704499
Absolute
xabs(x)
Returns the absolute value of an extended number xabs(x)= |x|
Do not use the built-in function "abs", as Excel converts x in double, then takes the absolute
value but, at that time, the original precision of x is lost.
Change sign
xneg(x)
Returns the opposite of an extended number: xneg(x) = −x
Do not use the operator “−“ (minus) for extended numbers. Otherwise Excel converts the
extended numbers into double and changes its sign but, at that time, the original precision is
lost. In the following example the cell B8 contains an extended number with18 digits. If you use
the “−“ as in the cell B9, you lose the last 3 digits. The function xneg(), as we can see in the cell
B10, preserves the original precision.
Xnumbers Tutorial
29
Integer part
xint(x)
Returns the integer part of an extended number, that is the greatest integer less than or equal
to x.
Examples:
xint(2.99) = 2
xint(2.14) = 2
xint(-2.14) = −3
xint(-2.99) = −3
xint(12345675.00000001) = 12345675
xint(−12345675.00000001) = −12345676
Decimal part
xdec(x)
Returns the decimal part of an extended number
Examples:
xdec(2.99) = 0.99
xdec(-2.14) = - 0.14
Truncating
xtrunc(x, [dec])
Returns the truncated number; the parameter "dec" sets the number of decimals to keep
(default 0). It works like Excel function TRUNC. “dec” can be negative; in that case x is cut to
the integer number, counting back from the decimal point. See the following examples.
Examples:
number dec number truncated
4074861.076055370173 -2 4074800
4074861.076055370173 -1 4074860
4074861.076055370173 0 4074861
4074861.076055370173 1 4074861
4074861.076055370173 2 4074861.07
4074861.076055370173 3 4074861.076
Rounding
=xround(x, [dec])
Rounds an extended number, the parameter "dec" sets the decimal number of is to keep
(default 0). It works like standard round function. “dec” can be negative, in that case x is
rounded to the integer number, starting to count back from decimal point. See the following
examples.
Xnumbers Tutorial
30
number to round dec number rounded
6.2831853071795864769 0 6
6.2831853071795864769 1 6.3
6.2831853071795864769 2 6.28
6.2831853071795864769 3 6.283
6.2831853071795864769 4 6.2832
100352934.23345 0 100352934
100352934.23345 -1 100352930
100352934.23345 -2 100352900
When the number is in exponential format, it is internally converted into decimal before the
rounding.
number to round Decimal format Dec number rounded
1.238521E-17 0.00000000000000001238521 16 0
1.238521E-17 0.00000000000000001238521 17 1.E-17
1.238521E-17 0.00000000000000001238521 18 1.2E-17
1.238521E-17 0.00000000000000001238521 19 1.24E-17
Relative Rounding
=xroundr(x, [dgt])
Returns the relative round of a number. The optional parameter Dec sets the significant digits
to keep. (default = 15)
This function always rounds the decimal place no matter what the exponent is
number to round dgt number rounded
1.23423311238765E+44 15 1.23423311238765E+44
1.23423311238765E+44 14 1.2342331123876E+44
1.23423311238765E+44 13 1.234233112388E+44
1.23423311238765E+44 12 1.23423311239E+44
1.23423311238765E+44 11 1.2342331124E+44
1.23423311238765E+44 10 1.234233112E+44
Xnumbers Tutorial
31
Extended Numbers manipulation
Digits count
xdgt(x)
Returns the number of digits, significant or not, of an extended number.
It is useful for counting the digits of long numbers
xdgt(4.47213595499957939281834733746) = 30
xdgt(3.99999999999999999999999990000) = 30
xdgt(100000) = 6
xdgt(5856.51311933374) = 15
xdgt(1.2345678E-12) = 8
Significant Digits count
xdgts(x)
Returns the number of significant digits of a number( trailing zeros are not significant).
xdgts("1240100000") = 5
xdgts(3.99999999999999999999999990000) = 26
xdgts(100000) = 1
xdgt(1.2345678E-12) = 8
Numbers comparison
xcomp(a [b])
Compares two extended numbers. It returns the value y defined by:





<⇒−
=⇒
>⇒
=
ba
ba
ba
y
1
0
1
The number b is optional (default b=0)
If the second argument is omitted, the function returns the sign(a)
xcomp(300, 299)= 1
xcomp(298, 299)= -1
xcomp(300, 300)= 0
if b is missing, the function assumes b = 0 for default and then it returns the sign(a)
xcomp(3.58E-12)= 1
xcomp(0)= 0
xcomp(-0.0023)= -1
Xnumbers Tutorial
32
Extended number check
xIsXnumber(x)
Returns TRUE if x is a true extended number.
That is, x cannot be converted into double precision without losing of significant digits. It
happens if a number has more than 15 significant digits.
xIsXnuber(1641915798169656809371) = TRUE
xIsXnuber(1200000000000000000000) = FALSE
Format extended number
=xFormat(str, [Digit_Sep])
=xUnformat(str)
This function1
separates an extended number in groups of digits by the separator character of
you local system ( e.g. a comma "," for USA, a dot "." for Italy). Parameter "str" is the string
number to format, Digit_Sep sets the group of digits ( default is 3)
The second function removes any separator character from the string
Example (on Italian standard):
x = 1230000012,00002345678
xFormat(x,3) = 1.230.000.012,000.023.456.79
xFormat(x,6) = 1230.000012,000023.45679
Example (on USA standard):
xFormat(x,3)= 1,230,000,012.000,023,456,78
xFormat(x,6)= 1230,000012.000023,45678
Check digits
DigitsAllDiff(number)
This function2
return TRUE if a number has all digits different.
DigitsAllDiff(12345) = TRUE
DigitsAllDiff(123452) = FALSE
Argument can be also a string. Example
DigitsAllDiff(12345ABCDEFGHIM) = TRUE
DigitsAllDiff(ABCDA) = FALSE
1
These functions were original developed by Ton Jeursen for his add-in XNUMBER95, the downgrade version of
XNUMBERS for Excel 5.
Because it works well and it is very useful for examining long string of number, I have imported it in this package.
2
This function appears by the courtesy of Richard Huxtable
Xnumbers Tutorial
33
SortRange
=SortRange (ArrayToSort, [IndexCol], [Order], [CaseSensitive])
This function returns an array sorted along a specified column
ArrayToSort: is the (N x M ) array to sort
IndexCol: is the index column for sorting (1 default)
Order: can be "A" ascending (default) or "D" descending
CaseSensitive: True (default) or False. It is useful for alphanumeric string sorting
Example: The left table contains same points of a function f(x,y). The right table is ordered from
low to high function values (the 3-th column)
Digits sum
sumDigits(number)
This useful1
function returns the digits sum of an integer number (extended or not)
sumDigits(1234569888674326778876543) = 137
Vector flip
Flip(v)
This function returns a vector in inverse order [a1, a2, a3, a4] ⇒ [a4, a3, a2, a1]
1
This function appears by the courtesy of Richard Huxtable
Xnumbers Tutorial
34
Scientific format
xcvexp(mantissa, [exponent])
This function converts a number into scientific format. Useful for extended numbers that, being
string, Excel cannot format.
xcvexp(-6.364758987642234, 934) = -6.364758987642234E+934
xcvexp(1.2334567890122786, ) = 1.2334567890122786E-807
This function is useful also to convert any xnumbers into scientific notation, simply setting
exponent = 0 (default)
xcvexp(12342330100876523, 0) = 1.2342330100876523E+16
xcvexp(0.000023494756398348) = 2.3494756398348E-5
Split scientific format
xsplit(x)
This function returns an array containing the mantissa and exponent of a scientific notation.
If you press Enter this function returns only the mantissa. If you select two cells and give the
CTRL+SHIFT+ENTER sequence, you get both mantissa and exponent
Note that, in the last case, you cannot convert directly into double (for example, using the
VALUE function), even if the number of digits is less than 15. The exponent is too large for the
standard double precision.
Converting multiprecision into double
=xcdbl(x)
This function converts an extended number into standard double precision
It can be nested with other functions and/or array-functions.
Usually the extended numbers are too long for a compact visualization. So, after, the
multiprecision computation, we would like to convert the result in a compact standard precision.
For example, if you invert the matrix
2 1 4
7 12 4
-4 0 8
using the multiprecision xMatInv function, you will get a matrix like the following
0.3076923076923076923076923 -0.02564102564102564102564103 -0.1410256410256410256410256
-0.2307692307692307692307692 0.1025641025641025641025641 0.06410256410256410256410256
0.1538461538461538461538462 -0.01282051282051282051282051 0.05448717948717948717948718
Xnumbers Tutorial
35
If you use the functions xcdbl nested with the multiprecision function, the matrix will be
rounded in standard precision and the output will have a more compact format
Note that xcdbl affects only the output. The internal computing is always performed in
multiprecision.
Macros X-Edit
These simple macros are very useful for manipulating extended numbers in the Excel
worksheet. They perform the following operations:
Format Separates groups of digits
Unformat Removes the separator character
Double Conversion Converts multiprecision numbers into standard double precision
Round Rounding multiprecision numbers
Relative Round Relative rounding multiprecision numbers
Mop-Up Converts small numbers into 0
From this menu you can also change the default Digit_Max parameter
Using these macros is very simple. Select the range where you want to operate and then start
the relative macro. They work only over cells containing only numeric values, extended or
standard. Cells containing function are ignored
Tip. For stripping-out a formula from a cell and leaving its value, you can select the cell and
then click in sequence (copy + paste values)
Here are same little examples:
Format - group 6
31415926.53589793238462643 31,415926.535897,932384,62643
19831415926.53589793238462 ⇒ 19831,415926.535897,932384,62
0.535897932384626433832734 0.535897,932384,626433,832734
Double Conversion
31415926.53589793238462643 31415926.5358979
19831415926.53589793238462 ⇒ 19831415926.5358
0.535897932384626433832734 0.535897932384626
Rounding 3 decimals.
31415926.53589793238462643 31415926.536
19831415926.53589793238462 ⇒ 19831415926.536
0.535897932384626433832734 0.536
Xnumbers Tutorial
36
Relative rounding - significant digits 15.
4.5399929762484851535591E-5 4.53999297624849E-05
1.0015629762484851535591E-6 ⇒ 1.00156297624849E-06
0.539929762484851535591E-12 5.39929762484852E-13
Mop-Up - Error limit 1E-15.
31415926.53589793238462643 31415926.53589793238462643
-1.00E-15 ⇒ 0
5.78E-16 0
-1.40E-18 0
Note that the function mopup is used overall for improving the readability. The cells having
values greater than the limit are not modified.
Macro X-Converter
This macro1
(*) converts a well formed Excel formula to the equivalent in terms of Xnumber
multiprecision functions (xadd, xmult, etc.).
The advantage over x-evaluate is that the code can be debugged in normal excel using small
values
and in a familiar form. When the spreadsheet works it is ' converted to using nested x-calls for
the precision work. The intention is to tag the conversion onto the copy worksheet function ' so
that you end up with a multi-precision copy of the original.
Its main features are:
• converts a range of cells directly on site or in a new worksheet.
• skips cells having functions not convertible.
• skips array functions except MINVERSE and MMULT that are substituted with the
correspondent x-functions xMatInv and xMatMult.
• Errors (if any ) are shown on the panel
The digits parameter can be set in 4 different ways:
1) an integer number i.e. 30
2) a fixed reference of a cell, i.e. $A$1
3) a name of a cell, i.e. "digits"
4) simply nothing. In that case all the x-functions use the internal default = 30
Of course not all the Excel Functions can be converted. The list of the Excel functions2
converted is:
* , / , + , - , ^ , ABS , ACOS , ACOSH , ASIN , ASINH , ATAN , ATANH , AVERAGE
, COMBIN , COS , COSH , EXP , FACT , INT , LN , LOG , MDETERM , MINVERSE ,
MMULT , MOD , PI , PRODUCT , ROUND , SIN , SINH , SQRT , STDEV , STDEVP , SUM
, TAN , TANH , TRUNC , VAR , VARP
1
The conversion engine of this macro was originally developed by John Jones in an smart m4 macro language which
makes the VB native j-code more simple (!) to write. It appears in this package thanks to his courtesy
2
The functions are indicated with their original English names, that usually are different from the local names.
Xnumbers Tutorial
37
Example. Assume to have an Excel formula in the cell B6 calculating the norm of a 3dim vector
like the following worksheet.
We would transform this formula into a multiprecision one using the x-functions of the
Xnumbers add-in
Select the cell B6 and start the macro X-Converter from the menu X-Edit
Range to convert: the range containing the one or more formulas to convert
Digit Parameter: specifies the global precision digits for all the functions under conversion.
You can leave "(Default)" or blank meaning that all the function will use the internal default
precision (usually 30 digits). But you can set a reference cell (example $A$1) where you have
set your own precision. Or you can insert a name of a cell (example "mydigits") that you have
defined. You can also insert directly a number (example 25)
Output Option: sets the output where the multiprecision function will be copied. Select "on
site" if we want to convert the formula directly in the selected cell. Select "new sheet" if we want
to perform the conversion in a new worksheet. The original formula will be preserved.
Let's select "on site" and click "run". The worksheet look will look like as the following
As we can see the original standard formula
=SQRT(B2+B3+B4)
in cell B6 has been substituted with the multiprecision formula
Xnumbers Tutorial
38
=xsqr(xadd(xadd(B2,B3),B4)).
working with the default precision digits (30)
Note that there are 3 nested functions in the converted formula.
The maximum number of nested functions in Excel is 8.
If an error raises during the cell conversion, the formula is left unchanged and the text is turned
in red. The macro always shows a short list of the errors encountered during the conversion.
The macro works also with a range selection
Example. In the following worksheet we have same functions in the range A3:A18
Note that the cell A18 contains the
function COUNTA that are not the
similar x-function
Note that the range A14:A15
contains an array function { ... },
thus the cells A14 and A15 are
inseparable.
The cells A7 contain a simple
constant
We want to convert them, where possible, in a new worksheet without affect the original
worksheet.
For that, select all the range A3:A18 and start the X-Converter
Select "new sheet" and click "run". The new worksheet look like as the following
Note that the cell A7, A14, A15, A18 are unchanged
Note that the original cells A9 = B7^12 and A13 =A11^2.6 having the same operator symbol
"^", are substituted with two different x-functions: xpow for integer power and xexpa for float
power.
Xnumbers Tutorial
39
Statistical Functions
Factorial
xfact(n, [Digit_Max])
Returns the factorial of an integer number xfact(n)= n!
This example shows all 99 digits of 69!
xfact(69, 100) = 711224524281413113724683388812728390922705448935203693936480-
40923257279754140647424000000000000000
If the parameter Digit_Max is less than 99, the function returns the approximate result in
exponential format:
xfact(69) = 1.71122452428141311372468338881E+98
For large number (n>> 1000) you can use the faster function xGamma(x). The relation between
the factorial and the gama function is:
Γ(n) = (n-1)!
Factorial with double-step
xfact2(n, [Digit_Max])
Returns the factorial with double step.
if n is odd ⇒ xfact2(n) = 1⋅3⋅5⋅7⋅9...n
if n is even ⇒ xfact2(n) = 2⋅4⋅6⋅8⋅ ...n
Note: In many books, this function is indicate improperly as "double factorial", or - even worse - with the
confusing symbol "!!".
Combinations
xComb(n, k, [Digit_Max])
Returns the binomial coefficients, a combination of n, class k. xcomb = C n,k
The example below shows all the 29 digits of the combination of 100 objects grouped in class
of 49 elements:
xComb(100,49) = 98913082887808032681188722800
Combinations of N = 100
objects in class of 10, 20, …
90
Note the typical parabolic
outline of the binomial
coefficients
For large argument (n and k >>1000) use the faster function xcomb_big(n,k) .
Xnumbers Tutorial
40
Permutations
xPerm(n, [k], [Digit_Max])
Returns the permutation of n, class k. xperm(n,k)= Pn,k.
If k is omitted, the function assume k = n and in this case will be P(n) = n!
Examples:
xPerm(100, 20, 60) = 1303995018204712451095685346159820800000
xPerm(100) = 9.33262154439441526816992388562E+157
Arithmetic Mean
xmean(x, [Digit_Max])
Returns the arithmetic mean of n numbers, extended or not. The argument is a range of cells.
n
x
M i∑=
Geometric Mean
xgmean(x, [Digit_Max])
Returns the geometric mean of n numbers, extended or not.
n
nxxxx ...GM 221=
Quadratic Mean
xqmean(x, [Digit_Max])
Returns the quadratic mean of n numbers, extended or not.
n
x
QM i∑=
2
Standard Deviation
xstdev(range, [Digit_Max])
xstdevp(range, [Digit_Max])
Return the deviation of n numbers, extended or not. Range is a range of cells. The optional
parameter Digit_Max, from 1 to 200, sets the number of significant digits (default 30)
• xstdev returns the standard deviation
1
)( 2
−
−
=
∑
n
xx
σ
Xnumbers Tutorial
41
• xstdevp returns the population standard deviation
n
xx
p
∑ −
=
2
)(
σ
Variance
xvar(range, [Digit_Max])
xvarp(range, [Digit_Max])
Return the variance of n numbers, extended or not. Range is a range of cells. The optional
parameter Digit_Max, from 1 to 200, sets the number of significant digits (default 30)
• xvar returns the standard variance
1
)( 2
−
−
=
∑
n
xx
v
• xvarp returns the population variance
n
xx
vp
∑ −
=
2
)(
Probability distributions
Xnumbers contains several type of probability distribution functions
DSBeta(x, a, b, [dtype]) Beta distribution 0 < x < 1 , a > 0, b > 0,
DSBinomial(k, n, p, [dtype]) Binomial distribution k integer, n integer , 0 < p < 1
DSCauchy(x, m, s, n, [dtype]) Cauchy (generalized) distribution n integer , s > 0
DSChi(x, r, [dtype]) Chi distribution r integer, x > 0
DSErlang(x, k, l, [dtype])) Erlang distribution k integer, x > 0
DSGamma(x, k, l, [dtype])) Gamma distribution x > 0, k > 0, l > 0
DSLevy(x, l, [dtype])) Levy distribution x > 0, l > 0
DSLogNormal(x, m, s, [dtype])) Log-normal distribution x > 0, m ≥ 0, s > 0
DSLogistic(x, m, s, [dtype])) Logistic distribution x > 0, m ≥ 0, s > 0
DSMaxwell(x, a, [dtype])) Maxwell-Boltzman distribution x > 0, a > 0
DSMises(x, k, [dtype])) Von Mises distribution k > 0, -π < x < π
DSNormal(x, m, s, [dtype])) Normal distribution s > 0
DSPoisson(k, z, [dtype])) Poisson distribution k integer, z > 0
DSRayleigh(x, s, [dtype])) Rayleigh distribution x > 0, s > 0
DSRice(x, v, s, [dtype])) Rice distribution (j=1 cumulative) x > 0, v ≥ 0 , s > 0
DSStudent(t, v, [dtype])) Student distribution (j=1 cumulative) v integer degree of freedom
DSWeibull(x, k, l, [dtype])) Weibull distribution (j=1 cumulative) x > 0, k integer, l > 0
The optional parameter dtype = 0 (default) returns the density distribution f(x); dtype = 1
returns the cumulative distribution F(x).
( ) ∫=<=
x
a
dttfxPxF )()( x
The lower limit "a" depends by the definition domain of the density function f(x).
Xnumbers Tutorial
42
Probability density definition
Rayleigh Gamma
Cauchy Beta
Lévy Logistic
Xnumbers Tutorial
43
Maxwell–Boltzmann Rice
Erlang Normal
Von Mises Weibull
Xnumbers Tutorial
44
Log-normal Binomial
knk
pp
knk
n
pnkf −
−
−
= )1(
)!(!
!
),,(
Student
2/)1(2
2
1
2
1
2
1
)1)((2
))1((
),( +
+Γ⋅
+Γ
= v
tvv
v
vtf
π
Poisson
!
),(
v
ex
vxf
xv −
=
Chi squared
)(2
),(
2
12/
2/12/
v
ex
vxf v
xv
Γ
=
−−
Xnumbers Tutorial
45
Xnumbers Tutorial
46
Univariate Statistic
xstatis(range, [digit_max])
This function returns the univariate statistic summary of a range of data.
The statistics computed are:
Total of numerical elements of the range
N
Arithmetic Mean
n
x∑
Standard Deviation
1
)( 2
−
−∑
n
xx
Popul. Standard Deviation
n
xx∑ − 2
)(
Autocorrelation lag1
∑
∑
−
−−
−
+
n
i
n
ii
xx
xxxx
1
2
1
1 1
)(
))((
This function returns a vector of 5 elements. Use the ctrl+shift+enter key sequence for insert it.
Xnumbers Tutorial
47
Linear Regression Coefficients
xRegLinCoef( Y, X, [DgtMax], [Intcpt])
Computes the multivariate linear regression with the least squares method in multi-precision
arithmetic.
Y is a vector (n x 1) of the dependent variable.
X is a list of independent variable. It may be a vector for monovariable regression (n x 1), or a
matrix for multivariate regression (n x m).
DgtMax sets the precision (default 30).
Intcpt = true/false. If true (default ) the algorithm calculates the intercept; otherwise the intercept
is set to 0
For monovariate regression, this function returns two coefficients [a0, a1]; the first one is the
intercept of Y axis, the second one is the slope.
Simple Linear Regression
Example. Evaluate the linear regression for the following xy data table
x y
0.1 1991
0.2 1991.001046
0.35 1991.001831
0.4 1991.002092
0.45 1991.002354
0.6 1991.003138
0.7 1991.003661
0.8 1991.004184
0.9 1991.004707
1 1991.00523
1.5 1991.007845
1.8 1991.009414
2 1991.01046
3 1991.01569
Let's see other examples
The model for this data set is
y = a0 + a1 x
Where [a0 , a1] are the unknown coefficents that
can be evaluate by the xRegLinCoef function
We can also compute the factor r2
in order to
measure the goodness of the regression
This can be done by the xRegLinStat function
Xnumbers Tutorial
48
Example of univariate regression
Example of multivariate regression
Example of linear regression with intercept = 0
Note that the x-regression functions always returns a vector of m+1 values [a0, a1, ...am]
independently if the intercept is 0 or not.
Multivariate Regression
This function can also compute a multivariate regression. This is when y depends by several
variables x1, x2, … xn. Look at this example
x1 x2 x3 y
0 0 -4 4000.8
0.1 0 -2 4000.7
0.2 0.5 -1 4001.55
0.3 0.5 0 4001.65
0.4 1 1.5 4002.4
0.5 1 2 4002.59
The model for this data set is
y = a0 + a1 x1+ a2 x2+ a3 x3
Where [a0 , a1 , a2 , a3] are the unknown
coefficients
Xnumbers Tutorial
49
Linear Regression - Standard Deviation of Estimates
xRegLinErr( Y, X, Coeff, [DgtMax], [Intcpt])
Returns the standard deviation of the linear regression of estimate
Y is a vector (n x 1) of dependent variable values.
X is a list of independent variable values. It may be a vector for monovariable regression (n x 1)
or a matrix for multivariate regression (n x m).
Coeff is the coefficients vector of the linear regression function [a0, a1, a2...am].
DgtMax sets the digits precision of computing.
Intcpt = true/false. If true (default ) the algorithm calculates the intercept deviation; otherwise
intercep a0 is set to 0
See above examples
Polynomial Regression
The same function finding the linear regression can easily be adapted to the polynomial
regression. In the example below we will find the best fitting polynomial of 3rd
degree for the
given data
x y
10 1120
11 1473
12 1894
13 2389
14 2964
15 3625
16 4378
17 5229
18 6184
19 7249
20 8430
The model for this data set is
y = a0 + a1 x+ a2 x2
+ a3 x3
where [a0 , a1 , a2 , a3] are the unknown coefficients
First of all, we add at the given table two extra columns containing the power x2
, x3
They can easily be computed in an Excel worksheet as shown above.
The polynomial coefficients can be computed by xRegLinCoef.
The exact result is y = 10 + x + x2
+ x3
Xnumbers Tutorial
50
Linear Regression Formulas
Generally, the multivariate linear regression function is:
mm xaxaxaay ...22110 +++=
where: [ ]maaaa ...,, 210
The coefficients of regression can be found by the following algorithm
Make the following variables substitution:
1..mifor =−= xxX ii
yyY −=
where the right values are the averages of samples y and x respectively:
∑= k ky
n
y
1
∑= k kii x
n
x ,
1
After that, the coefficients a= [a1, a2, ....an] are the solution of the following linear system
baC =⋅][
where [C] is the cross-covariance matrix, and b is the XY covariance














===
==
=
=
∑
∑∑
∑∑∑
∑∑∑∑
j jm
j jmjj j
j jmjj jjj j
j jmjj jjj jjj j
X
XXX
XXXXX
XXXXXXX
2
,
,,3
2
,3
,,2,3,2
2
,2
,,1,3,1,2,1
2
,1
..
..
..
C
















=
∑
∑
∑
∑
j jmj
j jj
j jj
j jj
XY
XY
XY
XY
,
,3
,2
,1
....
b
and the constant coefficient is given by:
∑
=
−=
m
i
ii XaYa
1
0
For m=1 we obtain the popular formulas of the monovariate linear regression
∑
∑
=
j j
j jj
X
XY
a 21 XaYa 10 −=
This is the linear solution known as the Ordinary Least Squares (OLS). The analysis of this kind
of approach shows that, for large dimensions of n (many measurement values) the matrix C
becomes nearly singular
Xnumbers Tutorial
51
Linear Regression Covariance Matrix
xRegLinCov ( Y, X, Coeff, [DgtMax], [Intcpt] )
Returns the (m+1 x m+1) covariance matrix of a linear regression of m independent variables
mm xaxaxaay +++= ...ˆ 22110
For a given set of n points ),( 21 imiiii yxxxP =
Parameter Y is an (n x 1) vector of dependent variable. Parameter X is a matrix of independent
variables. It may be an (n x 1) vector for monovariable regression or an (n x m) matrix for
multivariate regression.
Parameter Coeff is the (m+1) vector of the linear regression coefficents
Cross Covariance Matrix
Given the matrix X of the independent variables points
Intercept calculated Intercept = 0












=
mnn
m
m
xx
xx
xx
X
...1
............
...1
...1
1
212
111












=
mnn
m
m
xx
xx
xx
X
...
.........
...
...
2
212
111
The covariance matrix C is
( )12 −
⋅⋅= T
XXsC
Where:
( )
1
ˆ 2
2
−−
−
=
∑
mn
yy
s i ii
The covariance matrix C is
( )12 −
⋅⋅= T
XXsC
Where:
mn
yy
s ii
−
−
=
∑ 2
2
)ˆ(
Note that the square roots of the diagonal elements of the covariance matrix
iii cs =
are the standard deviations of the linear regression coefficients
Xnumbers Tutorial
52
Linear Regression Statistics
xRegLinStat( Y, X, Coeff, [DgtMax], [Intcpt])
Returns some statistics about the linear regression
R2
Square of the linear correlation factor
S y,x Standard deviation of the linear regression
Parameter Y is a vector (n x 1) of dependent variable.
Parameter X is a list of independent variable. It may be an (n x 1) vector for monovariable
regression or a (n x m) matrix for multivariate regression.
Coeff is the coefficients vector of the linear regression function [a0, a1, a2...am].
Formulas
The regression factor (better: the square of regression factor) R2
lie between 0 and 1 and
roughly indicates how closely the regression function fits the given values Y.
Generally, it can be computed by the following formula:
2
2
*
2
2*
2
1
)(
)(
1
y
yy
i i
i ii
yy
yy
R
σ
σ
−
−=
−
−
−=
∑
∑
Where
*
y is the value estimated by the regression function and y is the mean of y values.
mm xaxaxaay ...* 22110 +++=
∑= k ky
n
y
1
For monovariate regression (m=1), the above formula returns the popular formula:
( )
( )
∑
∑
∑
∑
−
−
=
n
y
y
n
x
x
R 2
2
2
2
2
Standard error of the linear regression is:
Intercept calculated Intercept constrained to 0
( )
1
2*
,
−−
−
=
∑
gln
yy
s i ii
xy
( )
gln
yy
s i ii
xy
−
−
=
∑
2*
,
Where gl = number of independent variables
Xnumbers Tutorial
53
Linear Regression Evaluation
= xRegLinEval(Coeff, X)
Evaluates the multivariate linear regression in multi precision arithmetic.
Parameter Coeff is the coefficients vector [a0. a1, a2, ....] of the linear regression
Parameter X is the vector of independent variables. It is one value for a simple regression
The functions return the linear combination.
nn xaxaxaay ...22110 +++=
Example: Plot the linear regression for the following data set
x y
-1 0.58
-0.8 0.65
-0.6 0.88
-0.4 1.25
-0.2 1.32
0 1.14
0.2 1.31
0.4 1.51
0.6 1.54
0.8 1.48
1 1.98
In this worksheet, each value of linear regression
*
y is computed by the function xRegLinEval.
The regression coefficients are computed by xRegLinCoef. The results are converted in double
by the function xcdbl.
Selecting the range A1:C12 and plotting the data we get the following regression graphs
0
0.4
0.8
1.2
1.6
2
2.4
-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1
y
y*
Xnumbers Tutorial
54
Polynomial Regression - Coefficient
xRegPolyCoef( Y, X, Coeff, [Degree], [DgtMax], [Intcpt])
Computes the polynomial regression with the least squares method in multi precision
arithmetic.
m
m xaxaxaay +++= ...2
210
with m > 1
Y is a vector (n x 1) of the dependent variable.
X is a vector (n x 1) of the independent variable
Degree is the degree m >1 of the polynomial
DgtMax sets the precision (default 30).
Intcpt = true/false. If true (default) the algorithm calculates the intercept; otherwise the intercept
is set to 0
The function returns the coefficient vector [a0, a1, a2...am]
Example. Find the 3rd degree polinomial fitting the given data set (xi, yi)
Polynomial Regression - Standard Deviation of Estimates
xRegPolyErr ( Y, X, Coeff, [Degree], [DgtMax], [Intcpt])
Returns the standard deviation of the polynomial regression estimate
Y is a vector (n x 1) of dependent variable values.
X is a vector (n x 1) of the independent variable
Coeff is the coefficients vector [a0, a1, a2...am]. of the polynomial regression
DgtMax sets the precision (default 30)..
Intcpt = true/false. If true (default) the algorithm calculates the intercept; otherwise the intercept
is set to 0
see above example
Polynomial Regression Statistics
xRegPolyStat ( Y, X, Coeff, [Degree], [DgtMax], [Intcpt])
Computes the R-squared factor of the polynomial regression and the standard deviation of the
regression.
Xnumbers Tutorial
55
2
R R-Squared coefficient of the polynomial regression
xyS , Standard Deviation of the polynomial regression
Y is a vector (n x 1) of dependent variable values.
X is a vector (n x 1) of the independent variable
Coeff is the coefficients vector [a0, a1, a2...am]. of the polynomial regression
DgtMax sets the precision (default 30).
Intcpt = true/false. If true (default ) the algorithm calculates the intercept deviation; otherwise
this is set to 0
If you wanto to see the standard deviation select two cells and give the CTRL+SHIFT+ENTER
sequence.
Macro - Regression
Xnumbers contains two macros performing the linear and polynomial regression in
multiprecision arithmetic. From the Xnumbers menu, select
• Macro  Regression  Linear
• Macro  Regression  Polynomial
Data XY is an array containing the column of the variable X at the left with the adjacent column
of Y at the right. For a m-multivariate regression also the columns of X must be exactly m.
Select this range before starting the macro. In this case the input box will be automatically filled
Data XY for univariate and
polynomial regression
Data XY for
multivariate regression
Output cell indicates the starting upper left cell of the output area.
Intercept. If checked the macro calculates the intercept otherwise it is set to 0
Convert to double. Converts the multiprecision in double before the output
Xnumbers Tutorial
56
R-squared + residual std. dev. Calculate the correspondent statistics
Estimate std. dev. Calculate the standard deviation of estimate
Digits Max. from 1 to 200, sets the precision (default 30)
Degree. The panel of the Polynomial Regression macro is similar to the Linear except for the
input degree box that allows to chose the polynomial degree.
An example of the regression output is.
To obtain this result follow this steps:.
• Select the range A2:C7 and start the macro.
• Select all the option boxes and deselect the "double conversion" in
order to see all the 30 digits.
Note. The text has been added by hand only for clarity. The macro do not write them. We do it.
The coefficient a0 (intercept) will be always output. If the intercept is switched off, the result in
the cell F2 will be 0
Xnumbers Tutorial
57
Sub-Tabulation
One important application of linear regression is the sub-tabulation, which is the method to
extract a table of values with smaller step from an original table with bigger steps. In other
words, we can obtain a fine tabulation from a table with a few values of a function. Let’s see
this example.
Example: Extract from the following dataset, a table having 11 values with step 0.1, from 0 to 1
x y
0 5.1
0.2 4.7
0.5 4.5
0.6 4.3
0.7 4.2
1 3.6
3.00
3.50
4.00
4.50
5.00
5.50
0 0.2 0.4 0.6 0.8 1
The graph shows the extra points added by the sub tabulation. Note that this method is
different from the interpolation because the regression line does not pass through any of the
original points. The new values of the table B are different from the ones table A even in the
same x-values.
This feature came in handy when we want to regularize the row data.
Data Conditioning
The conditioning of the data consists of subtracting the mean from the values of the sample. It
can improve the accuracy of the linear regression, but the regression coefficients obtained -
conditioned coefficients - are different from the regression coefficients of the row data. They
can be back-transformed by the following method:
Given X and Y two data vectors, the linear regression polynomial of n degree well be:
First of all, we find the linear regression coefficients
[ a0 , a1 ]
Than we re-calculate the values
y i = a0 + a1 h , i = 1…10, h = 0.1
Xnumbers Tutorial
58
∑=
⋅=
n
i
i
i xaxp
0
)(
We made the data conditioning, making the average of X and Y
11
∑∑ == ii y
n
yx
n
x
Substituting the old variables with the new variable u and v
yyvxxu iiii −=−=
Than, the new linear regression polynomial well be:
∑=
⋅=
n
i
i
i ubup
0
)(
The original ai coefficients can be obtained from the new bi coefficients by the following
formulas.
i
i
n
i
i
xbya ⋅⋅−+= ∑=0
0 )1( ki
i
n
ki
ki
k xb
k
i
a −
=
+
⋅⋅





−= ∑ )1(
This method is often useful for increasing the gloabal accuracy of the linear regression
Xnumbers Tutorial
59
Linear Regression with Robust Method
RegLinRM(x, y, [Method])
This function1
performs the linear regression with three different robust methods:
- SM: simple median
- RM: repeated median
- LMS: least median squared
Robust methods are suitable for data containing wrong points. When data samples have noise
(experimental data), the basic problem is that classic LMS (least minimum squared) is highly
affected by noisy points. The main goal of robust methods is to minimize as much as possible
the influence of the wrong points when fitting the function
The parameter x and y are two vectors of the points to fit.
The optional parameter "Method" sets the method you want to use (default = SM)
The functions returns an array of two coefficients [a1, a0] where
01 axay +⋅≅
Use CTRL+SHIFT+ENTER to paste it.
Example: Suppose you have sampled 5 experimental values (xi, yi), with a (suspected) large
error in the last value 6.5.
x y
1 1.1
2 2
3 3.1
4 3.8
5 6.5
In the graph are shown the
regression lines obtained with
all robust methods in
comparison with the standard
OLS regression.
As we can see all the lines SM, RM, LMS (Robust Methods) minimize the influence of the value
(5, 6.5)
1
The routines for robust linear regression were developed by Alfredo Álvarez Valdivia. They appear in this collection
thanks to its courtesy
Linear Regression
0
1
2
3
4
5
6
7
0 1 2 3 4 5 6
Ordenadas
SM
RM
LMS
MM.CC.Standard
Xnumbers Tutorial
60
Linear Regression Min-Max
RegLinMM(x, y)
This function performs the linear regression with the Min-Max criterion (also called Chebychev
approximation) of a discrete dataset (x, y)
The parameter "x" is a (n x 1) vector of the independent variable,
The parameter "y" is a (n x 1) vector of the dependent variable
The function returns the coefficients [a0, a1] of the max-min linear regression
xaay 10
~ +=
As known, those coefficients minimize the max absolute error for the given dataset
|)(~|max ii yxyE −=
Example. Find the better fitting line that minimize the absolute error
The liner regression is y ≅ 0.428 + 1.142 x
with an error max Emax ≅ ± 0.7
The scatter plot shows the lineare regression approximation
As we can see, all the points lie in the plane strips of ± Emax around the min-max line (pink
line). (Emax ≅ 0.7)
Xnumbers Tutorial
61
NIST Certification Test
The multiprecision regression functions of Xnumbers have been tested against the NIST linear
regression data sets. The table below gives the LRE results from xRegLinCoef and
xRegPolyCoef functions
NIST test for linear regression (row data sets)
StRD Datasets
Level of
difficulty
Model of
class
Coeff. Function
LRE
Norris low Linear 2 xRegLinCoef 15.0
Pontius low Quadratic 3 xRegPolyCoef 15.0
NoInt1 Average Linear 1 xRegLinCoef 15.0
Filip high Polynomial 11 xRegPolyCoef 15.0
Longley high Multilinear 7 xRegLinCoef 15.0
Wampler1 high Polynomial 6 xRegPolyCoef 15.0
Wampler2 high Polynomial 6 xRegPolyCoef 15.0
Wampler3 high Polynomial 6 xRegPolyCoef 15.0
Wampler4 high Polynomial 6 xRegPolyCoef 15.0
Wampler5 high Polynomial 6 xRegPolyCoef 15.0
The table below gives the LRE results on the NIST test univariate data sets obtained from
xStats function
NIST test for univariate data sets
Dataset Category Difficulty Size Mean
Stand.
Dev.
Autocor.
Coef.
PiDigits Univariate 1 5000 15 15 15
Lottery Univariate 1 218 15 15 15
Lew Univariate 1 200 15 15 15
Maveo Univariate 1 50 15 15 15
Michelso Univariate 1 100 15 15 15
NumAcc1 Univariate 1 3 15 15 15
NumAcc2 Univariate 2 1001 15 15 15
NumAcc3 Univariate 2 1001 15 15 15
NumAcc4 Univariate 3 1001 15 15 15
Xnumbers Tutorial
62
Transcendental Functions
Logarithm natural (Napier’s)
xLn(x, [Digit_Max])
Returns the natural logarithm (or Napier’s) , in base "e"
The argument may be either normal or extended number.
Example:
xLn(30) = 3.4011973816621553754132366916
Logarithm for any base
xLog(x, [base], [Digit_Max])
Returns the logarithm for any base (default 10)
)(log xy base=
The argument may be either normal or extended number.
Example
xlog(30) = 1.47712125471966243729502790325
Exponential
xexp(x, [Digit_Max])
Returns the exponential of x in base "e" xexp(x) = e
x
Example
e10
= xexp(10) = 22026.4657948067165169579006452
e1000
= xexp(1000) = 1.97007111401704699388887935224E+434
Note the exponent 434 of the second result. Such kind of numbers can be managed only with
extended precision functions because they are outside the standard limits of double precision.
Exponential for any base
xexpa(x, [a], [Digit_Max])
Returns the exponential of x in any in base xexpa(x, a) = a
x
The arguments “a” and “x” may be either normal or extended numbers, with a > 0.
If the base "a" is omitted the function assumes a = 10.
21.234
= xexpa(1.234, 2) = 2.3521825005819296401155858555
10−0.54
= xexpa(-0.54) = 0.288403150312660594239196924659
Xnumbers Tutorial
63
Constant e
xe([Digit_Max])
Returns Euler's constant "e", the base of the natural logarithm.
The optional parameter Digit_Max, from 1 to 415, sets the number of significant digits (default
30).
xe() = 2.71828182845904523536028747135
xe(60) = 2.71828182845904523536028747135266249775724709369995957496696
Constant Ln(2)
xLn2([Digit_Max])
Returns the constant Ln(2).
The optional parameter Digit_Max, from 1 to 415, sets the number of significant digits (default
30).
Constant Ln(10)
xLn10([Digit_Max])
Returns the constant Ln(10).
The optional parameter Digit_Max, from 1 to 415, sets the number of significant digits (default
30).
Hyperbolic Sine
xsinh(x, [Digit_Max])
Returns the hyperbolic sine of x in multiprecision arithmetic
2
sinh
xx
ee −
−
=
Hyperbolic ArSine
xasinh(x, [Digit_Max])
Returns the hyperbolic arsine of x in multiprecision arithmetic
( )1ln)(asinh 2
++= xxx
Xnumbers Tutorial
64
Hyperbolic Cosine
xcosh(x, [Digit_Max])
Returns the hyperbolic cosine of x in multiprecision arithmetic
2
)cosh(
xx
ee
x
−
+
=
Hyperbolic ArCosine
xacosh(x, [Digit_Max])
Returns the hyperbolic Arcosine of x in multiprecision arithmetic
The argument x, normal or extended, must be x >1
( ) 1,1lnacosh 2
>−+= xxx
Hyperbolic Tangent
xtanh(x, [Digit_Max])
Returns the hyperbolic tangent of x in multiprecision arithmetic
xx
xx
ee
ee
x −
−
+
−
=)tanh(
Hyperbolic ArTangent
xatanh(x, [Digit_Max])
Returns the hyperbolic artangent of x in multiprecision arithmetic
The argument x, normal or extended, must be |x| < 1
1,
1
1
ln
2
1
)(atanh <





−
+
= x
x
x
x
Euler constant γ
=xeu( [Digits_Max] )
Returns the Euler-Mascheroni constant γ
(The same constan returned by xGm function)
Example
xeu() = 0.57721566490153286060651209008
xeu(60) = 0.57721566490153286060651209008240243104215933593992359880576
Xnumbers Tutorial
65
Trigonometric Functions
Sin
xsin(a, [Digit_Max])
Returns the sine of the angle a xsin(a) = sin(a)
The argument a, in radians, may be either a normal or an extended number.
xsin(1.5) = 0.997494986604054430941723371141
Cos
xcos(a, [Digit_Max])
Returns the cosine of the angle a xcos(a) = cos(a)
The argument a, in radians, may be either a normal or an extended number.
xcos(1.5) = 7.07372016677029100881898514342E-2
Computation of cos(π/2)
Example: compute cos (89,99999995°) with the standard built-in function COS function
COS(89.99999995) = COS(1.570796326) = 7.94896654250123E-10
The correct answer, accurate to 15 digits, is 7.94896619231321E-10
As we can see, only 7 digits are corrected. The remaining 8 digits are meaningless. On the
contrary, with the multiprecision function xcos(x) we have the correct result with all its
significant digits.
xcos(1.570796326) = 7.94896619231321E-10
The table below shows the computation effect when a approaches π /2
angle α α (deg) xcos(α) COS(α) built-in Err %
1.57 89.95437383553930 7.96326710733325E-4 7.96326710733263E-04 7.75E-14
1.570 89.95437383553930 7.96326710733325E-4 7.96326710733263E-04 7.75E-14
1.5707 89.99448088119850 9.63267947476522E-5 9.63267947476672E-05 -1.55E-13
1.57079 89.99963750135470 6.32679489657702E-6 6.32679489666849E-06 -1.45E-11
1.570796 89.99998127603180 3.26794896619225E-7 3.26794896538163E-07 2.48E-10
1.5707963 89.99999846476560 2.67948966192313E-8 2.67948965850537E-08 1.28E-09
1.57079632 89.99999961068120 6.79489661923132E-9 6.79489670660314E-09 -1.29E-08
1.570796326 89.99999995445590 7.94896619231321E-10 7.94896654250123E-10 -4.41E-08
1.5707963267 89.99999999456290 9.48966192313216E-11 9.48965963318629E-11 2.41E-07
1.57079632679 89.99999999971950 4.89661923132169E-12 4.89658888522954E-12 6.20E-06
As we can see, the accuracy of the standard function COS decreases when the angle
approaches the right angle. On the contrary, the xcos function keeps its accuracy.
Xnumbers Tutorial
66
Tan
xtan(a, [Digit_Max])
Returns the tangent of a xtan(a) = tan(a)
The argument a, in radians, may be either a normal or an extended number.
Arcsine
xasin(a, [Digit_Max])
Returns the arcsine of a xasin(a) = arcsin(a)
The arcsine is defined between -π/2 and π/2
The argument a, where 1|| ≤a , may be either a normal or an extended number.
Arccosine
xacos(a, [Digit_Max])
Returns the arccosine of a xacos(a) = arccos(a)
The arccosine is defined between 0 and π
The argument a, where 1|| ≤a , may be either a normal or an extended number.
Arctan
xatan(a, [Digit_Max])
Returns the arctan of a xatan(a) = arctan(a)
The arctan(a) is defined between
2/arctan(a)2/ ππ <<−
Constant π
These functions return the following multiples of π
xpi([Digit_Max]) xpi = π
xpi2([Digit_Max]) xpi2 = π/2
xpi4([Digit_Max]) xpi4 = π/4
x2pi([Digit_Max]) x2pi = 2π
The optional parameter Digit_Max, from 1 to 415, sets the number of significant digits (default
30).
Example. Compute the Hermite-Ramanujan constant with 36 significant digits
163π
e
xexp(xmult(xpi(36),xsqr(163,36),36),36) = 262537412640768743.999999999999250005
Xnumbers Tutorial
67
Complement of right angle
xanglec(a, [Digit_Max])
Returns the complement of angle a to the right angle
xanglec(α) = π/2−α
where 0 ≤ α ≤ π/2 .
Example:
xanglec(1.4) = 0.17079632679489661923132169163
For angles not too near the right angle this function is like the ordinary subtraction. The use of
this function is computing the difference without loss of significant digits when the angle is very
close to the right angle. For example, computing in Excel the following difference:
(PI()/2 − 1.570796) = 1.57079632679490 − 1.570796 = 0.00000032679490
we have a loss of 7 significant digits, even though the computation has been made with 15
significant digits. On the contrary, if we use:
xanglec(1.570796 , 15) = 3.26794896619231E-7
we get the full precision with 15 significant digits. The "lost" digits are automatically replaced
Xnumbers Tutorial
68
Polynomial Rootfinder
The roots of polynomials are of interest to more than just mathematicians. They play a central
role in applied sciences including mechanical and electrical engineering where they are used in
solving a variety of design problems.
Xnumbes provides several macros based on the following polynomial rootfinder algorithms.
RootFinder JT
Jenkins and Traub algorithm
(translated in VB from the original rpoly FORTRAN 77 subroutine)
RootFinder GN Generalized Newton-Raphson method
RootFinder ADK Aberth, Durand, Kerner algorithm
Rootfinder RF Ruffini's method for real integer roots.
Rootfinder LB Lin-Bairstow algorithm
Rootfinder SK Siljak algorithm
Rootfinder LA Laguerre algorithm
These macros are able to find, in a few seconds, all the roots - real or complex - of a dense
polynomial up to 15th - 20th degree, in double or multi-precision. It is remarkable that
sometimes the results has shown in an exact way, even if the computation is intrinsically
approximate.
The characteristics of each rootfinder are reassumed in the following table
Macro Roots Coefficients Arithmetic
RootfinderJT Complex Real Standard
RootfinderGN Complex Real Multi-precision
RootfinderDK Complex Complex Multi-precision
RootfinderRF Real, Integer Real, Integer Multi-precision
RootfinderLB Complex Real Standard
RootfinderSK Complex Complex Multi-precision
RootfinderLA Complex Real Standard
Xnumbers Tutorial
69
Input parameters
The polynomial rootfinder interface is simple and straight.
Method:
JT Jenkins-Traub Standard Real coefficients
GN Gen. Newton-Raphson Multiprecision Real coefficients
ADK Aberth-Durand-Kerner Multiprecision Complex coefficients
RF Ruffini Standard Real Integer coefficients
LB Lin-Bairstow Standard Real coefficients
SK Siljak Multiprecision Complex coefficients
LA Laguerre Standard Real coefficients
Coefficients input: is an array containing the polynomial coefficients with increasing
degree, from top to bottom. May be also a single cell containing the polynomial formula string,
such as:
-120+274x-225x^2+85x^3-15x^4+x^5
RootfinderDK and Siljak can also accept complex coefficients. In that case the input is an
(n x 2) array. Examples of possible input are (thick black box):
Remarks.
The formula string is more adapt for sparse polynomials.
Real coefficients can be put in horizontal or vertical vector. Complex coefficients, only in
vertical vectors
Results Output: It is the upper left corner of the output area. If blank, the routine assumes the
cell nearest the given coefficients range.
Error: Sets the relative roots accuracy. The algorithm terminates when the relative difference
between two iterations is less then this value.
Iter: The algorithm stops when the iterations counter reaches this value.
Multi-Precision: Enable/disable the multi-precision arithmetic
MP-out: If checked, the results are written in multi-precision, otherwise they are converted in
standard double precision.
Xnumbers Tutorial
70
Output
The rootfinder macros output their results in the following simplified layout
The roots and their estimated relative errors are written in a table starting from the left upper
cell indicated in the input window. In the right-bottom cell is written the total elaboration time in
seconds
Note: we have formatted the table
only for clarity. The macros do not
perform this task. We do it.
Integer Rootfinder output
The macro RootfinderRF outputs all integer roots of the polynomial (if any) at the left and the
coefficients of the remainder polynomial (deflated polynomial) at the right
RootfinderRF
Polynomial
Integer
roots
Reduced
Polynomial
This result means that the given polynomial
8704119046280328151058214720 2345678
−+−−+−+− xxxxxxxx
can be factorized as
)3438234()8)(2)(2( 2342
+−+−+−+ xxxxxxx
Xnumbers Tutorial
71
How to use rootfinder macros
Using polynomial rootfinder macros is simple. Select the polynomial coefficients and start the
rootfinder that you prefer. All input fields are filled and the only work that you have to do – in the
most cases - is to press "Run".
Example. Select the range C3:C11 and start the RootfinderJT . The coefficients input-box is
filled with C3:C11 and the result-output box is filled with the cell E3.
Press “run” and - after a while - the routine ends and the roots will be displayed at the right,
like in the following fugure
Sparse polynomials. We can pass to the rootfinder macros also symbolic polynomial string,
(that it is the faster way for sparse high degree polynomials). Let’s see this example
Find all roots of the following 16th
degree polynomial
x^16-6817x^8+1679616
Write this string in a cell, select it and start a rootfinder macro
Xnumbers Tutorial
72
In this case we have used the Durand-Kerner algorithm obtaining a very high accuracy
(practically the highest accuracy in standard double precision)
Root Error Estimation
The third column produced by the rootfinder macros is an estimation of the relative root error,
defined as:
||/|~| iii xxxer −= for 0|| >ix
where x~ is the true unknown root and ix is the approximate root given by the rootfinder
We have to say that this number should be regarded as an estimation of “goodness” of the
root found; small values (for example 1E-9 , 1E-12 ) indicate an high precision of the
correspondent root. On the contrary, larger values (for examples 1E-3 , 1E-5) indicates a
“difficult” roots that require an extra investigation.
For example assume to find the root of the following 6th
degree polynomial
Clustering effect: In this case, the accuracy is enough good, but quite lower than the previous
example. The reason is that the roots:
– 1, 1.01, 1.02, 1.03, 1.04, 1.05
are very close each other (0.1% of difference)
Xnumbers Tutorial
73
Complex polynomials. The macro RootfinderDK and RootfinderSK can solve also complex
polynomials. Example: find the roots of the following polynomial with complex coefficients
5432
)3(5)515(4)412()( zzizzizizP ++−+−−+++−=
Select both real and imaginary coefficients columns and start the macro RootfinderKD
The roots are z = ±1 , z = ±2 , z = 3 − j . Observe that the results are shown in exact mode even
if the computation is intrinsically approximated.
A polynomial of n degree, having as roots
the first 1, 2,..n integers, belongs to the
Wilkinson class that, as known, is
ill-conditioned. This dense polynomial is
usually assumed as standard reference for
polynomial rootfinder algorithms. We have
tabulated the LRE (log relative error)
obtained with all the rootfinder macros.
As we can see, for a Wilkinson polynomial of
20th
degree, we have good about four
significant digits (0.1% accuracy)
Wilkinson polynomial test
0.0
2.0
4.0
6.0
8.0
10.0
12.0
14.0
16.0
0 5 10 15 20 25
LRE
But all polynomials are so hard to solve? Fortunately not. Many polynomials with higher
degree, can be solved with good accuracy
For example, if we try to get all real roots of the 16th
degree Legendre’s polynomial
L16(x) = 6435 -875160 x2
+19399380 x4
-162954792 x6
+ 669278610 x8
-1487285800 x10
+1825305300 x12
-1163381400 x14
+300540195 x16
We have a general accuracy of more than 13 digits
Legendre polyn. Coeff. Real Imm Rel. Err.
6435 -0.989400934991646 0 2.9585E-17
0 -0.944575023073157 0 1.6352E-13
-875160 -0.865631202387904 0 3.673E-14
0 -0.755404408355024 0 1.1559E-14
19399380 -0.617876244402639 0 1.0779E-14
0 -0.458016777657228 0 7.4625E-16
-162954792 -0.281603550779259 0 1.9313E-16
0 -0.095012509837637 0 8.5038E-18
669278610 0.095012509837637 0 8.5038E-18
0 0.281603550779259 0 1.4485E-16
-1487285800 0.458016777657228 0 1.5356E-15
0 0.617876244402640 0 5.0196E-15
1825305300 0.755404408354981 0 2.5615E-14
0 0.865631202387767 0 4.6793E-14
-1163381400 0.944575023073325 0 8.4139E-14
0 0.989400934991655 0 5.5583E-15
300540195
Xnumbers Tutorial
74
(remember that the higher degree coefficients are at bottom)
In the last column there are the errors estimation given by the rootfinder DK. They are slight
different from the true roots errors, but we have to remember that this column must be regard
as an index of the root approximation: low values mean a good accuracy, larger values often
(but not always) indicate a poor approximation
Integer roots
In applied science it's rarely to came across in polynomials having exact integer roots.
Nevertheless, they are frequent in math, didactical examples and algorithm testing .
Xnumbers has a dedicated special macro for finding the integer real roots of a polynomial. It
uses the Ruffini's method with the QD algorithm for roots isolation.
This method is generally less efficient then JT or DK but it can gain in accuracy.
The roots found by this method have no round-off errors so the deflated polynomial is exact.
Therefore, in that case, the root-finding-deflating process is without errors.
For polynomial having a mix of integer real roots, complex roots and real roots the method
returns the integer roots and the coefficients of the deflated polynomial that can be solved with
the aid of the general purpose macros: DK, GN or JT. Because the deflated polynomial has a
lower degree, the roots accuracy will be generally higher than by solving directly the given
polynomials.
Let's see how it works practically. Assume to have the following polynomial
degree coeff
a0 8678880
a1 -13381116
a2 8844928
a3 -3279447
a4 746825
a5 -107049
a6 9437
a7 -468
a8 10
The exact roots are:
integer real complex
5, 6, 7, 8, 9 2.8 5.0i5.4 ±
If we try to solve this 8th
degree polynomial with a general
rootfinder, probably the best accuracy that we can obtain
is about 1e-10, that it is a good result but we can do better
if we extract the integer roots before and then, solving for
the remaining roots
Select the range B2:B10 and start the macro for extracting the integer roots and deflated
polynomial
find real and complex
roots (if exist)
Extract integer roots
(if exist)
original
P(x)
Deflated
Q(x)
Xnumbers Tutorial
75
The original polynomial is now cracked into the following factors
)57445711810)(8)(7)(6)(5( 23
−+−−−−− xxxxxxx
Now let's find the roots of the following 3rd
degree polynomial by, for example, the general JT
rootfinder. We obtain:
Re Im Rel. Err.
2.8 0 1.14E-17
4.5 0.5 1.44E-15
4.5 -0.5 1.44E-15
The general accuracy is better than 1e-14 , thousand times than the direct method.
Clearly is a good thing to keep attention to the integer roots (when they are).
1.0E-16
1.0E-14
1.0E-12
1.0E-10
1.0E-08
1.0E-06
1.0E-04
1.0E-02
1.0E+00
Rootfinder
RF + DK
Rootfinder
JT
Rootfinder
DK
Rootfinder
NG
Global roots accuracy
versus the solving
methods:
Rootfinder RF + DK
Rootfinder JT
Rootfinder DK
Rootfinder NG
The multiprecision should be used when the coefficients exceed 15 digits (remember that the
coefficients must be exact in order to extract the exact integer roots)
Let's see the following 18th
degree polynomial having the roots
Coefficients
-612914519230813800000
91181999821816015800
-5186948337826516202
137665995531841931
-1622627967498318
6214402509219
-11208193158
10605849
-5122
1
Polynomial roots
Note that same coefficients have 16 - 18 significant digits and
they must be inserted as x-numbers, (that is as string) in
order to preserve the original precision.
We have also to set the multiprecision check-box in the
macro RootfinderRF
integer real complex
25, 27,29, 31,
1000, 1001, 1002, 1003, 1004
none none
Note that this a so called "clustered polynomial" because some of its integer roots (1000, 1001,
1002, 1003, 1004) are very closed each other (difference less then 1%). This situation is quite
difficult for many algorithms and the accuracy is generally quite poor. On the contrary, the
Ruffini's method works very fine in that case.
Xnumbers Tutorial
76
Multiple roots
The macro "Factors" performs the decomposition of a polynomial with multiple roots into
smaller factors having all single roots. It uses the GCD method with the Euclid' algorithm
Example, the polynomial:
98765432
2017587827795744773765343132648 xxxxxxxxx +−+−+−+−+−
Has the roots x = 1 (m = 2), x = 2 (m = 3) and x = 3 (m = 4)
It can be decomposed into the product of the following factors
2322
)6116)(56)(3( xxxxxx +−+−+−+−
Using this macros is simple. Select the polynomial coefficients and start the macro. All the
input and output cells are automatically filled. Press Run
Each factors contains only single roots and thus can be solved with high precision by any
rootfinder macro1
.
1
For further details see "Non Linear Equations", Foxes Team, 2006
Xnumbers Tutorial
77
Polynomial Functions
Polynomial evaluation
=POLYN(z, Coefficients, [DgtMax])
Computes the polynomial at the value z.
n
n zazazaazP ...)( 2
210 +++=
The parameter Coefficients is the (n+1) column vector containing the polynomial coefficients
from the lowest to the highest order.
This function accept also complex coefficients. In that case the parameter Coefficients is an
(n+1 x 2) array.
The optional parameter DgtMax set the precision. If omitted, the function works in the faster
double precision.
This function works also for complex arguments. In that case, z must be a complex number
(two adjacent cells) and the function returns two values. To see both real and imaginary part,
select two cells and give the CTRL+SHIFT+ENTER key sequence. If you press only ENTER,
the function returns only its real part.
Example: compute the following real polynomial
4252)( 234
++−+= zzzzzP
for iz 24−=
Otherwise, if you want to compute a real polynomial for a real argument, e.g. z = 10 - simply
pass a single value
Xnumbers Tutorial
78
Example: compute the following complex polynomial
)54()2(5)1(2)( 234
izizzizzP −+−+−−+=
for iz 24−=
Polynomial derivatives
=DPOLYN(z, Coefficients, Order, [DgtMax])
Computes the polynomial derivative at the value z.
n
n zazazaazP ...)( 2
210 +++=
j
j
j
dz
zPd
zD
)(
)( =
The parameter "Coefficients" is the (n+1) vector containing the polynomial coefficients from the
lowest to the highest order.
This function accept also complex coefficients. In that case the parameter Coefficients is an
(n+1 x 2) array.
The parameter "Order" sets the order of the derivative.
The optional parameter "DgtMax" set the precision. If omitted, the function works in the faster
double precision.
This function works also for complex arguments. In that case, z must be a complex number
(two adjacent cells) and the function returns two values. To see both real and imaginary part,
select two cells and give the CTRL+SHIFT+ENTER key sequence. If you press only ENTER,
the function returns only its real part.
Example. Compute the derivatives of the following polynomial
32
23)( zzzzP +++=
For z= 3, we have:
Example: calculate the 2nd derivative of the following complex polynomial at the point
iz 24 −=
)54()2(5)1(2)( 234
izizzizzP −+−+−−+=
Xnumbers Tutorial
79
With DPOLYN and POLYN it is very easy to implement, for example, the Newton’s algorithm
for finding the polynomial root with high precision
Example: find the real root of the following polynomial with Newton’s algorithm
8000645 367
−+− xxx
The popular iterative Newton’s formula is
)('
)(
1
i
i
ii
xp
xp
xx +=+
Starting from the point x = 10. Note that we cannot use the handly x = 0 , because the
derivative is zero
The exact digits caught by the algorithm, are shown in blue. Note the impressive acceleration.
Try this example with 60 and more digits if you like.
Polynomial coefficients
=PolyTerms(Polynomial)
Returns the vector of the polynomial coefficients
The argument is a polynomial string like "1-3x+5x^2 +x^5" in any order.
Example
Xnumbers Tutorial
80
Note the braces { } in the formula. This indicates that the function return a vector. We must
select the range before enter the function with "shift+ctrl+enter".
Polynomial writing
=PolyWrite(Coefficients, [variable])
It returns the polynomial string from its coefficients.
The first argument may be a (1 x n) vector or an (2 x n) array. In the last case, the first row
indicates the coefficient position and the second row contains the correspondent coefficient
value.
The second optional argument specifies the variable string (default is "x").
Note that the second argument "t" must be insert as string, that is between quotes "..."
Polynomial addition
=PolyAdd(Poly1, Poly2)
Performs the addition of two polynomials. The arguments are monovariable polynomial strings.
Example:
PolyAdd("1-3x" , "-2-x+x^2") = "-1-4x+x^2" .
Polynomial multiplication
=PolyMult(Poly1, Poly2)
Performs the multiplication of two polynomials The arguments are monovariable polynomial
strings.
Example:
PolyMult("1-3x" , "-2+5x+x^2") = "-2+11x-14x^2-3x^3" .
( )( ) 322
3141125231 xxxxxx −−+−=++−−
Xnumbers Tutorial
81
Polynomial subtraction
=PolySub(Poly1, Poly2)
Returns the difference of two polynomials. The arguments are monovariable polynomial strings.
Example:
PolySub("1-3x" , "-2+5x+x^2") = "3-8x-x^2" .
Polynomial division quotient
=PolyDiv(Poly1, Poly2)
Returns the quotient of two polynomials. The arguments are monovariable polynomial strings.
Example:
PolyDiv("x^4-1" , "x^2-x-1") = "2+x+x^2" .
In fact:
( )( ) xxxxxx 31211 224
++++−−=−
Polynomial division remainder
=PolyRem(Poly1, Poly2)
Returns the remainder of two polynomials
The arguments are monovariable polynomial strings.
Xnumbers Tutorial
82
Hermite’s and Cebychev’s polynomials
By the basic operations we can build any other polynomial.
Example: Calculate the first 9th
degree Cebychev’s and Hermite’s polynomials
Cebysev’s polynomials can be obtained by
the iterative formula
Hermite’s polynomials can be obtained by
the iterative formula
xTT == 10 ,1
11 2 −+ −⋅= nnn TTxT
xHH == 10 ,1
11 22 −+ ⋅−⋅= nnn HnHxH
The two iterative formulas can be arrange as:
=polysub(PolyMult("2x",Tn),Tn-1)
=polysub(PolyMult("2x",Hn),PolyMult(2*n,Hn-1))
These functions are inserted from the cell B4 to B9 and C5 to C9
Legendre’s Polynomials
Legendre’s polynomials can be obtained by the following well known iterative formula
xPPxP
n
n
xPx
n
n
xP nnn ==⋅
−
−⋅⋅
−
= −− 1021 ,1,)(
1
)(
12
)(
The first five polynomials are:
( ) ( ) ( )33035
8
1
,35
2
1
,13
2
1
,,1 24
4
3
3
2
210 +−=−=−=== xxPxxPxPxPP
The above formula is very popular, but from the point of view of numeric calculus has one
disadvantage: its coefficients are decimal and this causes round-off errors leading inaccuracy
for higher polynomial degree. It is convenient to rearrange the iterative formula to avoid
fractional coefficients.
Let’s assume that a Legendre’s polynomial can be written as
)(
1
)( xL
k
xP n
n
n =
Where kn is an integer number and Ln(x) is a polynomial having integer coefficients
The Legendre’s polynomial Pn(x) is completely defined by the couple of (kn , Ln(x))
Starting with
(1a)
Xnumbers Tutorial
83
1
1
1
0
=
=
k
k
xL
L
=
=
1
0 1
We can show that the following iterative process, with n ≥ 2 , gives the couples (kn , Ln(x))
)()()()(
)1(
)12()(
21
1
2
xLaxLxUxV
nka
xnkxU
nnnnn
nn
nn
−−
−
−
⋅−⋅=
−⋅=
−⋅=
( ))(,
21
nnn
nnn
VcoefbGCDc
kknb
=
⋅⋅= −−
Where the coeff operator returns the coefficients vector of the polynomial Vn(x), and the GCD is
the greatest common divisor.
Simplifying, we get, finally the couple (kn , Ln(x))
)(
1
)( xV
c
xL
c
b
k
n
n
n
n
n
n
=
=
This iterative algorithm, working only with integer values, is adapted to build Legendre’s
polynomials with high degree.
Let’s see how to arrange a worksheet for finding Legendre’s polynomial
In the first column we insert the degree n, beginning from 0 to 2, for the moment
In the last two columns “k” and “L(x)” we have added the starting values.
The row 6 contains all the functions that the process needs.
In particularly we note:
The function polyterms(D6) gives the coefficients vectors [-1, 0, 3] of V(x) = -1+3x^2
The function xMCD returns the greatest common divisor of [-1, 0, 3, 2] ⇒ 1
Select the row 6 and drag it down. We generate the Legendre’s polynomial in the form (1a)
Xnumbers Tutorial
84
Here is a table of Legendre’s polynomials obtained with the above method
n k L(x)
0 1 1
1 1 x
2 2 -1+3x^2
3 2 -3x+5x^3
4 8 3-30x^2+35x^4
5 8 15x-70x^3+63x^5
6 16 -5+105x^2-315x^4+231x^6
7 16 -35x+315x^3-693x^5+429x^7
8 128 35-1260x^2+6930x^4-12012x^6+6435x^8
9 128 315x-4620x^3+18018x^5-25740x^7+12155x^9
10 256 -63+3465x^2-30030x^4+90090x^6-109395x^8+46189x^10
11 256 -693x+15015x^3-90090x^5+218790x^7-230945x^9+88179x^11
12 1024 231-18018x^2+225225x^4-1021020x^6+2078505x^8-1939938x^10+676039x^12
13 1024 3003x-90090x^3+765765x^5-2771340x^7+4849845x^9-4056234x^11+1300075x^13
We can also extract a table of Legendre’s coefficients by the Polyterms() function
Xnumbers Tutorial
85
Polynomial shift
=PolyShift(Poly, x0)
Performs the polynomial translation to x0,
The argument "Poly" can be the polynomial strings or the vector of polynomial coefficients.
This function returns the coefficient vector of the translated polynomial.
If you select one cell, the output will be a polynomial string
Example. Given the polynomial:
65432
1448633275809495250447389623188784918 xxxxxx +−+−+−
substituting x with z+24, we have
6432
916918 zzzzz +−−−+−
This function is useful for transforming polynomial for reducing the coefficients amplitude and
improving the precision of rootfinder methods. In this example we work with coefficients of two
maximum digits, instead of 9 digits. We note also that the second polynomial, having the
second coefficient = 0, is centered. His roots are the same of the given polynomial, translated
of 24, but can be factorize much better. In fact, we have
)9)(2)(1( 222
−+++− zzzzz
Polynomial center
=PolyCenter(Coefficients)
Returns the center of the polynomial roots circle
The argument specifies the vector of the polynomial coefficients in the following order:
[ ]naaaa ...,, 210
It can also be a polynomial string
if x1, x2, ...xn are roots of polynomial the center Bx is defined as:
n
a
n
xxxx nn
x
1210 ...
B −−
=
+++
=
Xnumbers Tutorial
86
Polynomial roots radius
=PolyRadius(Coefficients)
Returns the approximated radius of the polynomial roots circle.
The argument is the vector of the polynomial coefficients in the following order:
[ ]naaaa ...,, 210
It can also be a polynomial string
If zi are the roots of a polynomial, the radius is defined as:
|)(|max
...1
i
ni
zR
=
=
.The circle of root is very useful for locating all the roots of a polynomial. For example, given the
following 9 degree polynomial.
degree coefficents
a0 -3098250
a1 4116825
a2 -2427570
a3 916272
a4 -244674
a5 46934
a6 -6430
a7 608
a8 -36
a9 1
The center = 4 and the radius ≅ 6.8
We can draw the circle containing, with high probability, all polynomial roots
We know that the roots of this polynomial are:
x real x imm
9 0
5 0
2 0
3 -6
3 6
1 -4
1 4
6 -3
6 3
We have to point out that this method is probabilistic. It means that, picking-up a random
polynomial, the most part of the roots are found inside the circle but it is also possible to find
same roots outside the circle with 1% of probability.
Example: compute the root circle of the polynomial:
x7
-5x6
+64x3
-8000
Xnumbers Tutorial
87
radius ≅ 4.331
center ≅ 0.714
The roots are:
x real x imm
-2.7429701 1.6132552
-2.7429701 -1.6132552
-0.4369651 3.2182957
-0.4369651 -3.2182957
3.17993518 2.2060806
3.17993518 -2.2060806
5 0
Polynomial building from roots
=PolyBuild(Roots, [Variable])
Builds a polynomial from its roots. Argument "Roots" is an (n x 2) array, contains the
polynomial roots. It can be an (n x 1) vector for real roots.
This function returns the coefficient vector of the polynomial.
If you select one cell, the output will be a polynomial string
Complex roots for real polynomial: Multiple roots:
Complex roots for complex polynomial
If the complex roots are not symmetrical, the
polynomial has both real and imaginary part.
Zero roots .If you want a polynomial with
multiple zero roots, simply repeat many couple
[0, 0] as it needs.
This function returns the vector of polynomial coefficients if you select more than two vertical
cells. It is useful for higher degree polynomial
Xnumbers Tutorial
88
In this example we get the 10
coefficients of the 9th
degree
polynomial having the 9 roots in the
range A2:B10.
Select the range E2:E11 and insert
the function PolyBuild with the
CTRL+SHIFT+ENTER sequence.
If complex roots are not conjugate,
the polynomial has complex
coefficients. This function returns also
the imaginary part of the coefficients.
Simply select the range D2:E6.
Xnumbers Tutorial
89
Polynomial building with multi-precision
PolyBuildCfx()
This macro generates the polynomial coefficients from the polynomial roots.
This macro works like the function PolyBuild except that it works in multi-precision. It is useful
for high degree polynomial, when the coefficients become longer than 15 digits.
For using this macro select the range
containing the roots.
Then, start the macro. Choose the digits
precision (default=30) and the range you want
to paste the coefficients (default is the range
at the right side of the roots range selected).
In the following table we have calculated the coefficient of the polynomial having as roots the
first 19 integer numbers. That is:
x1 = 1 , x2 = 2, x3 = 3 ,.... x19 = 19
Roots PolybuildCfx (30 digits) PolyBuild Diff.
1 -121645100408832000 -121645100408832000 0
2 431565146817638400 431565146817638000 400
3 -668609730341153280 -668609730341153000 -280
4 610116075740491776 610116075740492000 -224
5 -371384787345228000 -371384787345228000 0
6 161429736530118960 161429736530119000 -40
7 -52260903362512720 -52260903362512700 -20
8 12953636989943896 12953636989943900 -4
9 -2503858755467550 -2503858755467550 0
10 381922055502195 381922055502195 0
11 -46280647751910 -46280647751910 0
12 4465226757381 4465226757381 0
13 -342252511900 -342252511900 0
14 20692933630 20692933630 0
15 -973941900 -973941900 0
16 34916946 34916946 0
17 -920550 -920550 0
18 16815 16815 0
19 -190 -190 0
1 1 0
As we can see there are a little difference (digits in red) between the exact coefficients
computed by this macro PolyBuildCfx (multiprecision arithmetic with 30 digits) and those
returned by the function PolyBuild (standard double precision).
Xnumbers Tutorial
90
Polynomial Solving
=PolySolve (Polynomial)
This function returns the roots of a given real polynomial using the Jenkins-Traub algorithm.
n
n xaxaxaa ...2
210 +++
The arguments can be a monovariable polynomial strings like "X^2+3x+2" or a vector of
coefficients
This function returns an (n x 2) array.
It uses the same algorithm of the RootfinderJT macro. It works fine with low-moderate degree
polynomials, typically up to 10th
degree. For higher degree it is more convenient to use the
macro.
Example. Find all roots of the given 10 degree polynomial
Integer polynomial
=PolyInt(Polynomial)
This function returns a polynomial with integer coefficients having the same roots of the given
polynomial. This transformation is also know as "denormalization" and can be useful when the
coefficients of the normalized polynomial are decimal.
Example: Given thefollowing polynomial:
-0.44+2.82x-3.3x^2+x^3
To eliminate decimal coefficients we denormalize the polynomial
-22+141x-165x^2+50x^3 = PolyInt("-0.44+2.82x-3.3x^2+x^3")
Take care with the denormalization because the coefficients became larger and the
computation may lose accuracy. See the example below
The following polynomials have the same root x = 11/10:
Pb(x) = -2.4024+10.1524x-17.1x^2+14.35x^3-6x^4+x^5
Pa(x) = -6006+25381x-42750x^2+35875x^3-15000x^4+2500x^5
Xnumbers Tutorial
91
If we compute both polynomials for x = 11/10, with standard double precision we get:
Pa(1.1) = -2.664E-15
Pb(1.1) = 4.547E-12
As we can see, the first value, obtained by the decimal polynomial, is 1000 times more precise
then the one obtained by the integer polynomial
Polynomial System of 2nd
degree
=SYSPOLY2(Poly1, Poly2)
Solves a system of two 2nd
degree polynomials.




=+++++
=+++++
0
0
202524
2
2322
2
21
101514
2
1312
2
11
ayaxayaxyaxa
ayaxayaxyaxa
It returns a (4 x 4) array containing the four solutions.
The parameters "Poly1" and "Poly2" can be coefficients vectors or polynomials strings
The coefficients must be passed in the same order of the above equation.
Polynomial strings, on the contrary, can be written in any order. Examples of 2nd
degree x-y
polynomials strings are:
13+x+y^2-y+x^2+2x*y
x^2 + y^2 - 10
4x^2+8x*y+y^2+2x-2
Note: the product symbol “*” can be omitted except for the x*y mixed term
A 2nd
degree system can have up to four solutions. It can also have no solution (impossible) or
even infinite solutions (undetermined). The function returns #N/D if a solution is missing
Example: solve the following system




=−+
=−+++
010
02
22
22
yx
yxyxyx
Using SYSPOLY2 the solutions – real or
complex – can be obtained in a very quick
way
Real solutions represent the intersection
point of the curve poly1 and poly2.
They are: P1 = (−3 , 1) , P2 = (−1 , 3)
The system has also two complex solutions that have not a geometrical representation
P3 = (2.5 +j 1.118034 , −2.5 +j 1.118034) , P4 = (2.5 −j 1.118034 , −2.5 −j 1.118034)
The degree of the given system is 4
Example: solve the following system



=−+++
=−
012
01
2
yxyxy
xy
Xnumbers Tutorial
92
The apparent degree of the system is 2 x 2 = 4
As we can see, the function SYSPOLY2 returns only three solutions: one real and two
complex.
P1 = (−1, −1) , P2 = (−j , j ) , P3 = = ( j , −j )
Thus, the actual system degree is 3.
Bivariate Polynomial
=POLYN2(Polynomial, x, y, [DgtMax])
Returns the value - real or complex - of a bivariate polynomial P(x, y).
The parameter "Polynomial" is an expression strings. Valid examples are:
13+x+y^2-y+x^2+2x*y , x^2+y^2-10 , 8x*y+y^2+2x-2 , 10+4x^6+x^2*y^2
Note: the product symbol “*” can be omitted except for the x*y mixed terms
The third optional parameter DgtMax sets the multiprecision. If missing, the computation is
performed in faster double precision.
The variables x , y can be real or complex. The function can return real or complex numbers.
Select two cells if you want to see the imaginary part and give the CTRL+SHIFT+ENTER
sequence
Example: Compute the polynomial
yxyxyxP −+++= 22
2
at the point
x = (2.5 + j 1.11803398874989)
y = (−2.5 + j 1.11803398874989)
Verify that it is a good approximation of the
polynomial root
Xnumbers Tutorial
93
Partial fraction decomposition
Partial fraction decomposition is the process of rewriting a rational expression as the sum of a
quotient polynomial plus partial fractions..
∑+=+= iFxQ
xD
xR
xQ
xD
xN
)(
)(
)(
)(
)(
)(
where each Fi is a fractions of the form
m
m
px
A
px
A
px
A
)(
...
)( 2
21
+
+
+
+
+
or
m
mm
cbxx
CxB
cbxx
CxB
cbxx
CxB
)(
...
)( 222
22
2
11
++
+
+
++
+
+
++
+
being m is the multiplicity of the correspondent root
The denominators D(x) is determined from the poles of the fractions itself. In fact, p is a real
root of D(x), while the quadratic factor can be obtained from the complex root using the
following relation
22
,2 βααβα +=−=⇒± cbi (1)
Many calculators and computer algebra systems, are able to factor polynomials and split
rational functions into partial fractions. A solution can also be arranged in Excel with the aid of
Xnumbers functions. Let's see
Real single poles. Find the fraction decomposition of the following rational fraction
541056515
179414332763
)(
)(
234
23
+−+−
+−+
=
xxxx
xxx
xD
xN
First of all, we try to find the roots of the denominator using, for example, the function
polysolve. We find that the roots are pi = [1, 2, 3, 9]. They are all real with unitary multiplicity,
therefore the fraction expansion will be
4
4
3
3
2
2
1
1
)(
)(
px
A
px
A
px
A
px
A
xD
xN
+
+
+
+
+
+
+
=
where pi are the roots and Ai are unknown
Several methods exist for finding the fraction coefficients Ai . One of the most straight and
elegant is due to Heaviside that, for a real single root, simply states:
)('
)(
i
i
i
pD
pN
A =
where D'(x) is the derivative of D(x)
A possible arrangement in Excel is the following
Xnumbers Tutorial
94
Therefore, the searched decomposition is
9
40
3
5
2
8
1
40
541056515
179414332763
234
23
+
+
+
−
+
+
+
−=
+−+−
+−+
xxxxxxxx
xxx
You can prove yourself that this expression is an identity, thus always true for every x, except
the poles.
Complex single poles. Find the fraction decomposition of the following rational fraction
65042292
1235221
)(
)(
234
23
+−−−
++−−
=
xxxx
xxx
xD
xN
First of all, we try to find the roots of the denominator using, for example, the function
polysolve. We find that the roots are }34,25{ iip ±−±= . They are complex with unitary
multiplicity, therefore the fraction expansion will be
22
2
22
11
2
11
)(
)(
cxbx
CxB
cxbx
CxB
xD
xN
++
+
+
++
+
=
where bi and ci , calculated by the (1), are 25,8,26,10 2211 ===−= cbcb
The coefficients Bi and Ci are unknown. For solving them we used here the so called
undetermined coefficients method
Renamed, for simplicity:
22
2
211
2
1 )(,)( cxbxxDcxbxxD ++=++=
The fraction expansion may be rewritten as
)()()()()(
)(
2
2
2
2
1
1
1
1
xD
C
xD
xB
xD
C
x
xD
xB
xD
xN
+++=
Giving 4 different values to x, the above relation provides 4 linear equations with the unknown
B1, C1, B2, C2, that can be easily solved. We can choose any value that we want; for example xi
= { 0, 1, 2, 3 } and we get the following linear system
0 1/26 0 1/25 B1 123/650
1/17 1/17 1/34 1/34 C1 9/34
1/5 1/10 2/45 1/45
X
B2
=
3/10
3/5 1/5 3/58 1/58 C2 63/290
Solving this linear system by any method that we like, for examply by SYSLIN , we get the
solution
[B1 , C1 , B2 , C2] = [-2 , 7 , 1 , -2]
Substituting these values, we have finally the fraction decomposition
Xnumbers Tutorial
95
258
2
2610
72
65042292
1235221
22234
23
++
−
+
+−
+−
=
+−−−
++−−
xx
x
xx
x
xxxx
xxx
You can prove yourself that this expression is always valid for any compatible value of x
A possible arrangement for solving this
problem In Excel is a bit more
complicated then the previous one.
Let's see. First of all we compute the
roots with the function Polysolve; then
we compute the trinomials D1(x) and
D2(x) by the formulas (1)
Then we compute the polynomials N, D, D1, D2 for each values of x by the function polyn. We
get the 4x5 table visible at the right
From this table we get the complete system matrix in the following way.
The 4 x 4 linear system can be solved by any method that you want. For example by matrix
inversion as shown in the example.
Xnumbers Tutorial
96
Orthogonal Polynomials
Orthogonal polynomials are a class of polynomials following the rule:
∫ ⋅=
b
a
nmnnm cdxxpxpxw δ)()()(
Where m and n are the degrees of the polynomials, w(x) is the weighting function, and c(n) is
the weight. nmδ is the Kronecker's delta function being 1 if n = m and 0 otherwise.
The following table synthesizes the interval [a, b], the w(x) functions and the relative weigh c(n)
for each polynomials family
polynomial interval w(x)
Chebyshev polynomial of the
first kind
[-1, 1]
2/12
)1( −
− x



≠
=
0for2/
0for
n
n
π
π
Chebyshev polynomial of the
second kind
[-1, 1]
2/12
)1( x− 2/π
Gegenbauer polynomial [-1, 1]
2/12
)1( −
− α
x
0for/2
0for
)()(!
)2Γ(2
2
2
21
=
≠
Γ⋅+
+⋅
απ
α
αα
n
nn
αnπα-
Hermite polynomial (-∞,+∞)
2
x
e−
!2 nn
π
Jacobi polynomial (-1, 1)
βα
)1()1( xx +− nh
Laguerre polynomial [0, +∞) x
e−
1
generalized Laguerre
polynomial
[0, +∞) xk
ex −
!
)!(
n
kn +
Legendre polynomial [-1, 1] 1
12
2
+n
Where
)1(!
)1()1(
12
2 1
+++Γ⋅
++Γ⋅++Γ
+++
=
++
βα
βα
βα
βα
nn
nn
n
hn
Xnumbers Tutorial
97
Orthogonal Polynomials evaluation
This set of functions1
calculate the orthogonal polynomials and their derivatives at the given
point . They return two values: the first one is the polynomial value, the second is its 1st
derivative. If you want to see both values select two adjacent cells and give the
CTRL+SHIFT+ENTER sequence. If you give ENTER, you will get only the polynomial value
Function Poly_ChebychevT(x, n) Chebychev polynomial of the first kind
Function Poly_ChebychevU(x, n) Chebychev polynomial of the second kind
Function Poly_Gegenbauer(a, x, n) Gegenbauer polynomial
Function Poly_Hermite(x, n) Hermite polynomial
Function Poly_Jacobi(a, b, x, n) Jacobi polynomial
Function Poly_Laguerre(x, n, m) Laguerre generalized polynomial
Function Poly_Legendre(x, n) Legendre polynomial
Example: Tabulate the Legendre polynomial of 6th
degree, for 0 ≤ x ≤ 1, with step h = 0.1
As we can see we have insert Poly_Legendre as a standard function, because in this
exercise we do not need the derivative information
Example. Find the greatest zero of the 5th
degree Legendre polynomial
We can use the Newton-Raphson method,
starting from x = 1, as shown in the
worksheet arrangement.
Both polynomial and derivative are obtained
from the Poly_Legendre simply selecting
the range B5:C5 and pasting the function as
array with CTRL+SHIFT+ENTER sequence.
The other cells are filled simply by dragging
down the range B5:C5
1
Many thanks to Luis Isaac Ramos Garcia for his great contribution in developing this software
Xnumbers Tutorial
98
Function Poly_ChebychevT(x, [n])
Function Poly_ChebychevU(x, [n])
Evaluate the Chebychev orthogonal polynomial of 1st and 2nd kind
Parameters:
x (real) is the abscissa,
n (integers) is the degree. Default n = 1
Function Poly_Gegenbauer(L, x, [n])
Evaluate the Gegenbauer orthogonal polynomial of 1st and 2nd kind
Parameters:
x (real) is the abscissa,
n (integers) is the degree. Default n = 1
L (real) is the Gegenbauer factor and must be L < 1/2
Function Poly_Hermite(x, [n])
Evaluate the Hermite orthogonal polynomial of 1st and 2nd kind
Parameters:
x (real) is the abscissa,
n (integers) is the degree. Default n = 1
Function Poly_Jacobi(a, b, x, [n])
Evaluate the Jacobi orthogonal polynomial of 1st and 2nd kind
Parameters:
x (real) is the abscissa,
n (integers) is the degree. Default n = 1
a (real) is the power of (1-x) factor of the weighting function
b (real) is the power of (1+x) factor of the weighting function
Function Poly_Laguerre(x, [n], [m])
Evaluate the Laguerre orthogonal polynomial of 1st and 2nd kind
Parameters:
x (real) is the abscissa,
n (integers) is the degree. Default n = 1
m (integer) is the number of generalized polynomial. Default m = 0
Function Poly_Legendre(x, [n])
Evaluate the Legendre orthogonal polynomial of 1st and 2nd kind
Parameters:
x (real) is the abscissa,
n (integers) is the degree. Default n = 1
Xnumbers Tutorial
99
Weight of Orhogonal Polynomials
This set of functions calculate the weight c(n) of each orthogonal polynomial p(x, n)
[ ]∫=
b
a
nn dxxpxwc )()(
2
Function Poly_Weight_ChebychevT(n) Chebychev polynomial of the first kind
Function Poly_Weight_ChebychevU(n) Chebychev polynomial of the second kind
Function Poly_Weight_Gegenbauer(n, l) Gegenbauer polynomial
Function Poly_Weight_Hermite(n) Hermite polynomial
Function Poly_Weight_Jacobi(n, a, b) Jacobi polynomial
Function Poly_Weight_Laguerre(n, m) Laguerre generalized polynomial
Function Poly_Weight_Legendre(n) Legendre polynomial
If we divide each orthogonal polynomial family for the relative weight we have an orthonormal
polynomial family
Zeros of Orthogonal Polynomials
This macro finds all roots of the most popular orthogonal polynomials
Its use is very easy.
Simply start the Zero macro from the menu
"tools > Ortho-polynomials..."
Choose the family and the degree that you
want and fill the optional parameters
Then press OK
This is an example of output for a
Laguerre polynomial of 6th
degree
( m = 0)
Note: the format is added for clarity.
The macro does not do this
Xnumbers Tutorial
100
Coefficients of Orthogonal Polynomials
This macro calculate the coefficients of the most common orthogonal polynomials
Its use is very easy.
Simply start the Coeff macro from the menu
"tools/Ortho-polynomials..."
Choose the family and the degree that you
want and fill the optional parameters.
Then, press OK
This macro return also the polynomial weight
This is an example of output for a Laguerre
polynomial of 4th
degree (m = 0)
The ortho-polynomial can be written as
( )24967216
24
1
)( 234
6 +−+−= xxxxxL
Xnumbers Tutorial
101
Complex Arithmetic and Functions
Xnumbers provides a large collection of complex functions
Complex Addition
Complex Subtraction
Complex Multiplication
Complex Division
Polar Conversion
Rectangular Conversion
Complex absolute
Complex power
Complex Root
Complex Log
Complex Exp
Complex inv
Complex negative
Complex conjugate
Complex Sin
Complex Cos
Complex Tangent
Complex Inverse Cos
Complex Inverse Sin
Complex Inverse Tan
Complex Hyperbolic Sin
Complex Hyperbolic Cos
Complex Hyperbolic Tan
Complex Inverse Hyperbolic Cos
Complex Inverse Hyperbolic Sin
Complex Inverse Hyperbolic Tan
Complex digamma
Complex Exponential Integral
Complex Error Function
Complex Complem. Error Function
Complex Gamma Function
Complex Logarith. Gamma Function
Complex Zeta Function
Complex Quadratic Equation
Complex Expression Evaluation
How to insert a complex number
For definition a complex number is an ordered couple of numbers: (a,b)
In Excel a couple of numbers is represented by two vertical or horizontal adjacent cells and the
complex number (a, b) is a range of two cells. The figure below shows both vertical and
horizontal representations:
(234 , 105) in range "B7:C7" and in range "B2:B3"
( -100 , 23) in the range "E7:F7" and in range "D2:D3"
For entering complex functions you must select two cells, insert the complex function and give
the CTRL+SHIFT+ENTER keys sequence. If you press the ENTER key, the function returns
only the real part of the complex number.
Symbolic rectangular format
Xnumbers support the format "x+jy " only in expression strings passed to the function cplxeval.
Except this case, you must always provides a complex number as a couple of real numbers
(one or two cells).
The reason for this choice is that the rectangular format is more adapt for symbolic calculation
while the array format is more convenient for numerical computation characterized by long non-
integer numbers.
But, of course, you can convert a complex number (a,b) into its symbolic format "a+jb" by the
Excel function COMPLEX, as shown in the following example
Xnumbers Tutorial
102
XNUMBERS has two sets of complex functions: for standard double precision (prefixed by
"cplx") and for multiprecision (prefixed by "xcplx").
Complex Addition
xcplxadd(a, b, [Digit_Max])
cplxadd(a, b)
Performs the complex addition:
( ) ( ) ( )21212121 bb,aab,ba,a ++=+
Complex Subtraction
xcplxsub(a, b, [Digit_Max])
cplxsub(a, b)
Performs the complex subtraction.
( ) ( ) ( )21212121 bb,aab,ba,a −−=−
Complex Multiplication
xcplxmult(a, b, [Digit_Max])
cplxmult(a, b)
Performs the complex multiplication:
( ) ( ) ( )122122112121 baba,ba-bab,b*a,a +=
Complex Division
xcplxdiv(a, b, [Digit_Max])
cplxdiv(a, b)
Performs the complex division
( )
( ) 





+
−
+
+
= 2
2
2
1
2112
2
2
2
1
2211
21
21
bb
baba
,
bb
baba
b,b
a,a
Xnumbers Tutorial
103
Polar Conversion
xcplxpolar(z, [angle], [Digit_Max])
cplxpolar(z, [angle])
Converts a complex number from its rectangular form to the equivalent polar form.
The optional parameter angle sets the angle unit (RAD, DEG) (default RAD).
(x, y) ⇒ ( ρ , θ )
Where
22
yx +=ρ
0,atan >





= x
x
y
θ
0,
2
)sgn( =⋅= xy
π
θ





<≠⋅+





<=
=
0,0,)sgn(atan
0,0,
xyy
x
y
xy
π
π
θ
Rectangular Conversion
xcplxrect(z, [angle], [Digit_Max])
cplxrect(z, [angle])
Converts a complex number from its polar form to the equivalent rectangular form. The optional
parameter angle sets the angle unit (RAD, DEG) (default RAD).
( ρ , θ ) ⇒ (x, y)
Where
( )
( )θρ
θρ
sin
cos
=
=
y
x
Complex absolute
xcplxabs(z, [Digit_Max])
cplxabs(z)
Returns the absolute value of a complex number
2
2
2
1|| zzz +=
x y ρ θ (deg)
1 0 1 0
0.866025 0.5 1 30
0.707107 0.707107 1 45
0.5 0.866025 1 60
0 1 1 90
-0.5 0.866025 1 120
-0.70711 0.707107 1 135
-0.86603 0.5 1 150
-1 0 1 180
-0.86603 -0.5 1 -150
-0.70711 -0.70711 1 -135
-0.5 -0.86603 1 -120
0 -1 1 -90
0.5 -0.86603 1 -60
0.707107 -0.70711 1 -45
0.866025 -0.5 1 -30
Xnumbers Tutorial
104
Complex power
xcplxpow(z, [n], [Digit_Max])
cplxpow(z, [n])
Returns the nth
integer power of a complex number zn
(default n = 2)
( ) θ
ρ nnnn
eiyxz ⋅=+=
Where






=+=
y
x
yx atan,22
θρ
Complex Roots
xcplxroot(z, [n], [Digit_Max])
cplxroot(z, [n])
Returns all the nth
roots of a complex extended number z1/n
(default n = 2)
The function returns a matrix of (n x 2) values. Remember to press the sequence
CTRL+SHIFT+ENTER for insert properly this function.
The root of a complex number is computed by the De Moivre-Laplace formula.
1...1,0,
2
sin
2
cos −=










 +
⋅+




 +
⋅=+= nk
n
k
i
n
k
iyxz nnn πθπθ
ρ
where






=+=
y
x
yx atan,22
θρ
Note: If you select only one row, the function return only the first complex root (given for k = 0).
Example: compute all the 3 complex cubic roots of the number z = 8
Xnumbers Tutorial
105
Complex Log
xcplxLn(z, [Digit_Max])
cplxLn(z)
Returns the natural logarithm of a complex number
( ) θρ +=+= log)log()log( iyxz
Where:






=+=
x
y
yx atan,22
θρ
Complex Exp
xcplxExp(z, [Digit_Max])
cplxExp(z)
Returns the exponential of a complex number
)sin()cos( yieyeee xxiyxz
+== +
Complex inverse
xcplxinv(z, [Digit_Max])
cplxinv(z)
Returns the inverse of a complex number
2222
11
yx
y
i
yx
x
iyxz +
−
+
=
+
=
Complex negative
xcplxneg(z)
cplxneg(z)
Returns the complex negative
( ) iyxiyxz −−=+−=−
Complex conjugate
xcplxconj(z)
cplxconj(z)
Xnumbers Tutorial
106
Returns the conjugate of a complex number
iyxiyxz −=+=
Complex Sin
=cplxsin(z)
Returns the sine of a complex number
Complex Cos
cplxcos(z)
Returns the cosine of a complex number
Complex Tangent
cplxtan(z)
Returns the tangent of a complex number
Complex ArcCos
cplxacos(z)
Returns the arccosine of a complex number
Complex ArcSin
cplxasin(z)
Returns the arcsine of a complex number
Complex ArcTan
cplxatan(z)
Returns the arctangent of a complex number
Complex Hyperbolic Sine
cplxsinh(z)
Returns the hyperbolic sine of a complex number
Parameter “z” can be a real or complex number (two adjacent cells)
Xnumbers Tutorial
107
Complex Hyperbolic Cosine
cplxcosh(z)
Returns the hyperbolic cosine of a complex number
Parameter “z” can be a real or complex number (two adjacent cells)
Complex Hyperbolic Tan
cplxtanh(z)
Returns the hyperbolic tangent of a complex number
Complex Inverse Hyperbolic Cos
cplxacosh(z)
Returns the inverse of the hyperbolic cosine of a complex number
Complex Inverse Hyperbolic Sin
cplxasinh(z)
Returns the inverse of the hyperbolic sine of a complex number
Complex Inverse Hyperbolic Tan
cplxatanh(z)
Returns the inverse of the hyperbolic tangent of a complex number
Complex digamma
cplxdigamma(z)
Returns the logarithmic derivative of the gamma function for complex argument.
( )
)(
)('
)(ln)(
x
x
x
dx
d
x
Γ
Γ
=Γ=Ψ
Complex Exponential Integral
cplxei(z)
Returns the exponential integral of a complex number
∫
∞
−
−
−=
x
t
dt
t
e
xEi )(
Xnumbers Tutorial
108
Complex Error Function
cplxerf(z)
Returns the "error function" or "Integral of Gauss's function" of a complex number
∫
−
=
z
t
dtezerf
0
22
)(
π
Complex Complementary Error Function
cplxerfc(z)
Returns the complementary error function for a complex number
)(1)( zerfzerfc −=
Complex Gamma Function
cplxgamma(z)
Returns the gamma function for a complex number
∫
∞
−−
=Γ
0
1
)( dtetx tx
Complex Logarithm Gamma Function
cplxgammaln(z)
Returns the natural logarithm of the Gamma function for a complex number
Complex Zeta Function
cplxzeta(z)
Returns the Riemann zeta function ζ(s) for a complex number. It is an important special
function of mathematics and physics which is intimately related with very deep results
surrounding the prime number, series, integrals, etc.
For |s|>1 the function is defined as:
Xnumbers Tutorial
109
Complex Quadratic Equation
cplxEquation2(a, b, c, [DgtMax])
Returns the multiprecision solution of the quadratic equation with complex coefficients
02
=+⋅+⋅ czbza
where a, b, c are complex
The solutions are found by the resolution formula
a
acb
a
b
z
2
4
2
2
−
±−=
This function returns an (2 x 2) array
The optional parameter DgtMax, from 1 to 200, sets the number of the significant digits. If
missing, the computation is in standard double precision.
Example: Find the solution of the following complex equation with 20 digits precision
04)29(2
=++−+ iziz
Of course the function can also works with real coefficents equations
0322
=++ zz
Xnumbers Tutorial
110
Number Theory
Maximum Common Divisor
xMCD(a1, [a2])
MCD(a1, [a2], [a3]...)
Returns the Maximum Common Divisor (also called Greatest Common Divisor, GCD) of two or
more extended numbers
The arguments "a1" and "a2" may be single numbers or arrays (range). At least, two values
must be input. If "a1" is a range, "a2" may be omitted
Minimum Common Multiple
xMCM(a1, [a2])
MCM(a1, [a2], [a3]...)
Returns the Minimum Common Multiple (also Least Common Multiple, LCM) of two or more
extended numbers
The arguments "a1" and "a2" may be single numbers or arrays (range). At least, two values
must be input. If "a1" is a range, "a2" may be omitted
Example
Note that LCM may easily overcome the standard precision limit even if its arguments are all in
standard precision.
Xnumbers Tutorial
111
Rational Fraction approximation
xFract(x, [Digit_Max])
Fract(x, [ErrMax])
Returns the fractional approximation of a non-integer number x, the functions returns a vector
of two integer numbers, numerator N and denominator D :
x ≈ N / D
The optional parameter ErrMax sets the accuracy of the fraction conversion (default=1E-14).
The function tries to calculate the fraction with the maximum accuracy possible.
The algorithm uses the continued fraction expansion1
0,1
1,0
10
10
==
==
DD
NN
11
11
−+
−+
+⋅=
+⋅=
iiii
iiii
DDaD
NNaN
Where ai are found by the following algorithm:
( )
11
1
1 /int
++
+
+
⋅−=
=
=
iiii
ii
iii
ayxy
yx
yxa
In the example below we want to find
the fraction form of the number 0.126.
The function returns the solution:
N = 63 , D = 500
0.126 ≅ 63 / 500
Often the rational form is not so easy to find, and depends strongly on the precision we want to
reach.
See, for example, the fractions that approximate 2 with increasing precision
Digit N D N/D Error
2 3 2 1.500000000000000 0.08579
3 7 5 1.400000000000000 0.01421
4 41 29 1.413793103448280 0.00042
5 99 70 1.414285714285710 7.2E-05
6 239 169 1.414201183431950 1.2E-05
7 1393 985 1.414213197969540 3.6E-07
8 3363 2378 1.414213624894870 6.3E-08
9 8119 5741 1.414213551646050 1.1E-08
10 47321 33461 1.414213562057320 3.2E-10
11 114243 80782 1.414213562427270 5.4E-11
12 275807 195025 1.414213562363800 9.3E-12
13 1607521 1136689 1.414213562372820 2.8E-13
14 3880899 2744210 1.414213562373140 4.2E-14
15 9369319 6625109 1.414213562373090 1.3E-14
You can regulate the desiderate approximation with the parameter ErrMax
1
from The art of Computer Programming, D.E.Knuth, Vol.2, Addison-Wesley, 1969
Xnumbers Tutorial
112
Continued Fraction
xFractCont(x, [Digit_Max])
FractCont(x)
These functions perform the continued fraction expansion of a decimal number x
Usually the continued fraction is written in the
compact form x = [a0, a1, a2, a3...]
Each step gives the rational fraction fi = si / ti ,
where the fractions fi converge to the number x
....
1
1
1
3
2
1
0
+
+
+
+=
a
a
a
ax
Both the above functions return an array containing the integer values [ai, si, ti].
The algorithm stops when the fraction approximates x with the maximum precision allowed,
that is 10^-15 and 10^-(DgtMax) respectively for the standard and the multiprecision function.
Example. Compute the continued fraction
expansion of the number e2
= 7.38905...
with standard precision (error < 1E-15)
As we can see, the continued fraction of the
transcendental real number e2
can be written
as:
e2
= [7, 2, 1, 1, 3, 18, 5, 1, 1, 6, 30, 8...]
while the correspondent approximating
fractions are
e2
≅ 15/2 (1.5%),
e2
≅ 22/3 (0.75%),
e2
≅ 37/5 (0.15%),
e2
≅ 133/18 (0.002%),
....
Sometime the continued fraction expansion requires the multiprecision. Expand, for example,
the Ramanujian number
58π
e ≅ 24591257751.9999998222132414696... with 30 significant
digits
As we can see, the last fraction: 101111070168039846853 / 4111667292 approximates
58π
e with a relative error of about 1E-30
Xnumbers Tutorial
113
Continued Fraction of Square Root
FractContSqr(n)
This function returns the continued fraction
expansion of the square root of an integer
number
[ ]03210 2...,,, aaaaan =
....
1
1
1
3
2
1
0
+
+
+
+=
a
a
a
an
As known, the expansion is infinite and periodic.
The algorithm stops when it come across into a term double of the first term a0
This function returns the (m+1) vector [a0, a1, a2,...am ] where am = 2*a0
Use the ctrl+shift+enter sequence.
Example. Calculate the continued fraction of 77
Therefore the infinite, periodic continued fraction is
[ ]....16,1,3,2,3,1,16,1,3,2,3,1,16,1,3,2,3,1,877 =
Note that the function returns only the first terms + the period.
Check Prime
Prime(n)
Prime(n) = "P" if n is prime, or the lowest factor if n is not prime
Returns "?" if the function is not able to check it.
Example
prime(134560093) ="P"
prime(134560079)= 89
Next Prime
NextPrime(n)
This function1
returns the prime number greater than n or "?" if the function is not able to
calculate it
nextprime(9343560093) = 9343560103
Xnumbers Tutorial
114
Modular Addition
xaddmod(a, b, m)
Performs the modular addition1
)(mod)( mba +
where a, b integer and m positive integer
Modular Subtraction
xsubmod(a, b, m)
Performs the modular subtraction
)(mod)( mba −
where a, b integer and m positive integer
Modular Multiplication
xmultmod(a, b, m)
Performs the modular multiplication
)(mod)( mba⋅
where a, b integer and m positive integer
Modular Division
xdivmod(a, b, m)
Performs the modular division
)(mod)/( mba
where a, b integer and m positive integer
Remember that the modular division is always possible if, and only, the module m is prime.
Otherwise the division could be impossible. In that case the function returns "?"
Modular Power
xpowmod(a, p, m)
Performs the modular integer power of ap
That is defined as the remainder of the integer division of ap
by m






⋅−=
m
a
mar
p
p
1
Modular function xaddmod, xsubmod, xmultmod, xdivmod appear thanks to the courtesy of John Jones
Xnumbers Tutorial
115
Example: compute
)0059(mod324
xpowmod(3,24,9005) = 3306
It's easy to prove that
3306)9005mod(812824295364)9005mod(324
==
When the number a or p become larger it is impossible to compute the integer power directly.
But the function xpowmod can return the correct result.
Examples: compute
)3001mod(123939040
It would be impossible to compute all the digits of this power. Using multiprecision we have
xpow(12,3939040) = 1.24575154970238125896669174496E+4250938
This result shows that 123939040
has more then 4 million of digits!
Nevertheless the remainder of this impossible division is
xpowmod(12, 3939040,3001) = 947
Examples: Miscellanea of modular function
Observe that m = 1000453 is prime.
Xnumbers Tutorial
116
Perfect Square
xIsSquare(n)
Checks if a number n is a perfect square
xisSquare(1000018092081830116) = TRUE
Because: 1000018092081830116 = 10000090462
xisSquare(2000018092081830116) = FALSE
Check odd/even
xIsOdd (n)
Checks if a number n is odd (TRUE) or even (FALSE)
Check Integer
xIsInteger(x)
Checks if a number x is integer
Xnumbers Tutorial
117
Macro - Factorize
Factorize()
This macro factorize a number of active cell returning the list of prime number and their
exponents
ke
k
eee
ppppn ...3
3
2
2
1
1=
Select the cell contains the number you want to factorize and the run the macro Factorize from
the menu or Prime... > Factorize
Example. Assume to have in the cell A2 the following extended number
13320493696756339619246691430
Click “copy” if you want to copy the list in the worksheet, starting from the cell just below cell
A2.
This macro uses the trial division method with the prime table generated by the Eratostene's
sieve algorithm
This method is adapt for numbers having factors no more that 6 digts max. For higher factor
the elaboration time becames extremely long and it cames usefull a second factorization
method, the so called Pollard rho algorithm, for craking a number into two lower factors (not
necessary prime). Each factors, if not prime, can be factorized separately with the trial division
method.
Example. The number
18446744073709551617 = 274177 * 67280421310721
can be factorized with both methods: it requires about 33 sec with trial division; but less then 3
sec with Pollard method
The following number instead can be factorize only with Pollard method (about 40 sec).
10023859281455311421 = 7660450463 * 1308520867
Note that in this case both factors have 10 digits. The factos are prime so the factorization
stops itself.
For testing the primality see the probabilistic Prime test
Xnumbers Tutorial
118
Large numbers having factors more than 20 digits can be only manipulate with Xnumbers but
no more factorized. This task requires sophisticated algorithms joined with extreme fast
routines, usually written in C++ or Assembler.
When the numbers are very large, no efficient integer factorization algorithm is published; a
recent effort which factored a 200 digit number (RSA-200) took eighteen months and used over
half a century of computer time. The supposed difficulty of this problem is at the heart of certain
algorithms in cryptography such as RSA.
Not all numbers of a given length are equally hard to factor. The hardest instances of these
problems are those where the factors are two randomly-chosen prime numbers of about the
same size, but not too close
Factoring software is available either in commercial math packages or in standalone freeware
programs.
One of the most interesting program released in the public domain, supporting Quadratic Sieve
(QS) and Number Field Sieve (GNFS), that we have used for many years is Msieve.
Batch Factorization with Msieve
Msieve is a very power freeware program, created by Jason Papadopoulos, for factoring large
integers.
You can download the latest version of Msieve.exe from www.boo.net/~jasonp
No particular installation is required. Simply copy it in any folder that you like, for example,
/msieve.
What Msieve Does
Factoring is the study (half math, half engineering, half art form, and half... genial tricks) of
taking big numbers and expressing them as the product of smaller numbers. As the number to
be factored becomes larger, the difficulty involved in completing its factorization explodes and
the elaboration time increase sharply. The multiprecision library contained in Xnumber, written
in VBA, is no more sufficient for performing the factorization of number larger of 18-20 digits.
Msieve can with high probability find the complete factorization of any input number up to about
125 digits in size. The actual number of digits supported is much higher (up to 164 digits), but
problems larger than 125 digits are likely to fail.
Trial division is used on all inputs; if the result is less than 25 digits in size, tiny custom routines
do the factoring. For larger numbers, the code switches to more powerful methods. Prior to
version 1.04, those methods were limited to the quadratic sieve (QS). From that point on,
however, an implementation of Pollard-Brent algorithm and the number field sieve NFS are
also available.
A description of QS and NFS can be found in the Msieve Library itself with also a good amount
of Quadratic Sieve references.
The oldest users will be pleasant to know that Msieve.exe is a consolle program. It can be
started at the prompt command (the old DOS enviroment) using the following general syntax
>> msieve [options] [number]
Msieve supports many useful options. For a complete list, run the command
>> msieve -h
For factoring one number give the following command
>> msieve -q 8004000546054003543176004301
8004000546054003543176004301
p10: 1000400017
prp10: 2000000011
prp10: 4000400023
In this case the program only outputs all the factors found. The code "p10" indicates a prime
factor of 10 digits; "prp10" indicates a probable prime factor of 10 digits.
Xnumbers Tutorial
119
With the option -v (verbose), the program will also output the factoring trace with many useful
information (for factoring experts, of course).
>> msieve -v 8004000546054003543176004301
One useful option is also the time limit setting -d
>> msieve -v -d 10 number
This elaboration will stop after 10 minute, ended or not, and the intermediate results will be
saved in a working file, ready for a successive restart.
Onother way to stop a running elaboration is sending the key sequence CTRL+C to the active
command window.
The younger users will be happy to know that this elaboration can be performed directly from
Excel by the macro Numbers/Factors...
Note. The first time that you run Msieve from this macro, you have to provide the folder where
Msieve.exe is located. Follow the simple configuration instructions.
After that, the macro can start Msieve in batch input mode, returning output in Excel at the end
of the elaboration.
Using is very simple. Select the cell containing the number to factorize
Start the macro Numbers/Factors..., choose the Msieve and click on "Factorize".
If we like we can modify the job timeout (default 15 min).
The "Factorization Job Manager" lists and visualizes all the jobs submitted to Msieve.
Lists Refresh
Imports the results in
Excel
Kills a running job
Deletes a job
Verbose mode on/off
This Help
The jobs list is automatically refreshed every second. If the refreshing activity stops for some
reason, you can manually refresh it.
Xnumbers Tutorial
120
The factors obtained can be imported into Excel by the apposite buttom. The macro outputs
each factor in the format [exponent, factor]
An active factorization job runs in a minimize
window. If you like, you can open it at
normal size and observe what Msieve is
doing
The precedent submited jobs remain memorized until they are delete. They can be recalled
and viewed by the buttom Batch... of the Number Factorization panel
The factorization time is usually very fast for numbers up to 50 digits and remains reasonably
fast for numbers in the range 50 - 75 digits, as shows the following statistical graph
0.1
1
10
100
1000
0 10 20 30 40 50 60 70 80
Time (sec)
1.2 GHz, 256
Digits
Factorization function
Factor(n)
This function performs the decomposition in prime factors of a given integer number
Returns an array of two columns: the first column contains the prime factors and the second
column contains the exponents
This function is useful for factorizing on-line integer numbers N < 1E14 directly in the
worksheet.
It uses the so called "brute force attack" and the Fermat-Lehman method.
In this example, the given number is
decomposed in 5 factors
2277785128000 = 26
53
232
732
101
The #ND symbol indicates the end of factors
list. To make sure to get all factors you have
to extend the selection until you see this
symbol
Using this function is very simple. Select a range of 2 columns and several rows (for example 7
rows). The insert the function Factor using the ctrl+shift+enter sequence.
Xnumbers Tutorial
121
Macro - Prime Numbers Generator
PrimeGenerator
This macro is useful to generate your own table of prime numbers. The table begins from the
cell A1 of the active worksheet.
Prime Test
This macro perform the probabilistic prime test with the Fermat or Miller-Rabin method. These
tests are especially adapt for long number.
Using is very simple. Select the number that you want to test and start the macro from the
menu Macros / Numbers / Prime test
Select the method that you want and press "Test". After few seconds you get the results
Note that this test is exact for
detecting composite numbers,
but it can detect a prime number
with a finite probability (usually
very high).
The numbers satisfying the
Fermat or Miller-Rabin are called
"pseudo-prime"
The probability is correlated to the trials number T with the following approximate formula
Fermat test T
p −
−= 21 For T = 100 the probability is about
31
108.71 −
⋅−
Miller-Rabin test T
p −
−= 41 For T = 50 the probability is about
31
108.71 −
⋅−
You can also select a list of cells containing several numbers to test. This is useful for finding
prime in a set of consecutive integers.
The macro computes 10.000 (default)
prime numbers for each time.
The macro can be stop and restart as
you like
It always restarts from the last prime
number written in the worksheet.
Xnumbers Tutorial
122
Example: find the next prime number after 200000000000532100000000
A prime number must be odd, so let's begin to prepare a sequence of 20 or more odd numbers
starting from 200000000000532100000001
The frequency of prime numbers, in this range, is about 5%, so we hope to find a prime number
in our list. If this does not happen we try with a successive set of numbers, and so on, until a
prime comes out.
In this case we have found a probable prime 200000000000532100000003
You may prove by yourself that it is a true prime
Xnumbers Tutorial
123
Diophantine Equation
DiophEqu(a, b, c)
This function solves the Diophantine linear equation
Zyxcybxa ∈=+ ,
where a, b, c , x, y are all integer numbers
The integer solutions can
be expressed as ...2,1,0for
0
0
±±=



⋅+=
⋅+=
k
Dkyy
Dkxx
yk
xk
This function return an (2 x 2) array of four integer values.
The first row contains a particular solution, while the second row
contains the integer increments for generating all the solutions.
If you only want a particular solution [x0, y0] simply select an array of 2
adjacent cells. If the equation has no solution the function return "?"






yx DD
yx 00
Example. Find all the integer solutions of the equation 2x+3y = 6
As we can see, the function returns one
solution (-6, 6) and the increments (3, -2).
So all the integer solutions of the above
equation can be obtained from the following
formulas for any integer value of k



−=
+−=
ky
kx
k
k
26
36
Often is not so easy to find the solution of a diophantine equation. Let's see the following.
Long numbers. This function works also with extended numbers.
Example. Find a solution of the equation ax+by = c having the following coefficients
a b c
18760000596690052 13650847757772 64
Note that the first coefficients has 17
digits and the second one has 14
digits. Without multiprecision it would
be difficult to solve this problem.
You can enjoy yourself to prove that
the result returned from the function
DiophEqu is correct
Xnumbers Tutorial
124
Brouncker-Pell Equation
= PellEqu(d, [n])
This function solves the so called Brouncker-Pell quadratic equation on the integer domain
Ndyxydx ∈=⋅− ,,,122
The function returns the n-th
integer solution (default n = 1) using the continued fraction method.
As know, this equation has non trivial solutions if d is not a perfect square.
The function works for low-moderate integer d values but, even with this constraints,
sometimes this smallest solution is quite small, and sometimes it is huge. Let's see.
Compute the first 3 integer solutions of the equation 131 22
=⋅− yx
As we can see the solutions grow sharply also for very small d.
Sometime the first solution is huge also with moderate d. In that case is better to arrange the
solution in vertical cells, as shown in the following examples
Note that the "smallest" solution of the equation 1991 22
=⋅− yx has 30 digits !
When the solution exceed 250 digits or the time of 200 iterations the functions returns "?".
Xnumbers Tutorial
125
Euler's Totient function
= Totient(n)
Returns the number of integer not exceeding and relatively prime to n
This function is intended only for low-moderate numbers.
Remember that two numbers n , m are relative prime if, an only if, GCD(n, m) = 1
With this function is easy to set a table of the totient φ(n) values for 1 ≤ n ≤ 100
φ(n) 1 2 3 4 5 6 7 8 9 10
0 1 1 2 2 4 2 6 4 6 4
10 10 4 12 6 8 8 16 6 18 8
20 12 10 22 8 20 12 18 12 28 8
30 30 16 20 16 24 12 36 18 24 16
40 40 12 42 20 24 22 46 16 42 20
50 32 24 52 18 40 24 36 28 58 16
60 60 30 36 32 48 20 66 32 44 24
70 70 24 72 36 40 36 60 24 78 32
80 54 40 82 24 64 42 56 40 88 24
90 72 44 60 46 72 32 96 42 60 40
Xnumbers Tutorial
126
Integer relation
A set of real numbers [x1, x2, ... xn ] , is said to possess an integer relation if there exist a set of
integers [a1, a2, ...an ] , not all zero, such that
a1 x1 + a2 x2 ... + an xn = 0
The integer relation algorithm can be used to solve subset sum problems, as well as to
determine if a given numerical constant is equal to a root of a univariate polynomial with integer
coefficients; in this case we say the constant to be "rational" suche as "12/51" or "algebraic",
such as 2 . On the contrary, if such polynomial does not exist, we say the constant
"transcendental", such as π.
Therefore this algorithm can be used for algebraic-transcendental screening of real numbers.
Because we always have an approximated value of the constant, the found integer relation will
not exactly zero but very close to it only if we use a sufficient number of digits.
For example, we find the following 5th
degree relation
[a1, a2, a3, a4, a5 ]⋅[1, x, x2
, x3
, x4
]T
= a1+ a2 x + a3 x2
+ a4 x3
+ a5 x4
≅ 0
where [a1, a2, a3, a4, a5 ] = [-1, 0, -2, 0, 1] , and x = 1.553773974...
Of course, a numerical discovery of a relation by this algorithm does not in general constitute a
proof of this relation; one of the reasons being that the computer operates in finite precision. In
many cases, however, the relations we first discovered numerically subsequently received
rigorous mathematical proofs. Moreover, many complicated relations probably would never,
have been dreamed of without the assistance of the computer.
Algorithms for finding integer relations include HJLS algorithm, LLL algorithm, PSLQ algorithm.
All these algorithms requires high-precision arithmetic, but the PSLQ, developed by Ferguson
and Forcade (1979) and improved by Ferguson and Bailey (1992), is proved the most efficient
and stable integer relation algorithm. The Xnumbers VB macro has been developed from the
PSLQ descripted by Bailey, D. and Plouffe at http://www.cecm.sfu.ca/organics/papers/bailey
Norm and Bounding.
The norm of the relation is defined the Euclidean norm of the vector |x| = [x1, x2, ... xn ]
Among the relations satisfinyg the constraint | ∑ xi ai | < ε , we are clearly interested in those
relations having small norm, and, if possible, with the smallest nth
degree.
The constraint of small norm has been introduced for filtering between relations.
In fact, if we can easily observe that, if the vector x becames exagerately large, then an integer
relation will be surely find for any real number.
| 314159265358979323 −100000000000000000 π | < 10-16
,
Of course, the norm of this relation is very high (about 3.2 1017
) and thus we have to reject it
PSLQ computes the norm of any examined relation and returns the bound of the coefficients.
Even if a relation is not found, the resulting bound means that cannot possibly be the root of a
polynomial of degree n, with coefficients of size less than the established bound. Even negative
results of this sort are often of interest.
Examples
given a real value x we build the vector [1, x, x2
, x3
, ... xn
] with n in increasing order n = 3, 4,
5...., we are searching for possible integer relations having small error ε < 1E-14 and small
norm |x|
Epprox. value: 3.189207115002720, Exact value: 224
+
Relation: [14, -32, 24, -8, 1], Polynomial:
432
8243214 xxxx +−+−
Epprox. value: 2.67413461226797, Exact value: 223
+
Relation: [4, 24, -12, 4, 6, 0, -1] , Polynomial:
6432
6412244 xxxxx −+−−+
Xnumbers Tutorial
127
Epprox. value: 2.991971857463750, Exact value: 323
+
Relation: [-23, -36, 27, -4, -9, 0, 1], Polynomial:
6432
94273623 xxxxx +−−+−−
Epprox. value: 0.809016994374947, Exact value: 2/)15( +
Relation: [1, 2, -4] , Polynomial:
2
421 xx −+
Epprox. value: 0.923879532511287, Exact value: 4/)22( +
Relation: [-1, 0, 8, 0, -8], Polynomial:
42
881 xx −+−
Epprox. value: 0.900968867902419, Exact value: )7/cos(π
Relation: [1, -4, -4, 8], Polynomial:
32
8441 xxx +−−
Epprox. value: 1.839286755214160, Exact value:
Relation: [1, 1, 1, -1], Polynomial:
32
1 xxx −++
Epprox. value: 0.012345678901235, Exact value: 1/81
Relation: [1, -162, 6561], Polynomial:
2
65611621 xx +− =
2
)811( x−
A very simple trick for testing a relation consists in increasing the degree to n+1; if the algorithm
still returns the same relation then, with high probabability, the relation is correct. But pay
attention, because, sometime a numeric value can have two different relations with different
degree. This means that the given value is the root of two different polynomials. Let's see the
following example
Epprox. value: 7.7099759466767, Exact value: 653
+
Relation: [221, -108, 18, -1], Polynomial:
32
18108221 xxx −+−
But the same value also returns the following relation of degree 5
Relation: [221, 113, -90, 17, -1], Polynomial:
432
1790113221 xxxx −+−+
We can verify that both relations are correct, in this case.
Higher degree relations
All the previous examples can be found using PSLQ in standard 64 bit precision. However,
when we search for higher the Polynomial degree ( n > 6), the multiprecision is required
Epprox. value: 3.00161461434129452355339436348, Exact value: 243
+
Relation: [ -8, 48, -20, 56, -6, 8, 5, 0, -1], Polynomial: -1+104x2
-18x4
+8x6
-x8
Time: 0.4 sec
We have seen that for 8th
degree polynomial we have needed about 30 digits, in this case.
Generally speaking, the required precision increases with the degree and the norm of the
coefficients of the polynomial, as reported in the following table
3
1
9
33
27
19
9
33
27
19 33 +++−
Xnumbers Tutorial
128
Degree Digits Time (sec)
10 40 0.5
15 80 4.5
16 85 5.4
20 120 23.0
25 170 81.7
28 220 126.1
30 250 217.7
Absolute limits. The table can be accessed in two ways: given a polynomial degree we get
the minimum precision of the number (number of significant digits); on the contrary, given a
precision of a number (digits) we get the maximum obtainable polynomial degree.
For example, if we are searching for a polynomial of 15 degree, we must have a number with
80 digits, at least. If we have have calculate a number with 100 digits we can search for
polynomials having degree n < 18.
In the above table and graph we have also reported the time1
in second of the PSLQ (two-level)
algorithm. As we can see, the time increases sharply with the degree. The time stays less the
few seconds for n <16, but requires about 3-4 minutes for n = 30 and 250 digits (the maximum
possible for Xnumbers)
Some other high results are shownin the following list. In these trials, the problem is to recover
the polynomial of degree n satisfied by α, where α = a1/r
± b1/s
. In
other words, the input vector x = (1, α, α2
, α2
.... αn-1
), where n = (r s + 1). Here the working
precision was set to the level required by PSLQ and the input number α was computed with the
needed precision, as determined by the above table.
Degree: 12. Exact value: α = 31/ 6
+ 21/ 2
≅ 2.61515051... (50 digits)
Relation: [ -25, 0, 552, 0, -60, 0, 166, 0, -60, 0, 12, 0, -1] ,
Time: 2.8 sec
Degree: 12. Exact value: α = 31/ 4
+ 21/ 3
≅ 2.57599506... (50 digits)
Relation: [ -11, -216, -360, -32, 27, -288, 24, 0, -9, -8, 0, 0, 1],
Time: 3 sec
Degree: 15. Exact value: α = 31/ 5
+ 21/ 3
≅ 2.50565198951... (80 digits)
Relation:, [ -59, -360, -540, 80, -1620, 27, -80, -540, 0, 40, -9, 0, -10, 0, 0, 1]
Time: 5 sec
Degree: 16. Exact value: α = 31/ 4
− 21/ 4
≅ 0.126866897949771... (85 digits)
Relation: [ 1, 0, 0, 0, -3860, 0, 0, 0, -666, 0, 0, 0, -20, 0, 0, 0, 1],
Time: 5.6 sec
Degree: 20. Exact value: α = 31/ 5
− 21/ 4
≅ 0.0565238246127... (120 digits)
Relation: [ 49, -1080, 3960, -3360, 80, -108, -6120, -7440, -80, 0, 54, -1560, 40, 0, 0, -12, -10,
0, 0, 0, 1],
Time: 16.8 sec
Degree: 20. Exact value: α = 31/ 5
+ 21/ 4
≅ 2.434938054618... (120 digits)
Relation: [ 49, -1080, 3960, -3360, 80, -108, -6120, -7440, -80, 0, 54, -1560, 40, 0, 0, -12, -10,
0, 0, 0, 1],
Time: 23 sec
1
Computation on a PC with AMD Athlon, 2GHz, 512 MB Ram
Xnumbers Tutorial
129
It is interesting to note that the two numbers 31/ 5
± 21/ 4
gives the same relations. In fact they
are both roots of the same 20th
degree polynomial.
Degree: 25. Exact value: α = 31/ 5
+ 21/ 5
≅ 2.39442929461255233... (170 digits)
Relation: [-3125, 0, 0, 0, 0, 21875, 0, 0, 0, 0, -57500, 0, 0, 0, 0, -3500, 0, 0, 0, 0, -25, 0, 0, 0, 0,
1],
Time: 81.7 sec
Degree: 25. Exact value: α = 31/ 5
− 21/ 5
≅ 0.0970325846184823... (170 digits)
Relation: [ 1, 0, 0, 0, 0, -116255, 0, 0, 0, 0, -11240, 0, 0, 0, 0, -3760, 0, 0, 0, 0, 5, 0, 0, 0, 0, -1],
Time: 68.7 sec
Degree: 28. Exact value: α = 31/ 7
− 21/ 4
≅ -0.01927630224403418... (220 digits)
Relation: [ -47, -2688, -13104, -7560, 448, -79296, 136584, -108, -672, -184128, -25956, 0,
560, -74592, 54, 0, -280, -4872, 0, 0, 84, -12, 0, 0, -14, 0, 0, 0, 1],
Time: 126 sec
Degree: 30. Exact value: α = 31/ 6
− 21/ 5
≅ 0.0522386001789677198... (250 digits)
Relation: [ 179, -4860, 30780, -65520, 30240, -192, -405, -98820, -538380, -149520, -240, 0,
270, -84240, 90360, -160, 0, 0, -90, -7380, -60, 0, 0, 0, 15, -12, 0, 0, 0, 0, -1],
Time: 218 sec
Xnumbers Tutorial
130
Macro Integer Relation Finder
The macro Integer Relation Finder, from the menu "Numbers", searches for two type of integer
relations:
Polynomial relation
Input: a real number x, and the n-th degree
n
n xaxaxaxaa ...3
3
2
210 ++++
General relation
Input: a vector x of n-th dimension
nn xaxaxaxa ...332211 +++
Using this macro is very simple: select the cell containing the number x or the range of the
column-vector x and start the macro. The macro assumes a simple layout in order to speed-up
the input operation. The input layout is different for the two type of relation
Polynomial relation input layout
The cell B2 contains the multiprecision number x
of 30 digits and just below, the cell B3 contains
the degree n. Select the cells B2 and start the
macro
Multiple searching for one x and several degree The cell B2 contains the multiprecision number x
of 30 digits and just the row below, the range
B3:D3 contains the degree n = 5, 6, 7. Select the
cells B2 and start the macro. For the same
number x, the macro will search for integer
polynomial with degree 5, 6 and 7 respectively
Multiple searching for several x and several degree The range B2:B4 contains the multiprecision
numbers x1, x2 and x3 and just the row
below, the range B5:E5, contains the degree
n = 5, 6, 7 an 8. Select the range B2:B4 and
start the macro. For the each number x, the
macro will search for 4 integer polynomial with
degree 5, 6, 7 and 8, respectively
General relation input layout
The range B2:B6 contains the vector x .
Select the range B2:B6 and start the macro.
The dimension of he relation will be 5, just the same of the
vector dimension
Multiple searching for several vectors is possible. Simple select as many adjacent vectors as
we need. Note that each vector can have different dimension. In this case, select the
rectangular range containing all the vectors.
Xnumbers Tutorial
131
Output results
The macro writes the result in the worksheet are beside the input data using a simple standard
layout almost similar for polynomial and general relation.
Let' s see with an example
Example 1. We want to search for a smallest integer polynomial having the approximate root:
x ≅ 2.99197185746375045829465694878... (30 digits)
We restrict our searching to the polynomials from 3 to 8 degree
Insert the input data x and the degree
3, 4, 5, 6 and 7 as in the sheet at the
left. Select the cell B1 and start the
macro
The input fields are correctly filled.
Press "start". In few seconds the
macro finds the polynomials adding
same useful information. Fortunately
it have found 2 "good" polynomials,
in this case
The coefficents of the polynomials are listed in the last section of the report, but just above, the
macro write the information returned by the PSLQ routine
Digits: the precision of the calculus
Degree: degree of the polynomial; degree +1 is the dimension of the relation
RC: return code: 0 =OK, 1 =precision exhausted, 2 = iteration overflow, 3= overflow error
This code states the "goodness" of the relation found
Residue: relative residue of the relation, computed as ε / a , where
∑= ii xaε , |)max(| ii xaa =
Bound: this number M states that there can exist no relation vector whose Euclidean norm is
less than M. This is a very important result provided by PSLQ algorithm. Together with RC
helps us to analyze the goodness of the relation found.
Iter: number of iterations needed for reaching the result
Time: elaboration time in seconds
Example 2. We have 3 real numbers x1, x2, x3 computed with 15 precision digits.
x1 = 35.812551166675 , x2 = 8.554440155849 , x3 = 4.186428394404
We want to investigate if they could be express by the following simbolic formula






+++=
e
aa
eaa
a
x 54
32
1
1
π
π ⇔ 054
321 =++++−
e
aa
eaaxa
π
π
where [a1, a2, a3, a4, a5] all integer.
For finding this integer relation of dimension n = 5 (that it is compatible with the precision of the
numbers that we have) we compute the following vectors [ ]11
,,,, −−
eex ππ changing the x
value with x1, x2, x3 respectively
Xnumbers Tutorial
132
Arrange the 3 vectors as in the
sheet at the left. Select the range
B2:D6 and start the macro.
The input fields are correctly filled
and the option button "General" is
selected. Press "start".
In very few seconds the macro succesfully finds integer relations for the first two numbers,
while, on the contrary, the third numbers has not a small simple relation. Note how large are
the coefficents of the third column. Note also that its bound M = 3.4E+9 its very high; it assures
that no integer relation exists with norm ||a|| < 3.4E+9
Xnumbers Tutorial
133
Linear Algebra Functions
Matrix Addition
xMatAdd(mat1, mat2, [DgtMax])
Performs the addition of two matrices. mat1 and mat2 are (n x m) arrays






+





=





mnn
n
mnn
n
mnn
n
bb
bb
aa
aa
cc
cc
1
111
1
111
1
111
..
..
..
..
..
..
Matrix Subtraction
xMatSub(mat1, mat2, [DgtMax])
Performs the subtraction of two matrices. mat1 and mat2 are (n x m) arrays






−





=





mnn
n
mnn
n
mnn
n
bb
bb
aa
aa
cc
cc
1
111
1
111
1
111
..
..
..
..
..
..
Matrix Multiplication
xMatMult(mat1, mat2, [DgtMax])
Performs the multiplication of two matrices. mat1 (n x p) and mat2 (p x m) are arrays










⋅





=





mpp
m
m
pnnn
p
mnn
n
aa
aa
aa
aaa
aaa
cc
cc
1
212
111
21
11211
1
111
..
..
..
..
..
..
..
Matrix Inverse
xMatInv(A, [DgtMax])
Returns the inverse of a (n x n) square matrix
It returns "?" for singular matrix.
This function uses the Gauss-Jordan diagonalization algorithm with partial pivoting method.
Matrix Determinant
xMatDet(A, [DgtMax])
Returns the determinant of a square matrix.
It returns "?" for singular matrix.
Xnumbers Tutorial
134
Matrix Modulus
xMatAbs(A, [DgtMax])
Returns the absolute value of a matrix or vector.
It is also known as "modulus" or "norm"
Parameters A may be an (n x m) array or a vector
2
1 1
, )(∑∑
= =
=
n
i
m
j
jiaA
Scalar Product
xProdScal( v1, v2, [DgtMax])
Returns the scalar product of two vectors
i2
1
i121 VVVVc
n
i
⋅=•= ∑
=
Note: The scalar product is zero if, and only if, the vectors are perpendicular
0 2121 VVVV ⊥⇔=•
Similarity Transform
= xMatBAB(A, B, [DgtMax])
Returns the matrix product:
BABC 1−
=
This operation is also called the "Similarity Transform" of the matrix A by the matrix B. This
operation plays a crucial role in the computation of eigenvalues, because it leaves the
eigenvalues of the matrix A unchanged. For real, symmetrical matrices, B is orthogonal. The
Similarity Transform is also called the "orthogonal transform".
A and B must be square matrices.
Matrix Power
= xMatPow(A, n, [DgtMax])
Returns the integer power of a square matrix.
48476 time
...
n
n
AAAAAB ⋅⋅==
Example
Xnumbers Tutorial
135
Matrix LU decomposition
= xMatLU(A, [Pivot], [DgtMax])
Returns the LU decomposition of a square matrix A
It uses Crout's algorithm










⋅










=⋅=
33
2322
131211
2121
21
00
0
1
01
001
u
uu
uuu
ll
lULA
Where L is a lower triangular matrix, and U is an upper triangular matrix
The parameter Pivot (default=TRUE) activates the partial pivoting.
Note: if partial pivot is activated, the LU decomposition refers to a permutation of A
If the square matrix has dimensions (n x n), this function returns an (n x 3n) array where the
first n columns are the matrix L, the next n columns are the matrix U, and the last n columns
are the matrix P.
Globally, the output of the Mat_LU function will be:
- Columns (1, n) = Matrix L
- Columns (n+1, 2n) = Matrix U
- Columns (2n+1, 3n) = Matrix P
When pivoting is activated the right decomposition formula is: A = P L U , where P is a
permutation matrix
Note: LU decomposition does not work if the first element of the diagonal of A is zero
Example: find the factorization of the following 3x3 matrix A
Note: if you want to get only the L and U matrices select a range (3 x 6) before entering this
function
Matrix LLT
decomposition
= xMatLL(A, [DgtMax])
This function returns the LLT
decomposition of a square matrix A
It uses Cholesky's algorithm
T
T
lll
ll
l
lll
ll
l
LLA










⋅










=⋅=
333231
2221
11
333231
2221
11
0
00
0
00
Where L is a lower triangular matrix
The function returns an (n x n) array
Note: Cholesky decomposition works only for positive definite matrix
Xnumbers Tutorial
136
Example.
The diagonal elements of the L matrix are all positive. So the matrix A is positive definite and
the decomposition is correct. This function simply stops when detects a negative diagonal
element, returning the incomplete decomposition.
See this example
<Attention !>
A diagonal element of the L matrix is
negative. So the matrix is not positive
definite and the decomposition cannot be
completed
Vector Product
= xProdVect(v1, v2, [DgtMax])
Returns the vector product of two vectors










−
−
−
=










×










=×
12212211
32113112
31223221
32
22
12
31
21
11
21
vvvv
vvvv
vvvv
v
v
v
v
v
v
VV
Note that if V1 and V2 are parallels, the vector product is the null vector.
Solve Linear Equation System
xSYSLIN( A, B, [DgtMax])
Solves a linear system in multiprecision.
The input parameter A is an (n x n) array, B may be a vector (n x 1) or an (n x m) array.
It returns a vector (n x 1) or an (n x m) array depending by the argument B
A set of m linear systems in n unknowns looks like this:
][...,][,][ 12111 mbxAbxAbxA =⋅=⋅=⋅
It can be rewritten as:








=










⋅








⇒=⋅
mnn
m
mnn
m
nnn
n
bb
bb
xx
xx
aa
aa
1
111
1
111
1
111
..
....
..
...
..
.......
..
..
....
..
...][][][ BxA
This function uses the Gauss-Jordan diagonalization algorithm with partial pivoting method.
Xnumbers Tutorial
137
Example. Find the solution of the following 7x7 linear system
A b
462 792 1287 2002 3003 4368 12376 24290
924 1716 3003 5005 8008 12376 31824 62856
1716 3432 6435 11440 19448 31824 75582 149877
3003 6435 12870 24310 43758 75582 167960 333918
5005 11440 24310 48620 92378 167960 352716 702429
8008 19448 43758 92378 184756 352716 705432 1406496
12376 31824 75582 167960 352716 705432 1352078 2697968
The solution is the vector [1, 1, 1, 1, 1, 1, 1]. Solving with standard arithmetic, we have an
average accuracy of about 1E-8, while in multiprecision we have an accuracy better than 1E-28
Square Delta Extrapolation
ExtDelta2(x)
xExtDelta2(x, [DgtMax])
This function returns the Aitken's extrapolation, also known as "Square Delta Extrapolation".
The parameter x is a vector of n value (n ≥ 3), in vertical consecutive cells. (n = 3 for the multi-
precision function xExtDelta2).
This formula can be applied to any generic sequence of values (vector with n>2 ) for
accelerating the convergence.
),...,,(),...,,( 2321
2
321 −
∆
→ nn vvvvxxxx
Note that this algorithm produces a vector with n-2 values. If n = 3, the result is a single value.
Taking the difference iii xx −=∆ +1 , the Aitken's extrapolation formula is:
( )
( )21
2
1
21
2
1
2 −−
−
−−
−
+−
−
−=
∆−∆
∆
−=
iii
ii
i
ii
i
ii
xxx
xx
xxv
This formula can be applied to the second sequence to obtain a new sequence with n-4 values,
and so on. The process stops when the last sequence has less than 3 values.
Example. we want to find the numeric solution of the equation x = cos(x)
We choose the central point method. Starting from x0 = 0 we build the iterations
xn+1 = cos(xn)
Xnumbers Tutorial
138
As we can see in the following table, the convergence is evident but very slow (after 12
iterations the precision is about 3E-5) .
The functions of this worksheet are:
The cell B2 contains the starting value x0
The cell B3 , C2, D3 contain the formulas
[B3]=C2
[C2]=COS(B2)
[D3]=ASS(B2-C2)
Selecting the range B3:D3 and dragging
down we can easily perform the iterative
process as we like.
We observe that the convergence is
evident but quite slow.
As we can see the last 12th
value has an
error of about 3.5E-3. But if we perform
the delta extrapolation of the three last
values we get a new value having an
accuracy better than 1E-5.
Now let's repeat the iterative process using systematically the square delta extrapolation
In this process, we have systematically
repeated the ∆2
extrapolation every 3
iterations.
We have inserted in the cell B5
=ExtDelta2(B2:B4)
In the cell B8
=ExtDelta2(B3:B7)
In the cell B14
=ExtDelta2(B9:B11)
The acceleration is superb!. After only 12 steps, the precision is better than 1E-15.
The graph below shows better than many words the acceleration effect
1E-16
1E-14
1E-12
1E-10
1E-08
1E-06
0.0001
0.01
1
0 2 4 6 8 10 12
Error (extrap)
Error
The Aitken's extrapolation formula works very well with the Gauss-Seidel iterative method, and
for accelerating the convergence of many iterative processes.
Xnumbers Tutorial
139
Macro for Multiprecision Matrix Operations
This application collects a set of useful macros performing multiprecision matrix operations
Determinant )det(A Gauss-Jordan algorithm
Addition BA +
Subtraction BA −
Multiplication BA⋅
Scalar multiplication Ak ⋅
Inverse 1−
A Gauss-Jordan algorithm
Similarity transform BAB 1−
Linear System BAX = Gauss-Jordan algorithm
Linear System overdetermined. bAx = rows > columns
LU decomposition LUA = Crout’s algorithm
Cholesky decomposition T
LLA = Cholesky’s algorithm
Norm A
Scalar product BAT
⋅
SVD T
VU ⋅Σ⋅ Golub-Reinsch algorithm
The use of this macro is quite simple. If the operation requires only one matrix (determinant,
inversion, etc.) select the matrix, start the macro and choose the appropriate operation.
Other operations (addition, multiplication, etc.) require two matrices. In that case you have also
to select the second matrix in the second input-box.
The internal calculus is performed in multiprecision. The result is converted in standard
precision (15 significant digits max) for more readability, but, if you like, you may also leave it in
full multiprecision format.
Example: Solve the following linear system Ax = b.
Select the matrix A and start the macro
Choose the operation “Linear System” and then move into the right field to select the vector b.
Xnumbers Tutorial
140
Indicate, if necessary, the upper-left
cell of the range where you want to
write the result.
Then, press OK. The result will be output starting from the output cell I2.
Smart
Selector
The special button near the input field is useful for selecting large
matrices .Select the first cell, or an internal cell of the matrix and then
press this button. The entire matrix will be selected
Elaboration time
Multiprecision computation does slow down the computation considerably. It takes much more
time then the standard double precision. The time depends on the matrix dimension and on
the precision digits. The following graphs show the average time for the inversion and for the
multiplication of two dense matrices.
0
20
40
60
80
100
120
140
160
0 25 50 75 100
invers.
multip.
Time (sec)
N x N
30 digits
Multiprecision
0
1
2
3
4
5
6
0 50 100 150 200
digits
time (sec)
20 x 20
standard precision
As we can see, the inversion of a (100 x 100) dense matrix, with 30 precision digits, takes
about 150 seconds. Clearly, for this kind of tasks, macros are more suitable than functions.
Xnumbers Tutorial
141
Integrals & Series
Discrete Fourier Transform
=DFT(samples, [central])
=FFT(samples, [central])
Returns the complex matrix of the DFT transform of N samples.
This function returns an (N x 2) array. The first column contains the real part; the second
column the complex part .
The optional parameter "central" (default False) outputs a central transform.
If N is an integer power of 2, that is N = 2P
, use the fastest FFT.
FFT uses the Cooley and Tukey decimation-in-time algorithm.
Formulas
Given N samples ( f(0), f(1), f(2),....f(N-1) ) of a periodic function f(t) with a normalized
sampling rate (T=1), the DFT is:
( ) ( )[ ]∑
−
=
⋅−⋅=⋅+=
1
0
/2/2cos)()()()(
N
n
ir NnksiniNnknfkFikFkF ππ
The components (Fr , Fi) are called the harmonic spectrum of f(t)
By the Fourier series, we can approximate a periodic function f(t):
( ) ( )tksinbtkaatf k
K
k
k ⋅+⋅+≅ ∑
−
=
ωωcos)(
1
1
0
where the coefficients (ak , bk ) are the components 2Fr and 2Fi of DFT
Example: Find the 16-FFT of the following periodic function (T = 1 sec)
)3cos(5.0)cos(3)( tttf ωω ++= where
T
π
ω
2
=
First of all we have to sample the given function. Setting N = 16 , we have a sampling period of
1,...1,0,
16
1
−=∆⋅=⇒==∆ Nitit
N
T
t i
)3cos(5.0)cos(3 iii ttf ωω ++=
Appling the FFT function at the samples set ( f0, f1, f2,....f15 ), we get the complex discrete
Fourier’s transform
Xnumbers Tutorial
142
Note that the FFT returns a (16 x2)
matrix. The first column contains the real
part of FFT while the second column the
imaginary one.
The magnitude and phase can be easily
obtained with the following formulas
( ) ( )22
imrei FFTFFTA +=






=
re
im
i
FFT
FFT
arctanθ
Observe that the first row of the FFT
contains the mean of f(t).
Note also that the rows from 10 to 16 are
the mirror-copy of the previous rows.
Rearranging the mirror part we can obtain the central Fourier's spectrum.
For example, below there are two representations of the same DFT, obtained setting the
"central" parameter "false" and "true", respectively.
Bilateral DFT Central DFT
Amp
0
0.05
0.1
0.15
0.2
1 4 7 10 13 16 19 22 25 28 31
Amp
0
0.05
0.1
0.15
0.2
1 4 7 10 13 16 19 22 25 28 31
Xnumbers Tutorial
143
Discrete Fourier Inverse Transform
=DFT_INV(samples, [central])
=FFT_INV(samples, [central])
Returns the inverse of the DFT transform of N complex samples.
This function returns an (N x 2) array containing the samples of the function f(t).
The optional parameter "central" (default False) outputs a central transform.
If N is an integer power of 2, thus N=2P
, use the fastest FFT_INV function
FFT_INV uses the Cooley and Tukey decimation-in-time algorithm.
Formulas
[ ] ( ) ( )[ ]∑
−
=
⋅+⋅⋅+=
1
0
/2/2cos)()()(
N
k
ir NnksiniNnkkFikFnf ππ
Where the components (Fr , Fi) are the harmonic spectrum of f(t)
Example: Find the inverse transform of the FFT computed in the previous example
As we can see, the first column of FFT_INV returns the samples of f(t) that have originated the
FFT itself
Xnumbers Tutorial
144
Discrete Fourier Spectrum
=DFSP(samples, [dB], [Angle])
This function returns the harmonic spectrum of a samples set
The parameter "samples" is a vector of N equidistant samples
The optional parameter "dB" (default FALSE) sets the amplitude conversion in decibel
The optional parameter "Angle" (default “RAD”) sets the angle unit (RAD, GRAD, DEG)
The function returns an (N x 2) array, containing the amplitude and phase.
The spectrum (An, θn) is computed for real positive frequencys, being
( )∑ ++≅ nn tnAftf θωsin)0()(
Example: Find the harmonic spectrum of the following 32 samples
Amplitude
0
0.2
0.4
0.6
0.8
1
1.2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Phase
-100
-50
0
50
100
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Inverse Discrete Fourier Spectrum
=DFSP_INV(spectrum, [dB], [Angle])
This function builds the temporal sequence from its real spectrum (amplitude, phase)
( ) )(, inn tfA ⇒θ
The parameter "spectrum" is an (M x 2) array. Each row contains a harmonic. The first column
contains the amplitude and the second column the phase
The optional parameter "dB" (default FALSE) sets the output in decibel
The optional parameter "Angle" (default “RAD”) sets the angle unit (RAD, GRAD, DEG)
The function returns the vector (N x 1) where N = 2M
Xnumbers Tutorial
145
2D Discrete Fourier Transform
=FFT2D (samples)
This function performs the 2D-FFT of a bidimensional data set (xi, yi).
The parameter "samples" is an (N x M) array where N and M are integer powers of 2 (4, 8, 16,
32, 64…). The function returns an (2N x M) array. The first N rows contain the real part, the last
N rows contain the imaginary part.
Note: This function requires a large amount of space and effort. Usually it can works with
matrices up to (64 x 64).
Example: Analyze the harmonic component of the following (8 x 8) table
The 2D-FFT can be
computed in a very straight
way. Simply select a 16 x 8
array and insert the FFT2D
where the input parameter is
the given matrix (range
B10:I17).
We can easily extract the
harmonic components:
1)0,0( =H
05.0)0,1( =H
1.0)1,0( =H
jH 354.0354.0)1,1( +=
jH 25.0)2,2( =
If we compute the inverse transform DFT2D_INV(“L10:S25”) we will obtain again the given
starting matrix.
2D Inverse Discrete Fourier Transform
=FFT2D_INV (samples)
This function FFT2D_INV performs the inverse of the FFT2D. It accepts in input an (2N x M)
array having the real part in the first N rows and the imaginary part in the last N rows. It returns
an (N x N) array
Xnumbers Tutorial
146
Macro DFT (Discrete Fourier Transform)
This macro performs:
• the DFT of a data set of N samples
• the DFT-Inverse of a data set of N complex samples
• the 2D-DFT of a matrix of N x M samples
• the 2D-DFT-Inverse of a two matrices of N x M samples
DFT
It works for any number N.
If N is a powers of 2 (8, 16, 32, 64, etc.) the macro uses the faster FFT algorithm and the
elaboration is more efficient.
Its use is quite simple. Select the vector of samples and then start the macro
The column “x” is not strictly necessary. If present, the macro uses it to calculate the sampling
parameters (see the top-right box).
For long input vector, you can select only the first cell C9 and start the macro. The entire input
data set (32 x 1) will be automatically selected.
The macro writes the results in the following way
f = frequency sample
Hre = Real part of DFT transform
Him = Imaginary part of DFT transform
Amp = Amplitude (if “polar” is checked)
Phase = Phase (if “polar” is checked)
The amplituted can be converted in dB. That is: Amp dB = 20 Log(Amp)),
or also in dBm, the power ratio relative to 1 mW (a sine wave of 1 Vpeak , with 50 ohms load, is
equivalent 10 dBm).
The option check box "central" specifies if the DFT output is bi-lateral or central.
See the following examples
Xnumbers Tutorial
147
Example of a Fourier
transform of pulse.
Period = 1 sec
Duty cicle = 5.5%
Amplitude = 1
Sampling time = 1/256
f(t)
0
0.2
0.4
0.6
0.8
1
1.2
-0.5 -0.3 -0.1 0.1 0.3 0.5
Amplitude of the central
DFT
(-100 < f < 100)
Amp(f)
0
0.01
0.02
0.03
0.04
0.05
0.06
-100 -50 0 50 100
Amplitude of the bi-lateral
DFT
(0 < f < 255)
Amp
0
0.01
0.02
0.03
0.04
0.05
0.06
0 50 100 150 200 250
Amplitude in dB of the
central DFT
(-100 < f < 100)
Amp (dB)
-60
-55
-50
-45
-40
-35
-30
-25
-100 -50 0 50 100
Xnumbers Tutorial
148
Operation DFT-inverse
In this case you have to select two columns: the real and imaginary part of the DFT (Hre, Him).
Then start the macro as usually.
If the DFT is in polar form (Amplitude, Phase), you have to check the “polar” option box and
choose consequently the appropriate units: dB and angle
Operation 2D-DFT
Dimension N and M must be integer power of 2
In this case you have to select a matrix of N x M values (do not select the axes-scales) and
start the macro as usually.
If you want the DFT in polar form (Amplitude, Phase), you have to check the “polar” option and
choose consequently the appropriate units: dB and angle
The macro generates two matrices containing
the real and imaginary parts of the 2D-DFT
Operation 2D-DFT inverse
In this case you have to select a matrix of 2N x M values (do not select the axes values)
containing both real and imaginary part and start the macro as usually.
If the DFT is in polar form (Amplitude, Phase), you have to check the “polar” option and choose
consequently the appropriate units: dB and angle
Xnumbers Tutorial
149
Macro Sampler
This is a simple but very useful macro for function sampling
It can generate samples of multivariate functions such as:
f (x) , f (x1, x2) .... f (x1, ... xm)
The samples can be arranged in a list or in a table (only for 2 variables).
Examples of lists and tables generated by this macro are shown in the following sheet
The tables at the top are the skeletons to generate the samples-list or the samples-table just
below. The skeleton contains the following parameters for the sampler.
Start starting point of the variable X0
Samples number of samples to generate: N
Period length of the sampling: P
Step length between two consecutive point H = X1 – X0
Cyclic True or False (default), specifies if the function is periodic with period P.
The difference between a cyclic or no-cyclic function is in the step formula
S = P / N for cyclic function
S = P / (N-1) for no-cyclic function
For example, the sampling with N = 5, X0 = 0 and P = 2, needs a step H = 0.5
The first point and the last point, in this case, are always taken.
But, for a periodic function, the same sampling needs a step of H = 0.4
Practically, the last point X = 2, in this case, is discharged, because is f(0) = f(2). Usually
periodic functions require to set Cyclic = “True” for the FT analysis
The skeleton can be drawn by hand or
automatically. In this case you have only to give
the number of variables that you want.
The check-box “Function seed” tells the macro
to created also the cell in which you can insert
the function to sample
Xnumbers Tutorial
150
A simple skeleton for one variable is:
In the cell C2 you must insert the function
f(x) to sample. The reference for the
independent variable x is the cell B2. For
example, if the function is y = x +2x2
You have to insert the formula
= B2+2*B2^2 in the cell C2
Parameters N (Samples), P (Period), H (Step) are not all independent. Only two parameters
can be freely chosen.
The macro chooses the first two parameters found from top to bottom
The remain parameter is obtained by the step-formula
Synthetically you can have one of the following three cases
Given parameters Calculated parameter
Samples, Period (N, P) Step (H)
Samples, Step (N, H) Period (P)
Period, Step (P, H) Samples (N)
Look at the following three examples below for better explanation. The given parameters are in
blue while the calculated parameter is in red.
After you have set and filled the skeleton, select it and start the sampler macro again
(remember that range must always have 6 rows, including the header)
The macro show the following window
The check-box “Add formula” tells to the macro to leave the formula in the sample set,
otherwise it contains only the values. Formula can be added only for a monovariable list or for a
table.
Xnumbers Tutorial
151
Integral function
=IntegrData(xi, yi, [IntType])
Computes numerically the integral functions F(xi) of a given dataset (xi, yi)
∫=
ix
ii dxyxF
0
)(
The sampling interval must be constant
The IntType parameter (default = 2) sets the integration formulas 1, 2, 3
Case 1 - 2 points integration formula of 1st degree
symmetric 2/)( 2112 ffhI += 12/)2(3
fhE ≈
Case 2 - 4 points integration formulas of 3rd degree
symmetric 24/)1313( 432123 ffffhI −++−= )4(5
720/11 fhE ⋅≈
Left side 24/)5199( 432112 ffffhI +−+=
Right side 24/)9195( 432134 ffffhI ++−=
Case 3 - 6 points integration formulas of 5th degree
symmetric 1440/)1193)(8029311( 65432134 ffffffhI +−++−= )6(7
60480/191 fhE ⋅≈
Left side
1440/)271734827981427475( 65432112 ffffffhI +−+−+=
1440/)1177258102263727( 65432123 ffffffhI −+−++−=
Right side
1440/)2763710222587711( 65432145 ffffffhI −++−+−=
1440/)475142779848217327( 65432156 ffffffhI ++−+−=
Depending on the case, the algorithm uses these formulas at the beginning, at the center and
the end of the integration interval.
2 points formula
f1 f2
x
h
4 points formulas
f1 f2 f3 f4
x
h
f1 f2 f3 f4
x
h
f1 f2 f3 f4
h
Xnumbers Tutorial
152
6 points formulas
f1 f2 f3 f4
x
h
f5 f6 f1 f2 f3 f4
x
h
f5 f6
x
f1 f2 f3 f4
h
f5 f6
f1 f2 f3 f4
x
h
f5 f6
f1 f2 f3 f4
x
h
f5 f6
Example. Compute the integral function of the following tabulated function y(xi)
Using the integration formulas 1, 2 and
3, we have, of course, different precision.
The following graph shows the error
behaviour of the three schemas.
The average errors are about 0.05 for
the 1st degree formula, 5E-5 for the 3rd
degree and 4E-6 for the 5th degree.
Clearly, for smooth, regular functions,
the highest degree formulas reach the
highest precision but we have to
consider that they are also more
expensive
1.E-06
1.E-05
1.E-04
1.E-03
1.E-02
1.E-01
1.E+00
0 1 2 3 4 5 6
err1
err2
err3
Usually the 3rd
degree formulas are the best compromise between cost and precision
Avoid discontinues. But, generally, do highest integration degree formulas always give the
best precision? Not always. When the function to integrate shows jumps or direction
discontinuities, the better choice is the linear formula. See the following example.
Given the following functions
|2|2/)|4(|)( −−+−= xxxxy
its exact integral function is
[ ] 4/8|2|)2(2|4|)4()()( 2
0
++−−−−−== ∫ xxxxxdxxyxF
x
Xnumbers Tutorial
153
Sampling the function y(x) and F(x) with h = 0.25 for 0 ≤ x ≤ 6, we have the following data
sets (xi, y(xi)) and (xi, F(xi))
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
0 1 2 3 4 5 6
y
∫ y dx
As we can see, the function y(x), always linear, has no derivative at the points x = 2, and x = 4.
If we integrate the data set (xi, yi) with the 1st degree formula (IntType = 1) the result coincides
with the exact solution (error = 0). But if we try with the other higher formulas the average error
is, surprisingly, about 0.005
Integral function of a symbolic formula
When we have the function f(x) written in a symbolic formula we can obtain the plot of its
integral function in two ways.
• Sampling the given function f(xi) for xi = x0 + i⋅h with a suitable step h
• Solving the associated ODE equation
• Integrating f(x) from x0 to xi for xi = x0 + i⋅h with a suitable step h
The first method is simple and it is already shown in the previous chapter; the second way
need some more explanation.
The function y(x) that we want to plot is defined as
∫=
x
x
dxxfxy
0
)()(
Taking the derivatives of both sides and remebering Leibniz's rule, we have
)()(' xfxy = , 0)( 0 =xy
As we can see, the computing of any integral function is equivalent of solving an ODE.
Therefore we can use any method that we have used for solving ODE: Runge-Kutta, Predictor-
Corrector, Taylor, etc. (see chap. ODE for further details)
The third method, less efficient then the others, may be successfully used when the function
f(x) or its derivatives f'(x) has some singularities in the integration range.
The following example explain better the concept. Assume to have to plot the integral function
of the following function
∫=
x
dxxxy
0
2
)ln()(
We note that,. −∞=
→
)ln(lim 2
0
x
x
Therefore, x = 0 is a singular point. From theory we know that the integral exists, thus we can
project to tabulate the given integral for several points xi = i⋅h with a suitable step h, for
example h = 0.1
For this scope we may use the function integr_de or Integr that are more suitable for such
singularities
Xnumbers Tutorial
154
Insert the function definition in cell B1
Ln(x^2)
Build a sequence x = 0, 0.1, 0.guatda.com/cmx.p2...4 in the
range A4:A44
Insert the function integr_de in cell B5 and the
function = LN(A5^2) in cell C5 and drag down
the range B5:C5.
Note that we cannot complete the first cell C4
because of #NUM! error.
Now, plotting the the range A5:C44, we have the following graph
-4
-3
-2
-1
0
1
2
3
4
0 0.5 1 1.5 2 2.5 3 3.5 4
∫f(x)
f(x)
Comparing with the exact
solution
xxxxy 2)ln()( 2
−=
we observe a precision better
then 1E-15.
From the graph we see that the
integral function y(x) has a zero
between 2.5 and 3
From the table we may take a
more precise bracketing of the
zero
2.7 < xz < 2.8
Zeros of integral function
We may obtain a more close solution of the integral equation using the Newton-Raphson
iterative algorithm
0)()(
0
== ∫
x
x
dttfxy
)(
)(
1
n
n
nn
xf
xy
xx −=+
In the cell B1 insert the
definition of the function f(x)
Ln(x^2)
Starting from x0 = 2.7 we
calculate the integral from 0 to
2.7 in the cell C5, The
derivative is the function f(x)
itself, calculated in the cell B5.
The new value x1 is calculated
in the cell A6
Iterating the process, the solution happears in the last cells of the column A
As we can see, after few iteration the zero converges to the exact solution x = e with a
precision better then 1E-15
Xnumbers Tutorial
155
Function Integration (Romberg method)
=Integr_ro(Funct, a, b, [Param], [rank], [ErrMax])
This function computes the numeric integral of a function f(x) by the Romberg method.
∫=
b
a
xfI )(
The parameter "Funct" is a math expression string in the variable x, such as:
"x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. .
Remember the quote " " for passing a string to an Excel function.
"Funct" may be also a cell containing a string formula
"Param" contains label and values for parameters substitution (if there are)
"Rank", from 1 to 16 (default), sets the maximum integration rank.
"ErrMax" (default 1E-15) , sets the maximum relative error.
For further details about writing a math string see Math formula string
The algorithm starts with rank = 1 and incrementing the rank until it detects a stop condition.
|R(p, p) - R(p, p-1)| < 10^-15 absolute error detect
or
(|R(p, p) - R(p, p-1)|) / |R(p, p)| < 10^-15 if |R(p, p)| >> 1 relative error detect
or
rank = 16
Example. Compute the integral of "x*cos(x)" for 0 ≤ x ≤ 0.4
Integr_ro("x*cos(x)", 0, 0.4) = 0.0768283309263453
This function can also display the number of sub-intervals and the estimate error.
To see these values simply select three adjacent cells and give the CTRL+SHIFT+ENTER key
sequence.
This result is reached with rank = 4 , s = 16 sub-intervals, and an estimate error of about
E = 3.75E-16
The function accepts also external parameters. Remember only to include the label in the
parameter selection.
Xnumbers Tutorial
156
Function Integration (Double Exponential method)
= Integr_de(funct, a, b, [Param])
This function1
computes the numeric integral of a function f(x) by the Double Exponential
method. This is specially suitable for improper integrals and infinite, not oscillating integrals.
∫=
b
a
dxxfI )( ∫
+∞
=
a
dxxfI )( ∫
+∞
∞−
= dxxfI )(
The parameter "funct" is a math expression string in the variable x, such as:
"x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. .
Remember the quote " " for passing a string to an Excel function. "funct" may be also a cell
containing a string formula.
The limits "a" and "b" can also be infinite. In this case insert the string "inf"
"Param" contains labels and values for parameters substitution (if there are)
For further details about writing a math string see Math formula string
The Double Exponential method is a fairly good numerical integration technique of high
efficiency, suitable for integrating improper integrals, infinite integrals and "stiff" integrals having
discontinue derivative functions.
This ingenious scheme, was introduced first by Takahasi and Mori [1974]
For finite integral, the formula, also called "tanh-sinh transformation", is the following
∫∫
+∞
∞−
⋅= dtthtxfdxxf
b
a
)())(()(
where:
( ))sinh(tanh
22
)( t
abab
tx
−
+
+
=
( ))sinh(cosh
)cosh(
2
)( 2
t
tab
th
−
=
Example
4guatda.com/cmx.p996...0.47442115)1(
1
0
3.05.0
=−∫ dxxx
The above integral is very difficult to compute because the derivative is discontinue at 0 and 1
The Romberg method would require more than 32.000 points to reach an accuracy of 1E-7. On
the contrary, this function requires less then 100 points for reaching the high accuracy of 1E-14
This function can also evaluate infinite and/or semi-infinite integral.
1
This function uses the double exponential quadrature derived from the original FORTRAN subroutine INTDE of the
DE-Quadrature (Numerical Automatic Integrator) Package , by Takuya OOURA, Copyright(C) 1996
Xnumbers Tutorial
157
Example
∫
∞
−
0
dxx n
As known, the integral exist if n > 1 and its value is I = 1/(n-1). The parameter "n" is called
"order of convergence". For n = 1.1 we get I = 10
Note that we need to pass the parameter with its label "n". (Param = "D1:D2")
This function cannot give reliable results if n is too close to 1.
The minimum value is about n = 1.03. For lower values the function returns "?".
The DE integration works very well for finite improper integral
Example
( ) ( ) 2lnlimln
1
2
0
1
0
2
−== ∫∫ +
→
a
a
dxxdxx
Note that the function f(x) is not defined for x = 0
Example. Another difficult improper integral
9/4)ln(
1
0
−=⋅∫ dxxx
Xnumbers Tutorial
158
Function Integration (mixed method)
= Integr(Funct, a, b, [Param])
This function computes the numeric integral of a function f(x) over a finite or infinite interval
∫
b
a
dxxf )( ∫
∞+
)(
a
dxxf ∫∞−
b
dxxf )( ∫
∞+
∞−
)( dxxf
This function can compute definite integrals, improper integrals and piece-wise functions
integrals. The parameter "funct" is a math expression string in the variable x, such as:
"x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. .
Remember the quote " " for passing a string to an Excel function.
"funct" may be also a cell containing a string formula
The limits "a" and "b" can also be infinite. In this case, insert the string "inf"
"Param" contains labels and values for parameters substitution (if there are)
This function uses two quadrature algorithms
1) The double exponential method1
(see function integr_de )
2) The adaptive Newton-Cotes schema (Bode's formula) (see macro Integral_Inf )
If the first method fails, the function switches on the second method
Oscillating functions, need specific algorithms. See Integration of oscillating functions (Filon
formulas) and Fourier's sine-cosine transform
Example. Compute the integral of x⋅cos(x) for 0 ≤ x ≤ 0.4
In the given interval the function is continuous, so its definite integral exists. This result is
reached with rank = 4, s = 16 sub-intervals, and an estimate error of about 3.7E-16. This
function returns the integral and can also displays the number of sub-intervals and the estimate
error. To see these values simply select three adjacent cells and give the
CTRL+SHIFT+ENTER keys sequence.
Note that the function Integr is surrounded by { } . This means that it returns an array
The function Integr can accept also parameters in the math expression string.
See the example below.
1
This function uses the double exponential quadrature derived from the original FORTRAN subroutines INTDE and
INTDEI of the DE-Quadrature (Numerical Automatic Integrator) Package , by Takuya OOURA, Copyright(C) 1996
Xnumbers Tutorial
159
Note that we must include the parameter labels in order to distinguish the parameters "k", "w",
and "q". The integration variable is always "x"
Beware of the poles
Before attempting to evaluate a definite integral, we must always check if the integral exists.
The function does not perform this check and the result may be wrong. In other words, we have
to make a short investigation about the function that we want to integrate. Let's see the
following example.
Assume the following integrals to have to compute
dx
x
dx
x 12
2
,
12
2
2
1
0
2
2/1
0
−− ∫∫
We show that the first integral exists while, on the contrary, the second does not exist
For ...707.02/2 ≅=px the function has a pole; that is:
+∞=





−
−∞=





− +−
→→ 12
2
lim,
12
2
lim 22
xx pp xxxx
The first integral exists because its interval [0, 0.5] does not contain the pole and the function is
continuous in this interval. We can compute its exact value:








+
−
=
−∫ |12|
|12|
log
2
2
12
2
2
x
x
dx
x
⇒ ( )12log2
12
2
2/1
0
2
−=
−∫ dx
x
In this situation the function Integr
returns the correct numeric result
with an excellent accuracy, better
than 1E-14.
For this result the integration
algorithm needs 128 sub-intervals
The interval of the second integral contains the pole, so we have to perform some more
investigation. Let's begin to examine how the integral function approaches the pole xp taking
separately the limit from the right and from the left
−∞=








+
−
−∞=








+
−
+−
→→ 12
12
loglim,
12
12
loglim
x
x
x
x
pp xxxx
As we can see the both limits are infinite, so the second integral does not exists
Note that if we apply directly the fundamental integral theorem we would a wrong result:
( )12ln2
|12|
|12|
log
2
2
12
2
1
0
!1
0
2
−=
















+
−
=
−∫ x
x
dx
x
wrong
Let's see how the function integr works in this case.
Xnumbers Tutorial
160
The numeric result is, of course,
completely wrong because the
given integral goes to the infinity.
But, even in this situation, this
function gives us an alert: the sub-
intervals have reached the
maximum limit of 65536 (216
)
So the result accuracy must be
regarded with a reasonable doubt.
Complex Function Integration (Romberg method)
=cplxintegr(Funct, a, b)
This function returns the numeric integral of a complex function f(z) by the Romberg method.
dzzfaFbF
b
a
)()()( ∫=−
The integration function "Funct" must be a string in the variable z and can be defined mixing all
arithmetic operators, common elementary functions and complex numbers like:
"z*cos(z)", "1+(1+i)*z+z^2", "exp(-z^2)", ecc.. .
Remember the quote " " for passing a string to an Excel function.
Parameters "a" and b” can be real or complex. Complex values are inserted as arrays of two
cells.
Example: Evaluate the following integral
dz
z
i
i
i
1
1
1
2∫
+
−
+
Because the integration function is analytic, then the given integral is independent form the
integration path. Therefore it can be calculated by the function cplxintegr
The exact result is the complex
number (−1+i)
Note that, thanks to the excellent
accuracy, the result is shown exactly
even if it is intrinsically approximated
Xnumbers Tutorial
161
Data Integration (Newton-Cotes)
=IntegrDataC(x,y, [Degree])
This function returns the integral of a discrete set of points (xi, yi) using the Newton-Cotes
formulas. The points may be equidistant or random. The parameter "Degree", from 1(default) to
10, set the degree of the Newton-Cotes formula written as:
∑∫
=
+
⋅⋅=
n
j
jj
hnx
x
bf
k
h
dxxf
0
0
0
)(
where fi = yi , "h" is the integration step, "n" is the degree.
The coefficients (bj , K) can be extracted from the following table:
Degree 1 2 3 4 5 6 7 8 9 10
K 2 3 8 45 288 140 17280 14175 89600 299376
b0 1 1 3 14 95 41 5257 3956 25713 80335
b1 1 4 9 64 375 216 25039 23552 141669 531500
b2 1 9 24 250 27 9261 -3712 9720 -242625
b3 3 64 250 272 20923 41984 174096 1362000
b4 14 375 27 20923 -18160 52002 -1302750
b5 95 216 9261 41984 52002 2136840
b6 41 25039 -3712 174096 -1302750
b7 5257 23552 9720 1362000
b8 3956 141669 -242625
b9 25713 531500
b10 80335
As we can see, for degree=1, the Newton-Cotes formula coincides with the trapezoidal rule
and, for degree = 2, with the popular Cavalieri-Simpson formula.
Trapezoid rule Cavalieri-Simpson rule
( )10
1
0
01
2
)( ff
h
xf
xxh
x
x
+≅
−=
∫
( )210
2
0
02
4
3
)(
2
fff
h
xf
xx
h
x
x
++≅
−
=
∫
For degree = 4, the table gives the Bode's rule
4
04 xx
h
−
=
( )00000 1464246414
45
)(
4
0
fffff
h
dxxf
x
x
++++≅∫
Using the function IntegrDataC is very easy.
Example. Calculate the integral with the Newton-Cotes formulas of degree = 1, 2, 4, 6 for the
dataset obtained by sampling the function sin(x)/x with step 0.2 . In a previous example, using
the Romberg method we have approximated this integral obtaining
Si(1.6) ≅ 1.38918048587044 with an accuracy better than 1E-9
Let's see now how the Newton-Cotes formulas work.
Xnumbers Tutorial
162
As we can see, the convergence to the exact result is evident. The most accurate result is
reached with the 6th
degree Newton-Cotes formula. The IntegrDataC can work with random
samples.
Example. Given the data table (xi yi) , approximate the integral with the Cavalieri-Simpson
formula.
x y
1 0
1.1 0.09531018
1.2 0.182321557
1.4 0.336472237
1.8 0.587786665
2.4 0.875468737
3.2 1.16315081
4 1.386294361
5 1.609437912
0
0.4
0.8
1.2
1.6
2
0 1 2 3 4 5 6
Note that the data points are not equidistant.
The points have been
extracted from the function
y = ln(x) .
Thus the exact integral is
5*ln(5)-4 ≅ 4.0471896
Data integration for random points.
Having a set of not equidistant points (xi, yi), we cannot use directly the Newton-Cotes formulas
for fixed step.
In that case, IntegrDataC reorganizes the random data samples in equidistant data samples
and after that, computes the integral using the standard formulas for fixed step
Random Samples Converted to Equidistant Samples
{ (xi , yi) ; i = 0, 1, ... n } ⇒ { (xi = x0+i h , yi (xi ) ; i = 0, 1, ... m }
For computing the values f(x0+i h ) at the equidistant grid points, IntegrDataC uses the Aitken
Interpolation algorithm.
Aitken interpolation algorithm.
Given a set of points:
{ (xi , yi) i = 0, 1, ... n }
Xnumbers Tutorial
163
This method is used to find the interpolation point yp at the value xp .
It is efficient as the Newton formula, and it is also very simple to code.
For j = 1 To n - 1
For i = j + 1 To n
y(i) = y(j) * (x(i) - xp) - y(i) * (x(j) - xp)/ (x(i) - x(j))
Next i
Next j
yp = yi(n)
Function Integration (Newton-Cotes)
=Integr_nc(funct, a, b, Intervals, [Degree])
This function returns the numeric integral of a function f(x) using the Newton-Cotes formulas.
dxxfaFbF
b
a
)()()( ∫=−
The parameter "Funct" is a math expression string in the variable x, such as:
"x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. .
Remember the quote " " for passing a string to an Excel function.
"Funct" may be also a cell containing a string formula
The parameters "a" and "b" are the limits of integration interval
The parameter "Intervals" sets the number of sub-intervals of the integration interval.
The parameter "degree", from 1(default) to 10, set the degree of the Newton-Cotes formula.
degree = 1 coincides with the Trapezoidal rule; degree = 2 coincides with the Cavalieri-
Simpson formula; degree = 4 with the Bode's rule.
Remember that the total knots of the function computation is:
knots = Intervals × Degree + 1
Example: Approximate the following integral using 10 sub-intervals and three different
methods: trapezoidal, Cavalieri-Simpson, and Bode's rule.
∫
−
⋅
1
0
dxex x
The indefinite integral is known as the closed form:
( ) xx
exdxex −−
+−=⋅∫ 1
So we can compare the exact result, that is
1
21 −
− e ≅ 0.264241117657115356
Xnumbers Tutorial
164
Integr_nc("x*exp(-x)",0,1,10,1) = 0.263408098685072 (8.3E-04)
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0 0.2 0.4 0.6 0.8 1 1.2
x
f(x)
The trapezoidal rule,
with 10 sub-intervals,
requires 2 knots for
each sub-interval for a
total of 11 function
evaluations (11 knots)
The accuracy is better
then 1E-3
Integr_nc("x*exp(-x)",0,1,10,2) = 0.264241039074082 (7.8E-08)
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0 0.2 0.4 0.6 0.8 1 1.2
x
f(x)
The Cavalieri-Simpson
rule, with 10 sub-
intervals, requires 3
knots for each sub-
interval for a total 21
function evaluation (21
knots)
The accuracy is better
then 1E-7
Integr_nc("x*exp(-x)",0,1,10,4) = 0.264241117655293 (1.8E-12)
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0 0.2 0.4 0.6 0.8 1 1.2
x
f(x)
The Bode's rule, with 10
sub-intervals, requires
5 knots for each sub-
interval for a total of 41
function evaluation (41
knots).
The accuracy is better
then 2E-12
Xnumbers Tutorial
165
Integration: symbolic and numeric approaches
The usual approach to the calculation of the definite integral involves two steps: the first is the
construction of the symbolic anti-derivative F(x) of f(x)
∫= dxxfxF )()(
and the second step is the evaluation of the definite integral applying the fundamental
integration theorem.
∫ −=
b
a
aFbFdxxf )()()(
This approach can only be adopt for the set of the functions of which we know the anti-
derivative in a closed form. For the most f(x), the integral must be approximated either by
numerical quadrature or by same kind of series expansion.
It is usually accepted that symbolic approaches, when possible, gives more accurate result
then the numeric one. This is not always true. Even if the symbolic anti-derivative is known in a
closed form, it may often be unsuitable for further numerical evaluation. In particular, we have
cases in which such "exact" answers, when numerically evaluated, give less accurate results
than numerical quadrature methods1
Let's see. Assume to have the following integral functions
( ) cxdx
x
x
xF +=
+
= ∫
3
6
2
arctan
1
3
)(
We want to calculated the definite integral between a = 2000 and b = 2004
The analytic approach gives:
F(b) -F(a) = arctan(b3
) -arctan(a3
)
In the following worksheet we have compared the evaluations with the exact anti-derivative and
the numerical quadrature with the Bode's rule. In the cell C2 we have inserted the anti-
derivative function.
=ARCTAN(B2^3)-ARCTAN(A2^3)
In the cell C2 we have inserted the Bode formula with 20 intervals
=Integr_nc(D1, A2, B2, 20, 4)
In the cell C5 we have also inserted the reference integral value
As we can see, the more accurate result is those obtained with the numerical quadrature;
surprisingly, it is more than 200 millions times more accurate than the one of the exact method!
It is evident from this example that only the symbolic integration could not resolve efficiently the
problem. For numerical integration the quadrature methods are often more efficient and
accurate.
1
"Improving Exact Integral from Symbolic Algebra System", R.J. Fateman and W. Kaham, University of California,
Berkeley, July 18,2000
Xnumbers Tutorial
166
Integration of oscillating functions (Filon formulas)
=Integr_fsin(Funct, a, b, k, Intervals)
=Integr_fcos(Funct, a, b, k, Intervals)
Oscillating functions can reserve several problems for the common polynomial integration
formulas. The Filon's formulas are suitable to compute efficiently the following integrals.
∫
b
a
dxkxxf )cos()( ∫
b
a
dxkxxf )sin()(
for k = 1, 2, 3...N
The parameter "Funct" is a math expression string in the variable x, such as:
"x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. .
Remember the quote " " for passing a string to an Excel function.
"Funct" may be also a cell containing a string formula
The parameters "a" and "b" are the limits of integration interval
The parameter "k" is a positive integer
The parameter "Intervals" sets the number of sub-intervals of the integration interval.
Remember that the total number of the evaluation nodes is: Nodes = Intervals × 2 + 1
To understand the effort in this kind of numerical integration let's see this simple test. Assume
we have to evaluate the following integral for several integer values k, with 0 < k < 25
∫
π
0
4
)cos( dxkxx
If we perform the computation with the Cavalieri-Simpson formula (80 nodes) and with the Filon
formula (80 nodes), we get the following result
1.E-08
1.E-07
1.E-06
1.E-05
1.E-04
1.E-03
1.E-02
1.E-01
1.E+00
0 5 10 15 20 25 30
k
Cavalieri-Simpson
Fillon
Relative error versus k
As we can see, the relative error
increases with the number k much
more for the Cavalieri-Simpson rule
than the Filon formula.
For k = 24 the first formula should have
400 nodes at least, for reaching the
same accuracy of the Filon formula.
Example: evaluate the integral of the following oscillating function
∫ ⋅
+
π2
0
2
)8sin(
1
1
x
x
that can be rearranged as
∫ ⋅
π2
0
)8sin()( xxg where
1
1
)( 2
+
=
x
xg
The plot of the integration function and its envelope function g(x) are shown in the following
graph
Xnumbers Tutorial
167
-1.2
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
0 1 2 3 4 5 6 7
1/(x^2+1)
-1/(x^2+1)
1/(x^2+1)*sin(8*x)
Below, a simple arrangement to compute the given integral
The approximate error is less then 1E-8, with 300 intervals (default)
Xnumbers Tutorial
168
Integration of oscillating functions (Fourier transform)
= Fourier_sin(funct, k, [a], [param])
= Fourier_cos(funct, k, [a], [param])
These functions1
perform the numerical integration of oscillating functions over infinite intervals
∫
+∞
⋅⋅
a
dxxkxf )sin()( ∫
+∞
⋅⋅
a
dxxkxf )cos()(
If a = 0 (default) , these integrals are called "Fourier's sine-cosine transforms"
The parameter "funct" is a math expression defining the function f(x), not oscillating and
converging to 0 for x approaching to infinity:
"1/x", " 1/(8*x^2)", " exp(-b*x)", ecc.. .
Remember the quote " " for passing a string to an Excel function. "Funct" may be also a cell
containing a string formula
The parameter "k" is a positive number
The "Param" contains labels and values for parameters substitution (if there are)
These functions return "?" if the integral is not converging or if they cannot compute the integral
with sufficient accuracy
For finite integration see also Integration of oscillating functions (Filon formulas)
Example. Prove that is
2
sin
0
π
=∫
∞+
dx
x
x
The graph of the integration functions is
at the right.
Numerically specking, this integral is
very difficult to calculate for many
algorithms.
For example, the Bode adaptive quadrature needs more than 10.000 points for getting
accuracy of about 1E-4. The Fourier_sin function on the contrary is very efficient for this kind of
integral. The integral can be arranged in the following form
∫∫
∞+∞+
=
00
sin
1sin
dxx
x
dx
x
x
That is the Fourier's sine transform of 1/x
1
These functions use the double exponential quadrature derived from the original FORTRAN subroutine INTDEO of
the DE-Quadrature (Numerical Automatic Integrator) Package , by Takuya OOURA, Copyright(C) 1996
Xnumbers Tutorial
169
We see that the accuracy is better than 1E-15. Note that the function Fourier_sin automatically
multiply the integration function f(x) for the factor sin(k*x), so we have only to pass the f(x)
expression.
Example. Verify that is
8
4cos
0
π
=∫
∞+
dx
x
x
The graph of the integration functions is
Observe that the integration function goes to
infinity for x approaching to 0.
Numerically specking this function is "terrible".
The integral can be arranged in the following form
∫∫
∞+∞+
=
00
4cos
14cos
dxx
x
dx
x
x
That is the Fourier's cosine transform of 1/x^0.5
The accuracy is better than 1E-15
Infinite Integration of oscillating functions
Generally, the infinite integration of real functions having a certain type of infinite oscillating tails
may give some problem even to the most efficient quadrature algorithms. These problems can
be avoided adopting specific integration tricks
Let's see some of them.
Example. Assume to calculate the following integral
( ) ( ) dx
x
xx 2coscos
0
∫
+∞
−
The integration function coverges to zero but it contains two oscillating terms. So we cannot
use directly the integr or integr_de function because they returns "?"
For solving we can use the Fourier's cosine trasform, separating each oscillating term.
The given integral can be re-arranged in the following way
( ) ( ) ( ) ( ) ( ) ( ) dx
x
x
dx
x
x
dx
x
xx
dx
x
xx 2coscos2coscos2coscos
11
1
00
∫∫∫∫
∞+∞+∞+
−++
−
=
−
Note that the last two integrals cannot have the lower limit 0 because they do not converge for
x approaching to 0.
Xnumbers Tutorial
170
The first integral can be evaluted with the function integr and the two last integrals are
evaluated with the function Fourier_cos with a = 1. Let's see the following spreadsheet
arrangement
Compare the accuracy with the exact result I = Ln(2)
Example. Calculate the following integral
dx
x
x)(sin
0
2
4
∫
∞+
Remembering that is
8
)4cos(
2
)2cos(
8
3
)(sin4 xx
x +−=
The given integral can be arranged as
( ) ( )dx
x
x
dx
x
x
dx
x
dx
x
x
dx
x
x
∫∫∫∫∫
+∞+∞+∞∞+
+−++=
1
2
1
2
1
2
1
0
2
4
0
2
4
8
4cos
2
2cos
8
3sinsin
The first and second integral can be evaluted with the function integr and the two last integrals
are evaluated with the function Fourier_cos with a = 1.
Compare the accuracy with the exact result I = π /2
Xnumbers Tutorial
171
Example. Calculate the following integral
( )dxxsin
0
2
∫
∞+
This function oscillates very badly. Note that the function does not converge to zero, oscillating
continuously from 1 and −1, but we can show that its integral is finite.
Let's perform the substitution
dt
t
dxtxtx
2
12
=⇒=⇒=
So, the given integral becomes
( ) ( ) dt
t
t
dxx
2
sin
sin
00
2
∫∫
∞+∞+
=
That can be easily computed by the Fourier's cosine transform
Compare the accuracy with the exact result I = (π /8)1/2
Xnumbers Tutorial
172
Double Integral
2D Integration for Normal Domains
Xnumbers contains routines for integrating bivariate functions f(x, y) over a normal domain
(normal to the x-axis and/or to the y-axis) or a circular domain.
c
d
h 2(y)h 1(y)
x
y
Domain normal to y-axis
a b
g 1(x)
g 2(x)
x
y
Domain normal to x-axis
x2
+y2
≤ R2
x
y
Circular Domain
For those kinds of 2D-domains the integration formulas can be re-written as the following
∫ ∫∫∫ =
b
a
xg
xgD
dydxyxfdsyxf
X
)(
)(
2
1
),(),(
∫ ∫∫∫ =
d
c
yh
yhD
dxdyyxfdsyxf
Y
)(
)(
2
1
),(),(
∫ ∫∫∫ =
π
θρρθρθρ
2
0 0
))sin(),cos((),(
R
C
ddfdsyxf
Note that a normal domain implies that - at least - one axis must have constant limits.
Rectangular domains are a sub-case of normal domains in which both axes have constant
limits.
The macro Integr2D - suitable for integrating smooth functions f(x, y) – and its function version
Integr_2D use the same bidimensional Romberg algorithm, but the function is limited to about
65.000 points.
Macro for Double Integration
Integr2D()
This macro performs the numerical integration of a smooth, regular function f(x, y) over a plane
normal domain D(x, y) .
∫ ∫
max
min
max
min
),(
X
X
Y
Y
dydxyxf
The integration functions f(x, y) and – eventually – also the bounding limits – Xmin, Xmax,
Ymin, Ymax -can be written in symbolic expression. For further details about the math string
see Math formula string
The integration function can be:
• bivariate functions like x^2+y^2-x*y, log(1+x+y), 1/(1+x^2+2*y^2), etc.
• constant numbers like 0 , 2, 1.5, 1E-6 , etc.
• constant expressions like 1/2, √2+1, sin(0.1), etc.
Xnumbers Tutorial
173
Boundary limits can be:
• constant numbers like 0 , 2 , 10 , 3.141 , etc.
• constant expressions like 1/2 , √2+1, pi, sin(1/2*pi) , exp(1) , etc.
• mono-variable functions like x/2 , 3y-10 , x^2+x-1 , etc.
A normal domain has, at least, two constant boundary limits.
Function and limits can be passed to the macro directly or by reference. That is: you can write
directly the symbolic expressions or constants into the input-box of the macro panel or you can
pass the cells containing the expressions. This second mode is more easy and straight
Let’ see how it works.
Approximate the following double integral of the function f(x,y) = ln(1+x+y) in the closed region
delimited by the given constrains
Integration function
)1ln( yx ++
Integration domain D
1
10
2
+≤≤
≤≤
xyx
x
The domain D is shown in the above plot. As we can see, it is an x-nomal-domain domain.
Verify that the given integral approximates the symbolic expression at the right
∫ ∫∫∫
+
++=
1
0
1
),( 2
)1ln(),(
x
xyxD
dydxyxdsyxf
8
17
12
3
)2ln(7
4
)3ln(9
−−+
− π
The macro assumes as default
the following simple
arrangement (but, of course, it
is not obligatory at all)
Select the A2 cell and start the
Integr2D macro.
As we can see, the entire input boxes
are filled with the right references.
The output result will start from the A4
cell
Optionally we can adjust the Error limit
or the Rank. But usually the only thing to
do is click on the “run” button
Warning: The Rank increase
exponentially the computation effort,
because points = (2K)^2
"Parameter" input box may be used to
pass values of one or more parameters
eventually present in the integration
function
Activate "polar" for switching to the polar
coordinate system
Xnumbers Tutorial
174
The macro outputs 5 results:
1) Integral
2) Relative error estimation
3) Total points evaluated
4) Elaboration time
5) Error message (if any)
Example 2. Approximate the double integral of the function
[ ] x
exy 852.0148.3
)(218.01 −−
⋅−+
in the closed region delimited by
xyx ≤≤≤≤ 0,10
The above expression can be written as
"(1+0.218*(y-x))^(-3.148)*exp(-0.852*x)"
or alternatively
"(1+218/1000*(y-x))^(-3148/1000)*exp(-852/1000*x)"
In both cases the result is obtained with high precision
Another elegant way to insert constant in the integration function is using parameters
Example. Evaluate numerically the following double integral
∫∫
π π
0 0
)cos()sin( dxdymynx
for n = 0.75 and m = 0.125
Set two cells, for example, F2 and G2, containing the values of n and m; just above, in the cells
F1 and G1, add their name "n" and "m" and pass the entire range F1:G2 as parameters.
The result will be
Xnumbers Tutorial
175
Compare with the exact result 2562128
3
1
+
Macro for Triple Integration
Integr3D()
3D Integration in normal domains
This macro calculates the triple integral of a function f (x, y, z) in a normal domain or in a
spherical domain or in a cylindrical domain.
For this kinds of domains the triple integral can be decomposed by the following formulas
∫∫∫ ∫ ∫ ∫=
D
x
x
y
y
z
z
dxdydzzyxfdxdydzzyxf
max
min
max
min
max
min
),,(),,(
Where the functions:
• Xmin and Xmax may depend on variables y and z
• Ymin and Ymax may depend on variables x and z
• Zmin and Zmax may depend on variables x and y
The normal domain condition implies that, at least, one boundary must be constant
The macro can integrate in spherical coordinates system, using the formulas
ϕρ
θϕρ
θϕρ
cos
sinsin
cossin
=
=
=
z
y
x
where θ, from 0 to 2π, is the rotation angle on the xy-plane and φ , from 0 to π, is the colatitude
angle
The triple integral changes in
ρθϕϕρ
ρ
ρ
θ
θ
ϕ
ϕ
dddzyxfdxdydzzyxf
D
sin),,(),,(
max
min
max
min
max
min
2
∫∫∫ ∫ ∫ ∫=
The macro can also integrate in cylindrical coordinates system, using the formulas
zz
y
x
=
=
=
θρ
θρ
sin
cos
Xnumbers Tutorial
176
where θ, from 0 to 2π, is the rotation angle on the xy-plane
The triple integral changes in
∫∫∫ ∫ ∫ ∫=
D
z
z
dzddzyxfdxdydzzyxf
max
min
max
min
max
min
),,(),,(
ρ
ρ
θ
θ
θρρ
Functions f(x, y, z) and – eventually – also the boundary functions may be written in symbolic
expression..
For further details about the math string see Math formula string
Integration function can be:
• three-variate functions like x^2+y^2-x*z , log(1+x+y+z) , (x+z)/(1+x^2+y^2) , etc.
• Constant numbers like 0 , 2 , 1.5 , 1E-6, etc.
• Constant expressions like 1/2 , √2+1 , sin(pi/4) , etc.
Boundary limits can be:
• Constant numbers like 0 , 2 , 10 , 3.141 , etc.
• Constant expressions like 1/2 , √2+1, pi, sin(1/2*pi) , exp(1) , etc.
• univariate or bivariate functions like x/2 , 3y-10 , x^2+x-1 , sin(x+z), Ln(x+y) , etc.
A normal domain has, at least, two constant boundary limits.
The Integration function and the boundary limits can be passed to the macro directly or by
reference. That is: we can write directly the symbolic expressions into the input fields, or you
can pass the cells that containing the expressions (simpler), or even a mixed mode.
Let’s see how it works
Example 1. Approximate numerically the following triple integral
dxdydzxyz54
1
0
2
0
3
0
∫∫∫
The integration domain is the parallelepiped of lengths 1, 2, 3
The macro assumes as default the following simple layout (but, of course, it is not obligatory)
Now select the cell A2 containing the integration function and start the macro from the
Xnumbers toobar Macros > Integral > Triple.
Xnumbers Tutorial
177
As we can see, all the input
boxes are filled with the right
references.
The output result will start
from the A4 cell
Optionally we can adjust the
Rank, increasing it, if we want
to increase the final accuracy.
But usually the only thing to
do is click on the “run” button
Warning: The Rank increase
exponentially the computation
effort, because the number of
max points is (2*Rank)^3
Parameter input box may be
used to pass values of one or
more parameters eventually
present in the integration
function
The macro outputs 5 results:
1) Integral
2) Relative error estimation
3) Total points evaluated (function + boundaries)
4) Elaboration time
5) Error message (if any)
The results will appear as the following
Example 1. Approximate numerically the following triple integral
∫∫∫ +
D
dxdydzyx )(
1 22
π
where D is the domain defined by the following implict relation
1222
≤++ zyx
This domain is a sphere of unitary radius. Therefore is better to pass to the spherical
coordinates because the integration domain becomes much simpler, being
10 ≤≤ ρ , πθ 20 ≤≤ , πϕ ≤≤0
The macro automatically performs the spherical transformation; so we have only to set the right
boundaries
Select the cell A2 and start the macro. Select "Polar" and set the Rank = 25. Press "Run"
Xnumbers Tutorial
178
Compare the result with the exact solution 8/15 = 0.53333...
Double integration function
=Integr_2D (Fxy, Xmin, Xmax, Ymin, Ymax, [Polar],[ErrMax])
This function returns the numeric integral of a smooth regular function f(x, y) over a plane
normal domain D(x, y) .
∫ ∫
max
min
max
min
),(
X
X
Y
Y
dydxyxf
The integration functions f(x, y) and – eventually – also the bounding limits – Xmin, Xmax,
Ymin, Ymax -can be written in symbolic expression. For further details about the math string
see Math formula string
The integration function can be:
• bivariate functions like x^2+y^2-x*y, log(1+x+y), 1/(1+x^2+2*y^2), etc.
• constant numbers like 0 , 2, 1.5, 1E-6 , etc.
• constant expressions like 1/2, √2+1, sin(0.1), etc.
The boundary limits can be:
• constant numbers like 0 , 2 , 10 , 3.141 , etc.
• constant expressions like 1/2 , √2+1, pi, sin(1/2*pi) , exp(1) , etc.
• monovariable functions like x/2 , 3y-10 , x^2+x-1 , etc.
A normal domain has, at least, two constant boundary limits.
Example. Approximate the following double integral
∫ ∫
+
++
1
0
1
2
1)yln(x
x
x
dxdy
8
17
12
3
)2ln(7
4
)3ln(9
−−+
− π
The integration domain is shown in the previous example.
In order to avoid long elaboration time, the function limits the total evaluation points to about
65.000 (rank = 8). For heavy computations use the macro Integr2D
Xnumbers Tutorial
179
Note: this function can also return the relative error, the total of evaluation points and the error
message (if any). To see these values simply select a range of two, three or four, adjacent cells
(vertical or horizontal) and give the CTRL+SHIFT+ENTER key sequence.
Example: Approximate the following integral
dxdy
yx∫ ∫ ++
1
0 y/2
22
x
e
1
1
The integration domain is represented in the following plot
Integration domain D
y
ex
y
y
≤≤
≤≤
2
10
As we can see the domain is
normal to the y-axes
The calculus of this double integral can be arranged as the following
Sometime, the integral my contain one or more parameters
Example
∫∫
⋅+⋅
1
0
1
0
)( 22
dxdye ybxa
Note that the parameters "a" and "b" must be passed with their labels.
Xnumbers Tutorial
180
Example. Compute numerically the following integral
dxdyyx
D
∫∫ + 22
Where the domain D is the half-circular
region showed at the right
Set the parameter "Polar" to True.
Compare the precision with the exact result
Xnumbers Tutorial
181
Infinite integral
Integral_Inf()
This macro performs the numeric integration of a smooth, regular, not oscillating function f(x)
over an unlimited (or very long) interval
∫∫∫
+∞
∞−∞−
+∞
dxxfdxxfdxxf
a
a
)(,)(,)(
This macro can use two different methods:
• The Bode formula with adaptive step
• The double exponential algorithm
The Bode formula with 8 steps to calculate the integral and the truncation error.
( )432101 73212327
45
2
fffff
h
Ih ++++=
( )876542 73212327
45
2
fffff
h
Ih ++++=
21 hhh III +=
63
2hh
T
II
E
−
≈
After each step the routine detects the truncation error and recalculates the step in order to
keep a constant error (variable step integration method).
The double exponential algorithm, also called "tanh-sinh quadrature". first introduced by
Takahasi and Mori, is based on the hyperbolic variable transformations.
It is more complicated then the polynomial Newton-Cotes schema but, on the other hand, it is
much more efficient.
Using this macro is very easy.
Example: Approximate the given integral
∫
∞+
−
⋅⋅
0
2
100 dxex x
The integration function is regular over the entire x-axes; the exponential assures the
convergence. Therefore the infinite integral exists.
Put the symbolic expression “100*x^2*exp(-x)” in any cell that you like (A3 for example), and
arrange the worksheet in the following way (but it is not obligatory at all)
The word “inf” means – of course –infinity. It is not necessary to specify the sign, because the
macro always assumes “b” as +inf , “a” as -inf
Now select the cell A3 and run the macro Integral_Inf . The input fields will be automatically
filled
Xnumbers Tutorial
182
Choose “run” to start the integration routine. The result will be similar to the worksheet below
(without formatting) where we have compared the results of both methods
As we can see the integral is 100 with an excellent approximation for both methods but the
double exponential is more efficient. It required only 199 function evaluations.
Sometime we have to calculate the integral over the entire x- axes. Let’ see
∫
∞+
∞− ++
++
dx
xx
xx
1
32
4
2
Note that, in this case, we have needed more than 10.000 evaluation points for the variable
step method but only 344 for the DE method. The superiority is ever so evident? Not always.
There are cases in which the adaptive quadrature schema works better. For example when the
integration function has a finite discontinuity (jump) inside the integration interval; this usually
happens for the piecewise functions.
Example, Assume to have to compute the following didactical integral
( )∫
∞+
−
1
2
1
5.1sgn dx
x
x
The integration function is shown in the
following graph
In this case is easy to calculate the integral
simply separating the given interval [1, +∞] in
two sub-intervals: [1, 1.5]∪ [1.5, +∞].
Calculating each integral and summing we get
the exact result I = −1/3.
But we want here to investigate how the two methods works in this situation
Xnumbers Tutorial
183
As we can see the variable step method has find the result with high precision using about
7200 steps. The double exponential algorithm fails the convergence
We have to put in evidence that using quadrature algorithms in a “blind” way, may lead to
wrong result. We should always study the integration function to discover singularities,
discontinuities, convergence rate, etc. If the integration function is “sufficiently” smooth, then
the numeric integration can give good approximate results.
This routine can also be used over a closed long interval, when other algorithms would take too
long computational time.
Double Data integration
=IntegrData2D(Dataxy, hx, hy)
Given a bidimensional set of points (xi, yj) defined by a rectangular grid
{ }mjnijhyyihxxyxD yjxiji ...0,...0,,|),( 00 ==+=+=≡
this function computes the numerical double integral over the domain D
∫∫D
dxdyyxf ),(
"Dataxy" is a (n x m ) rectangular array containing the function values f(xi, yj)
"hx" and "hy" are the grid intervals respectively of the x-axis and the y axis
Using this function is very simple.
Example assuming to have the following rectangular dataset (x, y) in wich each cell represent a
point of the grid and thus a value of the function f(xi, yj) where i = 0, 1, 2...20, j = 0, 1, 2...20
Xnumbers Tutorial
184
the function = IntegrData2D(B54:V74, C53, A55) returns the value 16.2661406
The scales x and y at the border of the range are not necessary for the function IntegrData2D.
They are designed only for clarity.
This function can also return the estimate error as second, optional value. To see also the error
select two adjacent cells and insert the function with the CTRL+SHIFT+ENTER keys
The given data set was generated by the function
yx
eeyxf −−
+= 3/
),(
with xi = 0.3 i and yi = 0.2 j
Therefore the above result is an approximation of the following integral
∫∫
−−
+
3
0
4
0
3/
dydxee yx
that is about 16.266082.
One interesting feature of this function is that
it accepts piecewise-rectangular domains
For example. Assume you have to
approximate the following integral
∫∫ ++
D
yx )12Ln(
where D is the domain shown at the right
This integral could be split, of course, into three rectangular integrals
∫∫ ∫∫∫∫ ++
3
0
3
1
2
1
1
0
2
0
3
3
),(),(),( dydxyxfdydxyxfdydxyxf
which the result is about 18.18803389
Xnumbers Tutorial
185
But it could be approximated in a worksheet sampling the domain for 0 <= x <= 3 and 0 <= y <=
4 with a suitable grid, for example hx = 0.1 , hy = 0.1
The cells out of the domain must be left empty.
The entire range is now B2:AF42 but the function will compute the integral only over the not
empty cells.
Note that in that case the scales x and y at the border of the range are used for calculating the
function f(xi,yi)
The integral and its estimate error will be
Note the good accuracy of this result.
IntegrData2D use the bidimensional trapezoidal formula with one-step of the Richardson
extrapolation. This algorithm works very fine with smooth regular functions and its precision is
similar the Cavalieri-Simpson formula
Xnumbers Tutorial
186
Series Evaluation
=xSerie(Funct, Id, a, b, [Param], [DgtMax])
Returns the numeric series of a function f(n) .
∑=
=
b
an
nfS )(
The parameter "Funct" is a math expression string such as:
"2^n/n*(-1)^(n+1)", "x^n/n!", "(-1)^(n)*(3+a)*x/(n-1)", ...
Remember the quote " " for passing a string to an Excel function.
For further details about the math string see Math formula string
"Id" indicates the integer index of the sum (usually "n", "k" , "i", etc.)
"a" and "b" are the limits of the sum.
The function may also have other parameters ("x", "y", "a", etc.) that can assume real values.
"Param" contains labels and values for parameters substitution (if there are). If we pass the
variable range without “labels”, the function will assign the values to the variables in the same
order that they appear in the formula string, from left to right.
The parameter "DgtMax" sets the multiprecision arithmetic. if omitted or zero the function uses
the fastest standard arithmetic
Example 1. Compute
∑=
+
⋅−
10
1
1
)1(
n
n
n
n
x
for x = 2, with standard precision (15 digits) and with 25 digits.
The function substitutes x = 2 and then, computes the series f(n) for n =1, 2 ...10
10
2
...
4
2
3
2
2
2
2
2
)1(
10432
1
10
1
1
−−+−=⋅−∑=
+
n
n
n
n
xSerie("(-1)^(n+1)*x^n/n","n",1,10,2)= -64.8253968253968
Example 2. Compute
∑=
=
10
0n
n
n!
x
s
for x = −1.5, with standard precision (15 digits) and with 25 digits. As known, this series
approximates the exponential e^(-1.5)
The function xSerie accepts one or more parameters.
Xnumbers Tutorial
187
Example 3. Compute the following series where a and b are parameters
∑=
+⋅
=
10
1n n
n ab
s
for a = 0.7 and b = 1.5, in standard precision
Note that we have enclosed the labels "a" and "b" in the range B4:C5 passed to the function as
the argument "Param". The labels indicate to the function the correct assignment between the
variables and their values
Labels are optional. If we pass only the range B5:C5, without labels, the function assign the
values to the variables in the order from left to right.
Note how compact and straight is this calculation using the xSerie function.
Series acceleration with ∆^2
Many series are very slow to converge requiring therefore methods to accelerate their
convergence. The Aitken's extrapolation formula ( ∆2
extrapolation) can be used for this scope.
Practically we build a new series S(1)
, whose partial sums Sn
(1)
are given by the Aitken's
formula. It is possible to repeat the process starting from the series S(1)
to obtain S(2)
., and so
on.
Example. We want to approximate the following series:
∑
∞
= +
−
=
0k
k
k1
)1(
S
We know the exact result that is
Σ = Log(2) = 0.693147180559945...
In the cell B4 we insert the formula
=Series($B$1;"k";0;A4)
In the cell C4 we insert
=(B4-LN(2))
we fill the rows from 5 to 16 simply selecting the range
B4:C4 and dragging it down.
In the last cell B17 we have inserted the function
=ExtDelta2(B10:B16)
performing the ∆2
extrapolation using the last 7 values
of the sum
As we can see, the cell B16 shows the sum with 12 terms; its approximation is very poor
having an error of more than 0.01. But if we apply the ∆2
extrapolation at the last seven partial
sums S(12)
, S(11)
, S(10)
.... S(6)
we have a good approximation with an error less then 1E-8
Note that for reaching this accuracy the given series would need more than 100 million terms!
Xnumbers Tutorial
188
Complex Series Evaluation
=cplxserie(Formula, a, b, [z0])
This function returns the numeric series of a complex function f(z, n) .
∑=
=
b
an
nzfS ),(
"Formula" is a math expression string defined by arithmetic operators and common elementary
functions such as:
"2^n/n*(-1)^(n+1)", "x^n/n!", "(-1)^(n)*(3+j)*x/(n-1)", ...
Remember the quote """ to pass a string to an Excel function.
The integer variable must be “n”.
The parameters "a" and "b" set the minimum and the maximum limits of the integer variable "n".
The function may have also a complex variable "z". In that case specify its value in the
parameter z0.
Example: evaluate the given series for z = z0 = 2-i
20
...
32
20
1
zzz
z
n
z
S
n
+++== ∑=
Xnumbers Tutorial
189
Double Series
= xSerie2D(Funct, Id1, a, b, Id2, c, d, [Param], [DgtMax])
Returns the numeric double series of a function f(n, m).
∑∑= =
=
b
am
d
cn
nmfS ),(
The parameter "Funct" is a math expression string such as:
" x^(n+2*m)/(n!*m!)", "(n+1)/(m+1)!", "comb(n,k)*a^k*b^(n-k)" ...
Remember the quote " " for passing a string to an Excel function.
For further details about the math string see the par. Math formula string
"Id1" , "Id2" indicate the integer indexes of the sum (usually "n", "m", "k" , "i", etc.)
"a" , "b", "c", "d" set the minimum and the maximum limits of the integer variables n and m.
The function may have other parameters ("x", "y", "a", etc.) that can assume real values.
"Param" contains labels and values for parameters substitution (if there are). If we pass the
variable range without “labels”, the function will assign the values to the variables in the same
order that they appear in the formula string, from left to right.
The parameter "DgtMax" sets the multiprecision. if omitted or zero the function uses the fastest
standard arithmetic
Example. Compute the following double series, in standard (15 digits) and multiprecision (25
digits)
( )
∑ ∑= =
+
=
4
0m
10
1n
2mn
m!n!
x
s
for x = 0.8
Take care to the index limits because, for large interval, this function can slow down your Excel
application
Xnumbers Tutorial
190
Trigonometric series
= Serie_trig(t, period, spectrum, [offset], [angle], [smooth])
It returns the trigonometric series
( )∑=
++=
N
1n
sin)0()( nn tnaftf θω
T
π
ω
2
=
The set
( ) Nna nn ...1,, =θ
is called “spectrum” of the function f(t)
Each couple is called harmonic.
The parameter “t” can be a single value or a vector values
The parameter “period” is the period T.
The parameter "spectrum" is an array of (n x 2) elements: the first column contains the
amplitude, the second column the phase.
The optional parameter "offset" is the average level (default 0)
The optional parameter "Angle" sets the angle unit: (RAD (default), DEG, GRAD)
Optional parameter smooth (default False) applies the Lanzos factor to the trigonometric series
)sin()0()(
1
n
N
n
nn tnaftf θωσ ++= ∑= Nn
Nn
n
/
)/sin(
π
π
σ =
Here is a worksheet arrangement to
tabulate a trigonometric serie having
a spectrum of max 8 harmonics (the
formulas inserted are in blue)
The independent parameters are N
(samples) and T (periodo)
From those, we get the sampling
interval
∆T = T/(N-1)
The table at the left contains the
parameters for each harmonic: the
integer multiple of the harmonic, its
amplitude and its phase
0
0.5
1
1.5
2
2.5
3
3.5
4
0 0.5 1 1.5 2
Period T
Period T = 1
Note that we can always transform
the cosine terms into sine with the
following formula
)2/sin()cos( παα +=
n° Arm. Amp Phase
1 1 45
2 0 0
3 0.4 -45
Xnumbers Tutorial
191
The “ringing” phenomenon is an overshoot of Fourier series occurring at simple discontinuities
(jumps). The series does not converge uniformly to a discontinuous function in a small interval
of the discontinuity point. It can be proven that, at each discontinuity point, the series either
overshoots or undershoots by about 9% of the magnitude of the jump.
The ringing can be removed with the Lanczos sigma factors.
Example. The phenomenon is illustrated for a square wave.
Given the spectrum of the first 20 harmonics An = { 0, 1, 0, 1/3, 0, 1/5, 0, 1/7…1/19, 0 } and θn
= 0, of the square wave with period T = 1, let’s plot its Fourier series with and without Lanczos
sigma factors (parameter smooth = true / false respectively)
-1.2
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
0 0.2 0.4 0.6 0.8 1 1.2
-1.2
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
0 0.05 0.1 0.15 0.2 0.25
As we can see, the series with Lanczos factors (pink line) shows a more stable and smooth
behavior. Note, however, that ringing has interesting physical consequences. Consider, for
example, a linear electric circuit in which, by means of a switch, a fast voltage transition is
created: then the response of the circuit will really exhibit an overshoot
Xnumbers Tutorial
192
Trigonometric double serie
= Serie2D_trig(x, y, Lx, Ly, Spectrum, [offset], [Angle])
It returns the trigonometric double serie
∑∑= =
+++=
N
n
M
m
mnyxmn ymxnafyxf
1 1
,,0 )cos(),( θωω
where
y
y
x
x
LL
π
ω
π
ω
2
,
2
==
The set
[ ] MmNna mnmn ...0,...0, ,, ==θ
is called “spectrum” of the function f(t). Each couple is called "harmonic".
The parameters “x” and “y” are vectors
The parameters “Lx” and “Ly” are the base lengths of the x-axis and y-axis.
The parameter "Spectrum" is an array of (n x 4) elements: containing the following information:
index n, index m, amplitude and phase.
That is, for example:
n m Amplitude Phase
0 1 1 45
2 1 0.5 -45
3 1 0.25 15.5
1 4 0.125 30
The optional parameter "offset" is the average level (default 0)
The optional parameter "Angle" sets the angle unit (RAD (default), DEG, GRAD)
The function f(x, y) is returned as an (N x M) array.
Use the CTRL+SHIFT+ENTER key to insert this function.
Example: Here it is a worksheet arrangement to tabulate a trigonometric serie f(x, y) having a
spectrum of max 4 harmonics
Xnumbers Tutorial
193
Discrete Convolution
Convol(f, g, h, [algo])
This function approximates the convolution of two sampled functions f(ti), g(ti)
∫
∞+
∞−
−= )()(* dvvtgvfgf
The parameters "f" and "g" are column-vectors
The parameter "h" is the sampling step.
The optional parameter "algo" sets the algorithm: 0 = discrete, 1 = linear (default), 2 =
parabolic.
The functions returns a vector with the same dimension of the two vectors f and g.
The convolutions is also called "Faltung"
Here are other examples of convolution plots
In the signals analysis the function f is called "input signal" x(t) and g is called "system impulse
response" h(t). The convolution f∗g is called "system responce" y(t)
The system behavior is reassumed in the following schema
Xnumbers Tutorial
194
If the system is described by the following differential equation
)()()(' txtykty =⋅+
which has an impulse response given by
k t
)( −
= eth
We will use convolution to find the zero input response of this system to the square signal of
period T = 1 and amplitude xmax = 1.5
For obtaining this gaph we have used a sampling step of ∆t = 0.02, but this value is not critical.
You can choose the size that you like in order to obtain the needed accuracy.
h(t)x(t) y(t)
In a linear system, the outputs signal y(t) depends by the input signal x(t)
and by the inpulse responce of the system h(t). That is:
y(t) = ∫ x(v) h(t-v) dv
Xnumbers Tutorial
195
Interpolation
Polynomial interpolation
=PolyInterp(x, xi, yi, [degree], [DgtMax])
=PolyInterpCoef(xi, yi, [degree], [DgtMax])
The first function performs the interpolation of a given set of points (x,y), and returns the value
at the point x.
The Input parameters Xi and Yi are vectors.
The parameter "degree" sets the interpolation polynomial degree (default = 1)
The optional parameter DgtMax sets the maximum digits of the multiprecision arithmetic. If
omitted or 0, the functions works in faster double precision.
The second function returns an array containing the coefficients of the polynomial interpolation.
Use CTRL+SHIFT+ENTER for inserting this function.
This function use the following popular Newton's formula:
∑ ∏
=
−
=






−+=
n
m
m
j
jm xxxxDyxp
1
1
1
11 )(),...()(
where D(x1,...xn) is the "divided difference", given by the following recursive formulas:
,....),(,),(
32
32
32
21
21
21
xx
yy
xxD
xx
yy
xxD
−
−
=
−
−
=
.....,
),(),(
),,(
31
3221
321
xx
xxDxxD
xxxD
−
−
=
m
mm
m
xx
xxDxxD
xxD
−
−
= −
1
211
1
),...(),...(
),...(
Example : Compute the sub-tabulation between a set of 6 given points (x, y) using a linear
polynomial and a 3rd
degree polynomial and a step of h = 0.2. Note that the given points are not
equidistant.
Xnumbers Tutorial
196
Sub-Tabulation with Degree = 1 Sub-Tabulation with Degree = 3
0
0.2
0.4
0.6
0.8
1
1.2
0 2 4 6 8 10
y Interp
y
0
0.2
0.4
0.6
0.8
1
1.2
0 2 4 6 8 10
y Interp
y
If we wonder which is the 3rd
degree polynomial used for interpolating the point x = 3.2 we can
use the function PolyInterpCoef.
The interpolation polynomial will be
3
3
2
210)( xaxaxaaxp +++=
with the coefficients [a0, a1, a2, a3] returned from the function PolyInterpCoef
We can plot this polynomial by the function Polyn. The interpolation polynomial is shown in the
above graph.
Note that the function has selected the 4 points [x2, x3, x4, x5], discharging the first point x1 and
the last point x6, in order to get the most possible accurate interpolation.
Note. when the number of the points (x, y) is exactly degree+1, then the polynomial is
univocally determined no matter which the interpolation point is: we can pass any number x
that we like, for example x = 0.
In the following example we determine the 2nd
degree polynomial interpolating the given 3
points with 30 significant digits
Xnumbers Tutorial
197
Interpolation schemas
If the number N of the points (x, y) is exactly equal to degree+1, then the polynomial is
univocally determined; but if N > degree+1 there are several different polynomials that can be
used for interpolating a given point x. A popular method, adopted by the function PolyInterp is
called "sliding centered polynomial"
Linear interpolation
The linear interpolation is always performed between the nodes xi xi+1 where x ∈[xi xi+1]
For degree > 1 the interpolation strategy is more complicated.
Odd degree polynomial (example degree = 3)
The 3rd
degree interpolation is performed between the nodes {xi-1 xi, xi+1, xi+2 } where
x ∈[xi , xi+1]. At the first of the segment, the interpolation is performed between the nodes
{x1, x2, x3, x4 } where x ∈[x1 , x3]. At the end, the interpolation is performed between the
nodes { xN-3, xN-2, xN-1, xN } where x ∈[xN-2 , xN ].
Even degree polynomial (example degree = 2)
The 2rd
degree interpolation is performed between the nodes {xi-1 xi, xi+1 } and { xi, xi+1, xi+2 }
where x ∈[xi , xi+1]. The first set gives the left interpolation polynomial while the second set
gives the right interpolation polynomial. In the range x ∈[xi , xi+1] the final interpolation is
obtained taking the average of the two polynomials. At the first of the segment, the interpolation
is performed between the nodes {x1, x2, x3 } where x ∈[x1 , x2]. At the end, the interpolation is
performed between the nodes { xN-2, xN-1, xN } where x ∈[xN-1, xN ].
The average interpolation schemas adopted for even degree polynomials can often increase
the gloabal accuracy but, on the other hand, it reduces the efficence.
This example helps to explain this trick.
Assume to have the data set composed by 4
points; we want to perform the parabolic
interpolation of a point y(x) where 0.6 ≤ x ≤ 1
and y(0.6) = y2 and y(1) = y3
i x y
1 0.2 0.163746
2 0.6 0.329287
3 1 0.367879
4 1.4 0.345236
Note that the given points belong to the function
x
exxy −
=)(
xi, xi+1
xi-1 xi, xi+1,
Left polynomial
xN-2, xN-1, xNx1, x2, x3 xi, xi+1, xi+2
Right polynomial
average interpolation
xi-1 xi, xi+1, xi+2 xN-3, xN-2, xN-1, xNx1, x2, x3, x4
Xnumbers Tutorial
198
Being the degree = 2, we can build
two interpolation polynomials using
the first 3 points and the last 3
points.
We have thus the left interpolation
polynomial PL(x), covering the
range [0.2, 1], and the right
interpolation polynomial PR(x)
covering the range [0.6, 1.4]
The intersection range is [0.6, 1]
that is just the range to interpolate
y = -0.1914x2
+ 0.4027x + 0.1566
y = -0.3967x2
+ 0.7312x + 0.0334
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6
If we take the average
PM(x) = [PL(x)+ PR(x)] / 2
We obtain a new 2nd
degree
polynomial interpolating the points
between [ 0.6 , 1]
Observe that it satisfys the nodes
constraints:
PM(0.6) = y2 , PM(1) = y3 0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6
If we compute the absolute errors |y(x) - PR(x)| , |y(x) - PL(x)|, |y(x) - PM(x)| in the range
0.6 ≤ x ≤ 1, we observe a significant error reduction for the average polynomial, as shown in
the following graph.
0
0.001
0.002
0.003
0.004
0.005
0.006
0.4 0.6 0.8 1 1.2
average
left
right
In this case the average-parabolic polynomial accuracy is comparable with the cubic
polynomial.
0
0.0004
0.0008
0.0012
0.0016
0.4 0.6 0.8 1 1.2
cubic
average
But we have to consider that the simple cubic interpolation schema is more efficient then the
average-parabolic. Generally odd degree interpolation schemas are preferable.
Xnumbers Tutorial
199
Interpolation with continued fraction
Fract_Interp_Coef(xi, yi, [DgtMax])
Fract_Interp(x, xi, coeff, [DgtMax])
These functions perform the interpolation with the continued fraction method.
Given, for example, a set of 5 points
xi = [ x0, x1, x2, x3, x4] , yi = [ y0, y1, y2, y3, y4 ]
the function Fract_Interp_Coef returns the coefficients vector [ a0, a1, a2, a3, a4 ] of the
continued fraction expansion given by the following formula:
4
3
3
2
2
1
1
0
0
a
xx
a
xx
a
xx
a
xx
ay
−
+
−
+
−
+
−
+≅
The function Fract_Interp returns the interpolate value y at the point x
The optional parameter DgtMax sets the maximum digits of the multiprecision arithmetic. If
omitted or 0, the functions works in faster double precision.
Example: find the continued fraction interpolation for the following (x, y) samples
These points are extracted from the following function
9.0
2
2
2
−
+
=
x
x
y
You can verify that the interpolation with these coefficients are better than 1E-14 for all x values
in the range [0.4 − 1.6]
Note also that this great precision is reached in spite of the pole (x ≅ 0.95) in the interpolation
range. The continued fraction interpolation is just suitable for interpolating rational functions.
The following graph shows very well how interpolate points fits the curve.
Xnumbers Tutorial
200
-50
-40
-30
-20
-10
0
10
20
30
40
50
0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3
y samples
y interp
Interpolation with continued
fraction
The pink dots are the given
knots.
The blue line is obtained by
interpolation
There is a pole at x ≅ 0.95
(red dotted line)
Note that the points outside
the range [0.5, 1.2] keep
also a good accuracy.
(extrapolation )
Example: Find an interpolation formula for the function tan(x) in the range 0 ≤ x ≤ 1.5 with no
more than 7 points.
The function tan(x) has a pole at x ≅ 1.57 , near to the upper bound 1.5; so its presence
suggests to adopt a fraction interpolation.
Assume to sample the function tan(x) at the
values (0, 0.2, 0.6, 1, 1.25, 1.45, 1.5).
The column A contains the knots of the
interpolations
In colum B we have inserted the
correspondent values of tan(x)
In column C we have computed the
coefficients of the fraction interpolation.
Using the function FracInterp we can interpolate any value between 0 and 1.5
obtaining the graph to the left. The second graph shows the absolute error in the given range.
You can verify that the interpolation is better than 1E-5 for any value x in the given range.
Xnumbers Tutorial
201
Interpolation with Cubic Spline
cspline_interp(Xin , Yin , Xtarget )
cspline_eval(Xin, Yin, Ypp, Xtarget)
These functions1
perform the natural cubic spline interpolation
Xin is the vector containing the x-values.
Yin is the vector containing the y-values..
Xtarget is the x value which we want to compute the interpolation
Xpp is the vector containing the 2nd
derivative
The cubic spline interpolation is based on fitting cubic polynomial curves through all the given
set of points, called knots
The cubic spline follows these rules:
• the curves pass through all the knots
• at each knot, the first and second derivatives of the two curves that meet there are
equal
• at the first and last knot, the second derivatives of each curve is equal to 0 (natural
cubic spline constraint).
The natural cubic spline has a continuous second derivative (acceleration). This characteristic
is very important in many applied sciences (Numeric Control, Automation, etc...) when it is
necessary to reduce vibration and noise in electromechanical motions. The drawback is that
cubic spline is less efficient than other interpolation methods.
The function cspline_eval is faster than cspline_interp, because it uses the information of the
2nd
derivatives and does not have to calculate them all over again like the cspline_interp does.
The 2nd
derivatives can be computed by the function cspline_pre (see next function)
Example:
Cubic Spline 2nd derivatives
cspline_pre(Xin , Yin)
This function Returns the cubic spline 2nd
derivatives at a given set of points (knots).
Xin is the vector containing the x-values.
Yin is the vector containing the y-values..
For n knots, it returns an array of n 2nd
derivative values. The first and the last values are zero
(natural spline constraint).
The 2nd
derivatives depend only by the given set of knots. So they can be evaluate only once
for the whole range of the interpolation. By cspline_eval we can compute faster interpolation.
1
These functions appear thanks to the courtesy of Olgierd Zieba
Xnumbers Tutorial
202
Example. Perform the sub-tabulation with ∆x = 0.1 of the following table
The given table is in the range
A3:B13
In the adjacent column C we have
computed the 2nd
derivatives by
the function cspline_pre.
Note that this function returns a
vector of 11 values. It must be
inserted with the ctrl+shift+enter
keys sequence.
At the right we have set the new
table with step 0.1.
the value of F3 has been interpolate by the formula
= cspline_eval($A$3:$A$13; $B$3:$B$13; $C$3:$C$13; E3)
The other values are computed simply by dragging down the cell F3.
The following plot shows the knots and the cubic spline fitting
The points of the original table was extracted from the function y = [cos(x)]4
.
You can verify that the interpolation accuracy is better than 1% over the entire range.
Xnumbers Tutorial
203
Cubic Spline Coefficients
cspline_coeff(Xin , Yin)
This function1
returns the coefficients of the cubic spline polynomials
Xin is the vector containing the x-values.
Yin is the vector containing the y-values..
It returns an (n-1 x 4 ) array where n is the number of knots. Each row contains the coefficients
of the cubic polynomial of each segment s [as,3 as,2 as,1 as,0]
0,1,
2
2,
3
3, )()()( ssssssss axxaxxaxxay +−+−+−=
where s = 1, 2, (n-1)
Example. Find the cubic spline polynomials that fit the given knots
1
These functions appear thanks to the courtesy of Olgierd Zieba
Xnumbers Tutorial
204
2D Mesh Interpolation
=Interp_Mesh(TableXY)
This function performs the linear interpolation of a bivariate function given in a pivot table XY.
The x-values and y-values must be sorted but not necessarily equidistant. This function returns
an array. Let's see how it works.
Regularization
As we can see, the use of this function
is straight. Simply select the area
where you want to insert the new
table, insert the function Interp_mash
and pass the old table as parameter.
The function Interp_mesh returns the
equidistant-linear-interpolated array.
Or, in other words, it returns the
regularized table
Rescaling
We can obtain a sub-tabulated
function in an very fast way
Simply select a larger area
The function Interp_mesh
counts the cells that you have
selected and fill all the cells with
the linear interpolated values
In this case the given table has
5 x 4 = 20 values.
The new table has 9 x 7 = 63
values; therefore, there are 43
new interpolated values
Xnumbers Tutorial
205
Macro Mesh Fill
This macro, accessible from the menu Macro / Regression, is useful for completing a 2D mesh
by interpolation. Practically, given a (n x m) matrix with holes, it tries to recover the missing
data. We could say a special case of bi-dimensional linear regression
Using this macro is very simple. Select the matrix with holes and start the macro.
Example
Original data matrix complete filled.
It was generated by the following linear
formula
jia 32ij −=
the same data matrix with same missing
values (holes). Select the matrix (range
B13:H21) and start the macro
The macro will try to rebuild the missing
value (red) by linear interpolation, using
the data of the nearest nodes
Of course, this process gives exactly the
original matrix only in the linear case
For all other case we have an error:
Generally, it is small if also the ratio
between holes and filled nodes is small
and if the original data is smooth
Xnumbers Tutorial
206
Interpolation of tabulated data function
Given a tabulated data (x i , y i), i = 1...N, generally not equidistant, we have to interpolate the
function y for an arbitrary x value, where x1 ≤ x ≤ xN
The points (x i , y i) are called knots of the interpolation
Cubic Spline interpolation
The goal of cubic spline interpolation is to get a polynomial interpolation formula that is smooth
in the 1st
derivative, and continuous in the 2nd
derivative, within the interval and at each
boundaries.
This method ensures that the functions y(x), y’(x), and y’’(x) are equal at the interior node
points for adjacent segments. The cubic polynomials Pi(x) satisfie these constrains.
Pi(xi−1) = yi−1 for i = 2...N
Pi(xi) = yi for i = 2...N
P'i(xi) = P'i+1(xi) for i = 2...N−1
P''i(xi) = P''i+1(xi) for i = 2...N−1
One form to write the interpolation polynomials is:
P(x) = A Pi + B Pi+1 + C P’’i + D P’’i+1 , for i = 1...(N-1)
Where:
A = (xi+1 – x)/ (xi+1 – xi)
B = 1 – A
C = 1/6 (A3
– A) (xi+1 – xi)2
D = 1/6 (B3
– B) (xi+1 – xi)2
The 2nd
derivatives can be evaluated by the following linear equations
(xi – x i-1) P’’i-1 + 2 (xi+1 – xi-1) P’’i + (xi+1 – xi) P’’i+1 = Hi for i = 2...(N-1)
where:
Hi = 6[(Pi+1 – Pi)/(xi+1 – xi) – (Pi – Pi-1)/(xi – xi-1)] , P’’1 = 0 , P’’N = 0
That gives the following tridiagonal matrix system
2(x3 – x1) (x3 – x2) 0 0 ... 0 P’’2 H2
(x3 – x2) 2(x4 – x2) (x4 – x3) 0 ... 0 P’’3 H3
0 (x4 – x3) 2(x5 – x3) (x5 – x4) ... 0 P’’4 = H4
0 0 (x5 – x4) 2(x6 – x4) ... ... P’’5 H5
... ... ... ... ... (xN – xN-1) ...
0 0 0 ... (xN – xN-1) 2(xN – xN-2) P’’N-1 HN-1
Another common way to write the interpolation polynomial is:
P(x) = a3i (x – xi)3
+ a2i (x – xi)2
+ a1i (x – xi) + a0i , xi ≤ x < xi+1
for i = 1...(N-1)
Where the coefficients are:
a3i = (P’’i+1 – P’’i)/(xi+1 – xi)/6
a2i = P’’i / 2
a1i = (Pi+1 – Pi)/(xi+1 – xi) – (xi+1 – xi) (2 P’’i + P’’i+1)/6
a0i = Pi
The matrix of the system is tridiagonal, therefore can be solved in O(N) operations
Xnumbers Tutorial
207
We note also that its solution (P’’1 , P’’2 , ... P’’N ) depends only by the given knots, therefore the
2nd
derivatives can be evaluated only once for any interpolate.
This example shows how the interpolation spline works.
X Y
0 0
1 2
2.5 4
3 3
4 4
5 1
For these 6 knots we obtain 5 cubic polynomials having the following coefficients
Polynomials a3 a2 a1 a0 Range
1st spline 0.20148927 0 1.79851073 0 0 ≤ x < 1
2nd spline -0.8783764 0.60446781 2.40297854 2 1 ≤ x < 2.5
3rd spline 5.54708717 -3.348226 -1.7126588 4 2.5 ≤ x < 3
4th spline -3.0718353 4.97240473 -0.9005694 3 3 ≤ x < 4
5th spline 1.41436706 -4.2431012 -0.1712659 4 4 ≤ x < 5
In the graphs below we can see the interpolated points (dotted line) fitting the data points and
the cubic polynomials (green line) passing through the nodes of each segment. Each
polynomial interpolates inside the proper segment. That is: the 1st spline works for 0 ≤ x < 1,
the 2nd spline for 1 ≤ x < 2.5, and so on.
In the graphs below are shown the entire interpolation line (left) and the 1st
spline (right).
-2
-1
0
1
2
3
4
5
6
-1 1 3 5
spline Interp
knots
-2
-1
0
1
2
3
4
5
6
-1 1 3 5
spline Interp
knots
1st spline
In the graphs below are shown the 2nd
spline (left) and the 3rd
spline (right)
-2
-1
0
1
2
3
4
5
6
-1 1 3 5
spline Interp
knots
2nd spline
-2
-1
0
1
2
3
4
5
6
-1 1 3 5
spline Interp
knots
3rd spline
In the graphs below are shown the 4th
spline (left) and the 5th
spline (right)
Assuming to have to sub-tabulate with a step
∆x = 0.1 a given function known only in the
following 6 points
Note that these points are unequal spaced
Xnumbers Tutorial
208
-2
-1
0
1
2
3
4
5
6
-1 1 3 5
spline Interp
knots
4th spline
-2
-1
0
1
2
3
4
5
6
-1 1 3 5
spline Interp
knots
5th spline
Let’s examine the 1st
and 2nd
derivatives. We can compute them either analytically or
numerically using – for example -the following derivative formulas:
y’(xi) ≅ (yi+1 – yi-1)/2∆x y’’(xi) ≅ (yi+1 – 2 yi + yi-1)/ ∆x2
In both ways, we get the following graphs
Y'
-5
-4
-3
-2
-1
0
1
2
3
0 1 2 3 4 5
Y''
-10
-5
0
5
10
15
0 1 2 3 4 5
As we can see the 1st
derivatives is smooth and the 2nd
is continuous. This last feature is
particularly appreciated in many fields of engineering. Although this algorithm is less efficient
than other polynomial interpolation methods, it has the advantage of following the interpolated
curve without the spurious oscillations that other schemes can create
Xnumbers Tutorial
209
Other test functions
In our last example we have found that both methods can provide acceptable interpolation for
all range of x . Thus, there are same case, that the superiority of the spline interpolation is more
evident. Gerald [2] used the “bump” test case to illustrate problems with other interpolation
methods. Let’s see.
Interpolate the following knots
Y = (cos(x))10
, for x= -2 , -1 , -0.5 , 0 , 0.5 , 1 , 2
Plotting the interpolated values with a step of 0.1, we get the following graphs
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
-2 -1 0 1 2
cos(x)^10
cubic poly
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
-2 -1 0 1 2
cos(x)^10
cubic spline
The curves appear acceptable in both graphs. The second shows a closer fit near the points x
= 1 and x = -1 where are "knees" of the curve.
But matching the error plots, we see clearly the better accuracy of the spline interpolation.
-0.06
-0.04
-0.02
0
0.02
0.04
0.06
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
error cubic poly
error cubic spline
As we can see, the amplitude
error of the cubic polynomial is
much more than the spline.
We can show that an higher
order of the interpolation
polynomial, is even worst
In this case, the cubic spline is
the better choice
Xnumbers Tutorial
210
Continued fraction interpolation
Continued fractions are often a powerful ways of interpolation when we work near the functions
poles.
In XNUMBERS the continued fraction coefficients can be obtained by the function
FracInterpCoef and the interpolation value with FracInterp
Example. Interpolate the following dataset
y(x) = 1/(x+0.01)1/2
, for x = 0, 0.05, 01, 0.2, 0.5, 1 , with step ∆x = 0.1
In the graphs below we have plotted the interpolated values obtained with three different
methods: cubic polynomial, cubic spline and continued fraction
Note that y(x) have a poles in x = −0.01 very near to the node x = 0
0
2
4
6
8
10
12
0 0.2 0.4 0.6 0.8 1 1.2
y
knots
0
2
4
6
8
10
12
0 0.2 0.4 0.6 0.8 1 1.2
y
cubic
poly
0
2
4
6
8
10
12
0 0.2 0.4 0.6 0.8 1 1.2
y
cubic spline
0
2
4
6
8
10
12
0 0.2 0.4 0.6 0.8 1 1.2
y
fraction
We can see a good general accuracy except for the final part of the polynomial interpolation
method. In this case, the worst accuracy is concentrated where the function is more flat, but,
surprisingly, this perturbation is due to the distant pole in x = -0.01. We note also that both
spline and fraction methods keep a good accuracy also for point external at the interpolation
range ( extrapolation for x >1 )
0.0001
0.001
0.01
0.1
1
10
0 0.2 0.4 0.6 0.8 1
poly
spline
fraction
Method avg. err.
Cubic spline 0.22
Cubic poly 0.19
Contined fraction 0.003
Xnumbers Tutorial
211
Differential Equations
Xnumbers contains functions for solving the following differential problem of the 1st
order with
initial conditions (Cauchy’s problem):
00 )(,),( ytyytfy ==′
and for solving the 1st
order ordinary differential system.
00 )(,),( yyyfy ==′ tt ⇔














=
=
=







=′
=′
=′
00
2002
1001
21
2122
2111
)(
....
)(
)(
,
)...,,(
....
)...,,(
)...,,(
nnnnn
n
n
yty
yty
yty
yyytfy
yyytfy
yyytfy
ODE Runge-Kutta 4
= ODE_RK4(Equations, VarInit, Step, [Par, …])
This function integrates numerically a 1st
order ordinary differential equation or a 1st
order
differential system, with the Runge-Kutta formula of 4th
order
),(
)5.0,5.0(
)5.0,5.0(
),(
34
23
12
1
khyhtfk
khyhtfk
khyhtfk
ytfk
ii
ii
ii
ii
++=
++=
++=
=
( )43211 22
6
kkkk
h
yy ii ++++=+
"Equations" is a math expression string containing the equation to solve. For a system It is a
vector of equations. Examples of correct equation definition are:
y’ = -2*y*x , v’ = 2*x-v^2+v , y1’ = -3*y1+y2+sin(10*t)
Each string may contain symbolic functions with variables, operators, parenthesis and other
basic functions.
The parameter "VarInit" is a vector containing the initial values. It has two values for two
variables [ t0, y0 ].
For a system with n+1 variables, "Varinit" is an (n+1) vector [ t0, y10 , y20, ….yn0 ].
The parameter "Step" is the integration step.
The optional parameter "Par" contains the values of other extra parameters of the equations.
Let’s see how it works with an example
Solve numerically the following Cauchy’s problem for 0 ≤ x ≤ 3
1)0(,2 =−=′ yxyy
We know that the exact solution is
2
x
ey −
=
We can arrange a worksheet like the following
Xnumbers Tutorial
212
As we can see, we have written in cell G5 the differential equation
y’ = - 2*x*y
In the range A5:B5 we have inserted the starting values of x and y. Note that we have written
the labels just above theirs values. Labels are necessary for the correct variables assignment
Finally, in the range A6:B6 - just below the starting values - we have inserted the ODE_RK4,
that returns the value y(0.2) = 0.9607893… with a good accuracy of about 1E-7 (compare with
the exact solution)
Tip: In order to get all other values,
select the range A6:B6 and simply
drag it down.
The cells below will be filled
automatically
Only remember to fix the constant
cells in the function with the $
symbol
=ODE_RK4($G$5,A5:B5,$F$5)
We have also added the column with the exact values in order to check the approximation
error. Both exact and approximated solutions are plotted in the following graph
0
0.2
0.4
0.6
0.8
1
1.2
0 0.5 1 1.5 2 2.5 3
y
y exact
The approximate solution (dots) fits very good the exact solution (pink curve).
Xnumbers Tutorial
213
If you need, you can include parameters inside the differential equation
Example. Solve the following differential problem
1)0(
' n
=
⋅⋅−=
y
yxky
where k = 2 and n = 1
Note that we have added the
labels "k" and "n" above the
cells D2 and E2. In this way,
the parser will correctly
substitute the value 2 for the
variable "k" and 1 for the
variable "n". in the differential
equation
Do not forget the labels "x"
and "y" in the cells A5 and B5
Example: Solve the following linear differential equation
0)1(,
1
' =⋅=+ yxay
x
y n
For n = 3 and a = 1. Rearranging, we get
0)1(,' =−⋅= y
x
y
xay n
Note the labels "a" and "n"
above the cells D2 and E2. In
this way, the parser will
substitute the value 1 for the
variable "a" and 3 for the
variable "n". in the differential
equation
Do not forget the labels "x"
and "y" in the cells A5 and B5
With the step h = 0.1, we have a numerical solution with a very good approximation comparing
with the exact solution )5/()1( 5
xxy −= , (better than 1E-6)
0.E+00
1.E-07
2.E-07
3.E-07
4.E-07
5.E-07
6.E-07
7.E-07
8.E-07
0 0.5 1 1.5 2 2.5 3 3.5 4
Relative Error
Xnumbers Tutorial
214
This function can be used to solve ordinary differential systems.
Example: Solve numerically the following differential system, where v(t) and i(t) are the voltage
and the current of an electric network



⋅+⋅−=
⋅−=
vii
viv
155'
7'



=
=
0)0(
10)0(
i
v
The computation can be arranged as
following.
Write the variables labels in the row 6. The
labels “v” and “i” must be the same that
you have written in the equations. Just
one row below, insert the starting values in
the same order.
Select the range A8:C8 and insert the
function ODE_RK4. The first step will be
returned.
Now select this row and drag it down for
evaluating all the steps that you need
The graph below shows the transient of v(t) and i(t) with good accuracy
0
2
4
6
8
10
12
0 0.4 0.8 1.2
v
i
Note that you can change the
step “h” in order to re-compute
the transient in a very fast and
quick way.
Optional constant parameters can be arranged. For example if you want to add a parameter R,
independent from the time “t”, write:
Constant parameters can be
written in any part of the
worksheet. You need only to add
the labels with the same symbols
with they appear in the differential
equations. In this case, we have
added the label "R" in the cell C1,
upon its values.
You can add as many optional
parameters that you like
Xnumbers Tutorial
215
ODE Multi-Steps
Another very popular method for integrating ordinary differential equations adopts the multi-
step Adams’ formulas. Even if a little formally complicated, they are very fast, and adapted to
build a large family of ODE integration methods
The multi-step Adams’ formulas can be generally written as:
( )10121
1
11 '...''' +−−−−
=
+−−+ ⋅+⋅+⋅+=⋅+= ∑ NiiNiNi
N
k
kikNii yyy
M
h
yy
M
h
yy ββββ
( )20211
1
21 '...''' +−−+−
=
+−−+ ⋅+⋅+⋅+=⋅+= ∑ NiiNiNi
N
k
kikNii yyy
M
h
yy
M
h
yy ββββ
where ),(' iii ytfy = ihtti ⋅+= 0
The first formula generates the explicit formulas – also called predictor formulas.
The second formula generates the implicit formulas – also called corrector formulas.
The number N is the order of the formula. A formula of N order requires N starting steps. Of
course, formulas with high N are more accurate.
For N = 1 we get the popular Euler integration formulas
iii yhyy '1 ⋅+=+ Euler’s predictor (1 step)
( )iiii yy
h
yy ''
2
11 +⋅+= ++ Trapezoid formula corrector (1 step)
Theirs errors are given by
)2(2
2
1
yhe ≈ Error predictor 1st
order
)3(3
12
1
yhe −≈ Error corrector 2st
order
For N = 4 we get the popular Adams-Bashfort-Moulton predictor-corrector formulas
( )3211 '9'37'59'55
24
−−−+ −+−⋅+= iiiiii yyyy
h
yy Predictor (4 step)
( )2111 ''5'19'9
24
−−++ +−+⋅+= iiiiii yyyy
h
yy Corrector (4 step)
Theirs errors are given by
)5(5
720
251
yhe ≈ Error predictor 4th
order
)5(5
720
19
yhe −≈ Error corrector 4th
order
There are a large set of predictor-corrector formulas
Xnumbers Tutorial
216
Multi-step coefficents tables
The following tables list the coefficents for the Adams’ predictor-corrector formulas up to the 9th
order and relative errors
Multi-step Predictor coefficients
Ord. ⇒ 1 2 3 4 5 6 7 8 9 10
M 1 2 12 24 720 1440 60480 120960 3628800 7257600
β 0 1 -1 5 -9 251 -475 19087 -36799 1070017 -2082753
β 1 3 -16 37 -1274 2877 -134472 295767 -9664106 20884811
β 2 23 -59 2616 -7298 407139 -1041723 38833486 -94307320
β 3 55 -2774 9982 -688256 2102243 -91172642 252618224
β 4 1901 -7923 705549 -2664477 137968480 -444772162
β 5 4277 -447288 2183877 -139855262 538363838
β 6 198721 -1152169 95476786 -454661776
β 7 434241 -43125206 265932680
β 8 14097247 -104995189
β 9 30277247
Multi-step Corrector coefficients
Ord. ⇒ 1 2 3 4 5 6 7 8 9 10
M 2 12 24 720 1440 60480 120960 3628800 7257600
β 0 1 -1 1 -19 27 -863 1375 -33953 57281
β 1 1 8 -5 106 -173 6312 -11351 312874 -583435
β 2 5 19 -264 482 -20211 41499 -1291214 2687864
β 3 9 646 -798 37504 -88547 3146338 -7394032
β 4 251 1427 -46461 123133 -5033120 13510082
β 5 475 65112 -121797 5595358 -17283646
β 6 19087 139849 -4604594 16002320
β 7 36799 4467094 -11271304
β 8 1070017 9449717
β 9 2082753
Error coefficient
The general error is e ≈ -k⋅hOrd+1
y(Ord+1)
where k is given by the following table
Ord. ⇒ 1 2 3 4 5 6 7 8 9 10
predictor 0.5 0.41667 0.375 0.34861 0.32986 0.31559 0.30422 0.29487 0.28698 0.28019
corrector - -0.0833 -0.0417 -0.0264 -0.0188 -0.0143 -0.0114 -0.0094 -0.0079 -0.0068
The predictor-corrector algorithm
Usually the multi-step formulas, implicit and explicit, are used together to build a Predictor-
Corrector algorithm . Here is how to build the 2nd
order PEC algorithm (Prediction-Evaluation-
Correction).
It uses the Euler’s formula as predictor and the trapezoidal formula as corrector
Prediction Evaluation Correction
yp1 = y0 +h f(t0, y0) ⇒ f(t1, yp1) ⇒ y1 = y0 +h/2 [f(t0, y0) +f(t1, yp1)]
yp2 = y1 +h f(t1, yp1) ⇒ f(t2, yp2) ⇒ y2 = y1 +h/2 [f(t1, y1) +f(t2, yp2)]
yp3 =…. …. ….
The value y1 can be reused to evaluate again the function f(t1, y1), that can be used in the
corrector formula to obtain a more accurate value for y1.
Xnumbers Tutorial
217
If we indicate the first value obtained by the corrector with y1
(1)
and the second value with y1
(2)
we can arrange a new following schema
Prediction Evaluation Correction Evaluation Correction
yp1 ⇒ f(t1, yp1) ⇒ y1
(1)
⇒ f(t1, y1
(1)
) ⇒ y1
(2)
This is the so called PECEC or P(EC)2
schema.
The group EC can also be repeated m-times or even iterated still the convergence. In these
cases we have the schemas P(EC)m
and P(EC)∞
respectively.
Note that, for m >> 1 the final accuracy depends mainly by the corrector.
Let's come back to the PEC schema.
We note that, at the step, we use the value f(t1, yp1) to predict the new value yp2
We could increase the accuracy if we take the better approximation f(t1, y1).
The new schema becomes:
Prediction Evaluation Correction Evaluation
yp1 ⇒ f(t1, yp1) ⇒ y1 ⇒ f(t1, y1) ⇒
This schema is called PECE and it is used very often being a reasonable compromise between
the accuracy and the computation effort.
Using different schemas with different predictor-corrector formulas we can build a wide set of
algorithms for the ODE integration. Of course they are not equivalent at all. Same of them have
a high accuracy, others show a better efficiency and others have a better stability. This last
characteristic may be very important for long integration intervals. In fact, the most algorithms,
especially those with higher order, become unstable when the integration step grows over a
limit. Algorithms that are stable for any integration step (so called A-stable algorithms) are
much appreciated, but unfortunately they require implicit formulas that, generally, can be
solved only by iterative algorithms.
The 2nd
order trapezoid formula is one of the most popular A-stable formula.
Xnumbers Tutorial
218
Predictor- Corrector
= ODE_PRE(yn, f, h)
= ODE_COR(yn, fp, f, h)
These functions perform the integration of the ordinary differential equations with the popular
multi-step predictor-corrector Adams’ formulas
00 )(,),( ytyytfy ==′
The first function returns the predictor value yn+1,p while the second function returns the
corrector yn+1.
The parameter “yn” is the last point of the function y(t).
The parameter “f” is a vector containing the last N values of the derivative of y(t). That are the
last N-1 values of the corrector.
The parameter “fp”, only for the corrector, is the best approximation of the derivative of y(t) at
the step n+1. Usually it is provided by a predictor formula
The parameter “h” sets the integration step
PECE algorithm of 2nd
order
Now we see how arrange a PECE algorithm of 2nd
order to solve a the following differential
problem.
2)0(,' 2
=−= yxyy
Let's set in a cell that we like the integration step “h” and then the heading of the data table. We
set separate columns for predictor and corrector values
Build the first row.
Begin to insert the starting
values (x0, y0) in the cells A6
and B6 respectively, and the
formula evaluations of f(x,y) in
the cell C6 and E6. The
corrector value is set equal to
the starting value B6
The second row is a bit more
complicated. Let’s see.
Select the first row A6:E6 and
drag it down one row. This will
copy the formula for fp and fc
Insert in the cell A7 the
increment formula
xi+1 = xi+h
Now we have to add the predictor and corrector function
Xnumbers Tutorial
219
Insert in the cell B7
=ODE_PRE(yn, f, h)
“yn” is the last value of y(x).
contained in D6. “f” is the last
value of f(x,y) contained in E6.
“h” is the step B3.
Insert in the cell D7
=ODE_COR(yn, fp, f, h)
Where “yn” is the last value of
y(x). In that case is D6. “f” is
the last value of f(x,y), E6.
“fp” is the predicted. value of
f(x,y), C7 in this case.
“h” is the constan step.
Now the setting of the
PECE algorithm of 2nd
order
is completed. Select the
second row A7:E7 and drag
it down in order to calculate
the steps that you want.
The yP and yC values can be compared with the ones of the exact solution.
2
1
2
x
y
+
=
The differences:
)(
)(
iicic
iipip
xyyd
xyyd
−=
−=
are plotted in the graph at the right
We note clearly the characteristic
behavior of the predictor-corrector
algorithm.
-0.04
-0.02
0
0.02
0.04
0.06
0.08
0.1
0 1 2 3
err predictor
2nd PECE
The second formula refines the approximation of the first one.
The final accuracy of PECE algorithm is practically the accuracy of the corrector
Xnumbers Tutorial
220
PECE algorithm of 4th
order
Now we solve the above differential equation with a 4th
order PECE algorithm using the 4 steps
Adams-Bashforth-Moulton formulas
2)0(,' 2
=−= yxyy
To start this algorithm needs 4 steps. A good set of starting steps is:
x y(x)
0 2
0.2 1.9230769231
0.4 1.7241379310
0.6 1.4705882353
The first 4 rows of the PECE algorithm are built as shown in the previous example.
The first 4 values of yp and yc are
the same.
Now let's insert in the cell B10
=ODE_PRE(yn, f, h)
where “yn” is the last value of
y(x), D9 in that case.
“f” is a vector of the the last four
values of f(x,y), E6:E9 in this
case.
“h” is the step B3.
Insert in the cell D10
=ODE_COR(yn, fp, f, h)
where “yn” is the last value of
y(x). In that case D9.
“f” is a vector of the last 3 values
of f(x,y), E7:E9.
“fp” is the predicted value of
f(x,y), C10 in this case.
“h” is the step B3
T Now the setting of the PECE
algorithm of 4th
order is completed.
Select the 5th
row and drag it down in
order to calculate the steps you want.
he predictor-corrector error curves are
shown in the following graph
-0.01
-0.005
0
0.005
0.01
0.015
0 0.5 1 1.5 2 2.5 3
ep
4th PECE
We do not investigate here how to get the extra 3 values (they
could comes by Runge-Kutta method or by Taylor series
approximation). The only thing that we have to point out is
that these values must be sufficiently accurate in order to
preserve the global accuracy of the algorithm
Xnumbers Tutorial
221
In order to compare the accuracy of the solutions of the this algorithm with the 2nd
order
algorithm of the previous example let's draw both the error curves in a same graph
-0.006
-0.004
-0.002
0
0.002
0.004
0.006
0.008
0.01
0 0.5 1 1.5 2 2.5 3
4th PECE
2nd PECE
As we can see, the 4th
order algorithm is evidently more accurated then the 2nd
order. On the
other hand, the first one requires an extra work for providing 3 starting points.
ODE Predictor-Corrector 4
= ODE_PC4(Equations, VarInit, Step, [Par, …])
This function integrates numerically an ordinary differential equation of 1st order or differential
system of 1st order, with the 4th order P(EC)^2 schema predictor-corrector of Adams-
Bashforth-Moulton.
The input arguments are identical to the function ODE_RK4
The function can return all the solution points in an array. Before inserting the function, select
as many rows that you want to fill. For example if you select a (p x n+1) array , the function will
return p solution points of a n x n differential system.
Let’s see how it works with an example



−=
−−−=
wvw
wvvvv
'
)21)(1('



−=
=
1)0(
0)0(
w
v (FitzHug-Naguno model)
Insert the equations in the cell A2 and A3. Insert the starting point [t0, v0, w0 ] = [0, 0, -1] in the
range A7:C7
Select the range A8:C167 in order to get simultaneously 160 integration points and insert the
function ODE_PC4 by the CTRL+SHIFT+ENTER sequence.
Xnumbers Tutorial
222
The following plot shows the obtained functions v(t) and w(t) for 0 <= t <= 8
The 4th
order P(EC)2
schema predictor-corrector of Adams-Bashforth-Moulton is detailed in the
following flow-graph
The initial extra 3 steps, necessary for starting the predictor-corrector algorithm, are calculated
by the function ODE_RK4 requiring 4 function evaluations for step. After that, the PC schema
require only 2 function evaluations for step. Conventionally we measure the effort (EF) counting
the number of function evaluation for n steps.
EF = 12+2*n (for PC schema), EF = 4*n (for RK algorithm)
For n >> 1 the effort reduction of the P(EC)2
schema is about 50%
Compute the first 3
steps by the 4th
order
Runge-Kutta algorithm
Starting
x0, y0 , h
Starting points
x0, y0
x1, y1
x2, y2
x3, y3
Compute the next step
by 4th
order by the 4
th
order Predictor
Evaluate the function
fp = f (x4, yp)
yp
Refine the step by the
4
th
order Corrector
fp
Evaluate the function
fc = f (x4, yc)
yc
P
E
C
E
Refine the step by the
4th
order Corrector
fc
y4
right-shift the
points and repeat
x1, y1
x2, y2
x3, y3
x4, y4
C
P (EC)2
Predictor-Corrector
schema of 4th
order
Xnumbers Tutorial
223
ODE Implicit Predictor-Corrector
ODE_PC2I(Equations, VarInit, Step, [Par, …])
This function integrates numerically an ordinary differential equation of 1st order or a differential
system of 1st order, with the 2th order implicit Predictor-Corrector method. Thanks to its large
stability it is suitable for solving "stiff" problems.
The input arguments are identical to the function ODE_RK4
The function can return all the solution points in an array. Before inserting the function, select
as many rows that you want to fill. For example if you select a (p x n+1) array , the function will
return p solution points of a n x n differential system.
This function uses the 1st
order Euler formula as predictor and the 2nd
order trapezoidal formula
as the corrector.
),(1 nnnn ytfhyy ⋅+=+ Predictor (Euler formula)
( )),(),(2/ 111 +++ +⋅+= nnnnnn ytfytfhyy Corrector (Trapezoidal formula)
The second non-linear equation is started with the predicted value and then solved respect to
the variable yn+1 till the convergence.
This algorithm, despite its low order, exibits a large stability and, thus, it is suitable for solving
"stiff" problems
Let’s see how it works with an example
Solve numerically the following 2nd
order Cauchy’s problem for 1 ≤ x ≤ 7 and k = 100
0)1(2
2
=+++ yk
dx
dy
k
xd
yd
, 5.0)1(',1)1( −== yy
First of all, we transform the problem in a 1st order differential equations system taking
', 21 yyyy ==



+−−=
=
212
21
)1('
'
ykyky
yy



−=
=
5.0)1(
1)1(
2
1
y
y
A possible arrangement may be the following
We have written in cell A2 and A3 the
differential equations
In cell D3 we have inserted the k parameter
and in the cell E3 the h step
In the range A8:C8 we have put the starting
values for x and y1 and y2.
note that we have also added the labels "k" ,
"x", "y1", "y2" just above theirs values.
Labels are necessary for the correct
assignment in the symbolic equation.
Now select the range A9:C40 and insert the
function ODE_PC2I passing its arguments
The scatter plot of the 30 points (x, y1) is shown in the following graph
Xnumbers Tutorial
224
Compare the approximate solution (dotted line) with the exact one (pink continue line)
The fitting, even with only 30 points, looks good. If we try to solve this problem with the 4th
order
Runge-Kutta algorithm we have to choose a step less then 0.025 for avoiding the instability
and, thus, we need more than 250 points for reaching the same integration interval.
The Lotcka-Volterra Model
The following two-dimensional differential system






+−=
−=
yxaya
dt
dy
yxaxa
dt
dx
43
21
is called the Lockta-Volterra model or also "prey-predator" model. It is very useful in biology,
chemistry and many other fields. The numerical integration of this ODE family requires a stable
algorithm otherwise the result are not acceptable
The function x(t) and y(t), depending from the time, may represent different things. In a
biological models, for example, they simulate respectively the population of pray (rabbits) and
predator (foxes) at time t. The proportionality constants a1 , a2 , a3 , and a4 are positive. It is
known that its solution leads to oscillate steady-state.
. Let's see with a practical example



+−=
−=
xyxy
xyxx
25'
22'



=
=
1)0(
1)0(
y
x
Insert the equation defintion in the cells
A1 and A2 , the step in the cell C2 = 0.02
and the starting values [0, 1, 1] in the
range A6:C6
Do not miss the labels "t", "x", "y"
Then, select the range A7:C407 and
insert the function ODE_PC2I with the
ctrl-shift-enter keys sequence
The following graph at the left shows the result of the function x(t) and y(t). By performing the
scatter plot of x-y variable we have the steady-state plot at the right. It shows a closed loop, a
limit cycle.
Xnumbers Tutorial
225
0
1
2
3
4
5
6
0 2 4 6 8 10 12
x
y
0
1
2
3
0 2 4 6
We can easily change the zoom of the graph by changing the integration step h
Note that the oscillations are quite deep. The above system has two equilibrium points where
dx/dt = 0 and dy/dt = 0 simultaneously, that can be found solving the algebric system



=+−
=−
025
022
xyx
xyx
⇒



=
=
0
0
y
x



=
=
1
5.2
y
x
The non trivial solutions means that if the initial populations start from x0 = 2.5 and y0 = 1, then
there will be no oscillation and the population fraction x/y will be always constant in the time.
On the contrary, the populations become progressively to oscillate when the initial populations
are different from the equilibrium point [2.5 , 1].
Here same examples:
x(0) = 2.45 and y(0) = 1 x(0) = 2.4 and y(0) = 1
0
0.5
1
1.5
2
2.5
3
0 2 4 6 8 10 12
x
y
0
0.5
1
1.5
2
2.5
3
0 2 4 6 8 10 12
x
y
Xnumbers Tutorial
226
Differential Systems
OD Linear System
= ODE_SYSL(A, y0, step, [b]))
This function integrates numerically a system of ordinary linear differential equations of 1st
order with constant coefficients, starting from an initial value. For example, the general form of
a 3x3 linear differential system is:





+++=
+++=
+++=
31331321313
21231221212
11131121111
'
'
'
byayayay
byayayay
byayayay
where all the coefficients aij and bi are constant.
Such system can be put in the following handy matrix form.
[ ] byAy +⋅='
where










=
333231
232221
131211
aaa
aaa
aaa
A
[ ]T
yyyy 321 ,,=
[ ]T
bbbb 321 ,,=
and with the initial condition:
[ ]T
yyyy
)0(
3
(0)
2
(0)
10 ,,=
The constant term b is optional. If omitted the system is called "homogeneous"
This function uses the exponential expansion method that, for this kind of differential systems,
is both accurate and efficient.
The function returns an (n x m ) array containing all the nodes of the integration: m is the
number of equations; n is the numbers of the nodes. The function automatically sets n equal to
the rows of the range that you have selected before inserting it.
Let's see how it works practically
Solve the following homogeneous differential system with constant coefficients
[ ] yAy ⋅='
where










−−
−
−−
=
122
16616
191020
A [ ]T
y 0,0,10 =
For 0 ≤ x ≤ 4 and h = 0.05
The numerical solution can be arranged as in the following worksheet
The initial values are in the first row (range B7:D7). The column "x" was added only for clarity
but it is not indispensable at all. Select the range B8:D87, than insert the function ODE_SYSL
giving the suitable parameters A, y0, h. Then press CTRL+SHIFT+ENTER
Xnumbers Tutorial
227
All the 240 cells will be filled with the nodal solutions of y1, y2, y3.
The scale can be esily arranged simply by changing the parameter h
-1
-0.5
0
0.5
1
1.5
0 0.5 1 1.5 2 2.5 3 3.5 4
y1
y2
y3
-1
-0.5
0
0.5
1
1.5
0 0.25 0.5 0.75 1
y1
y2
y3
h = 0.05 h = 0.012
Compare with the exact solutions





+−=
−=
++−=
−−
−−
−−−
xx
xx
xxx
eey
eey
eeey
2
3
102
2
102
1
22
22
22
High order linear ODE
The function ODE_SYSL can also be used to solve high order linear ODE with constant
coefficients, that in general can be written as
byayayayay n
n
n
=++++ −
− 012
)1(
1
)(
'''....
with the initial conditions
00 )( yxy = , ')(' 00 yxy = , '')('' 00 yxy = , ,
)1(
00
)1(
)( −−
= nn
yxy
As known, such ODE can be transformed into a linear differential system of 1st
order.
















+
































−−−−
=
















−
−
−
by
y
y
y
aaaay
y
y
y
n
n
nn
n 0
0
0
1000
0100
0010
'
'
'
'
1
2
1
1210
1
2
1
LL
M
M
KOKKK
M
M
L
having the initial conditions
[ ] [ ]TnT
n yyyyyyyyy )0(,)0('',)0(',)0()0(,)0(,)0(,)0( 1
3210
−
==
Xnumbers Tutorial
228
For example assume tha you have to calculate the solution of following IVP problem:
05'9''5''' =+++ yyyy , with 2)0( =y , 3)0(' −=y , 4)0('' =y
Introducing the axiliary variables y1 = y , y2 = y' , y3 = y'' , we get the following equivalent
differential system










⋅










−−−
=










3
2
1
3
2
1
595
100
010
'
'
'
y
y
y
y
y
y
with





=
−=
=
4)0(
3)0(
2)0(
3
2
1
y
y
y
Observe that the last row of the matrix contains the coefficients of the given ODE with the
opposite sign; besides of that, it has only all "1" in the upper subdiagonal.
Insert the initial values in the first row (range B10:D10). The column "x" was added only for
clarity but it is not indispensable at all.Select the range B11:D40, where you want to otput the
results and insert the function ODE_SYSL giving the suitable parameters: A, y0, h.
Then press CTRL+SHIFT+ENTER
The selected area will be filled with the numerical solution of the given system
0
0.5
1
1.5
2
2.5
0 1 2 3 4 5 6
y1
Exact
Observe that the solution y1(x) is also the solution y(x) of the given ODE. Compare with the
exact solution
xeexy xx
cos)( 2−−
+=
Of course the above differential system can be also solved with other methods.
In order to show the accuracy of the
exponential method we put in a graph the
average relative error
e(x) = |yi - y(xi)| / | y(xi)| ,
obtained in the same condition, with 3
different methods: Exponential, Euler,
and Runge-Kutta 4.
The graph is eloquent. The error of the
Exponetial method is several times more
accurate then the others
1E-16
1E-14
1E-12
1E-10
1E-08
1E-06
0.0001
0.01
1
100
0 1 2 3 4 5 6
Euler
RK 4
Expo
Clearly it takes advantages using dedicated methods for linear differential equations.
Another important feature of the exponential method is its high stability
Let' s try the following test stiff system



−−=
=
212
21
2120
'
yyy
yy



−=
=
6770293.0)4.0(
6706555.0)4.0(
2
1
y
y
Xnumbers Tutorial
229
The exact solution is




−−=
+=
−−
−−
xx
xx
eey
eey
20
2
20
1
20
In the following graph we shows the numerical result obtained with three different integration
methods, Exponential, Runge Kutta of 4th
order and Euler, using different integration steps
The Expo method reaches the integration length of about x = 19 with a step h = 0.4
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
0 5 10 15 20
Expo method
h = 0.42
The Runge-Kutta method, using a step h = 0.14 gives accurate only for x < 3; after that the
solution begins to diverge.
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
0 5 10 15 20
Runge Kutta 4
h = 0.14
The Euler method, with a step h = 0.11, begins to oscillate even from the first steps. At this step
the Euler method is completely unstable.
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
0 5 10 15 20
Euler method
h = 0.11
As we can see the stability step of the Exponential method is here about 3 times greater than
the others methods.
Xnumbers Tutorial
230
ODE for integral function solving
The integral function y(x) that we want to
calculate is defined as
Taking the derivatives of both sides and
remembering Leibniz's rule, we have
∫=
x
x
dxxfxy
0
)()( ⇔
)()(' xfxy = , 0)( 0 =xy
As we can see, the computing of any integral function is equivalent of solving an ODE.
Example. Plot the integral function of
x
exxf −
=)(
Insert in the cell A1 the expression
y' = x*exp(-x)
Choose a suitable step, for example h = 0.15.
Select the range A7: B54 and insert the
function ODE_RK4 with crtl+shift+enter
In the adjacent column C we insert the
integration function f(x) = x*exp(-x)
Now select all the range A7:C54 and plot
together.
The result is shown in the following graph
0
0.2
0.4
0.6
0.8
1
1.2
0 2 4 6 8
f(x)
∫f(x)
Comparing with the exact integral function
x
exxy −
+−= )1(1)( , we have obtained a good
solution with an average error of about 5E-7. For this example we have taken about 50 points.
Remark. This method works fine when the function f(x) is smooth together with its derivative
and has no singularity in the integration range. Otherwise we have to choose some quadrature
algorithm as the double exponential method (see chap Integration for further details)
Xnumbers Tutorial
231
Example. Solve the following linear differential equation of 3rd
order.
029'33''5''' =+++ yyyy with 2)0( =y , 3)0(' −=y , 20)0('' −=y
This linear equation with constant terms can be transformed into the equivalent differential
system:










⋅










−−−
=










3
2
1
3
2
1
53329
100
010
'
'
'
y
y
y
y
y
y
with





−=
−=
=
20)0(
3)0(
2)0(
3
2
1
y
y
y
Solving numerically this differential
system is very rapid.
Simply write the system matrix A in the
worksheet, for example, in the range
B5:D7. Insert the row starting values,
[2,-3,-20] in the range B10:D10, where
you want to begin the functions
tabulation. Choose a suitable step that
you like, for example h = 0.05
Then, select the range B11:D60 and
insert the function
=ODE_SYSL(B5:D7;B10:D10;B3)
And give the ctrl+shift+enter key
sequence.
The selected cells will be automatically filled with the calculated solutions.
Compare with the exact solution
)5cos(2
xeey xx −−
+=
The following graph shows the exact solutions (continue lines) compared with the calculated
solutions (dotted lines). We note a global good fitting.
-5
-4
-3
-2
-1
0
1
2
3
4
5
0 0.5 1 1.5 2 2.5
y1
y2
y3
Xnumbers Tutorial
232
Macro ODE Solver
This macro performs the numerical integration of ordinary differential equations systems (ODE)
using the Runge-Kutta-Fehlberg method of 4th order.
It is useful for solving ODE with initial values (Cauchy problem)
),(' yxfy = , 00 )( yxy = max0 xxx ≤≤
as well an ODE system
),(' yfy x= , 00 )( yy =x max0 xxx ≤≤
where [ ]T
nyyy ..., 21=y is the vector of depend variables
The macro works with equation definited by worksheet formulas (on site) or even with equation
definited by symbolic string. The macro implements a control of the local error and changes the
integration step consequently Therefore the precision can be set independently from the
tabulating step. The independence of the integration step from the tabulation step is the main
feature of this macro.
The macro implement a predefined simple input schema. Let's see how to use it with a simple
example
Example 2. Find a numerical solution of this simple differential system



−−=
=
212
21
54'
'
yyy
yy
with



=
=
2)0(
0)0(
2
1
y
y
and with 50 ≤≤ x
Arrange the worksheet reserving two columns for [y1 , y2 ] and the two adjacent columns for the
derivatives [y1', y2']. In the first column set a x-grid with 50 step of increment ∆x = 0.1.
Then select the area A4:E55 and start the macro ODE Solver.
Differential Equations links to the cells where are the equation formulas: = C5, = -
(4*B5+5*C5) .
Variables are the cells (x, y1, y2) referenced by the formulas; in that case A4:C5
Starting values indicate the starting cells of the table (they may or not may coincide with the
variables cells).
If we have followed the above schema, the input fields are correctly filled.
We have only to set the required precision: Err. Max = 1E-6
Optionally, if we want the macro to fill also the derivative columns, activate the check box
Press "Run" to start the RKF integration algorithm. After few seconds and about 1400 function
evaluations, the macro output its result. Each solution node has a relative error less then 1E-6.
Compare it with the exact solution
Xnumbers Tutorial
233
xx
eey 4
1
−−
−= xx
eey 4
2 4 −−
+−=
The following graphs shows the exact solution (pink line) and the calculated solution (dotted
line)
Solution y1(x) Solution y2(x)
Note the general good fitting. It is a remarkable result overall if we consider the relative few
points in the initial range where both functions have high swinging.
For further details see " ODE tutorial" by Foxes Team, 2006
Macro ODE - Slope Grid
This macro generate and visualize the slope y'(x) of a 1st
ODE solution over a rectangular
domain. It is didactically useful for studying the 1st
order differential equation ),(' yxfy =
For example we have to study the following equation
x
eyy −
=+' ⇒ yxey x
−= −
'
Therefore we have to plot the slope given by the bivariate function yeyxf x
−= −
),( in a
suitable rectangular domain, for example in D = {-2 ≤ x ≤ 2 , {-2 ≤ y ≤ 2 }
For using this macro, set the variables x and y in
adjacent cells, for example I4 and J4. Then, in the
adjacent right cell K4, insert the formula defining
y'. thus: = EXP(-I4)-J4
Select the range I4:K4 and start the macro ODE Slope Field
Option Tab
The Grid number, from 4 to 24, set the
density of the grid. The dots check box
adds the grid points to the plot. The axes
check box adds the x-y axes
Xnumbers Tutorial
234
In the Axes tab we can set the domain D. Click the "Draw" button to generate the plot
The macro generates a graphical object
showing the slope y' of each point of the
grid.
Every solution y(x) of the differential
equation follows the direction of the slope
field. Therefore the field gives a global
view of the solutions crossing the given
domain.
For example, the solution crossing the
point (0,0) is xey x−
= ( red line)
We can see that this solution evolves
following the slope directions. In this case,
it never crosses the slope in any point.
Other interesting examples
xyy 2' −= 2
1' yxy −=
Xnumbers Tutorial
235
Nonlinear Equations
Bisection
=Zero_bisec(a, b, func, [step], [param])
Approximates the zero of a monovariable function f(x) with the bisection method
0)( =xf
This function needs two starting points [a, b] bracketing the zero.
Parameter "func" is a math expression string containing the symbolic function f(x)
Examples of correct function definitions are:
-2*ln(x) , 2*cos(x)-x , 3*x^2-10*exp(-4*x) , etc.
The optional parameter "step" sets the maximum number of steps allowed. If omitted the
function iterates still the convergence. Step = 1 is useful to study the method step-by-step
Param contains labels and values for parameters substitution (if there are)
At the first step, the function returns a new segment
[ a1, b1] where a1 < x0 < b1
At the second step, the function return a new segment
[ a2, b2] where a1< a2 < x0 < b2 < b1.
After several iterations, the interval [an , bn ], with n>>1, will be very closed to the value x0
Example: Find the approximated zero of the following equation and show the first steps of the
bisection method.
05
)(log
2
)(log3
10
10 =−+⋅
x
x
The plot indicates two zeros: one trivial
x = 10 and another in the interval 2 < x < 9
Starting the algorithm with a = 2
and b = 9 we get
x0 = 4.64158883361278
The root approximates the exact
zero x0 = 1001/3
with an error < 1E-14
Xnumbers Tutorial
236
We can also solve this equation step-by-step in order to investigate how this algorithm works
As we can see, the convergence is quite low but very robust because the zero always remains
bracketed between the interval limits [a, b]. The error estimation is also very quick. Simply
taking the difference |b-a|
Secant
=Zero_sec(a, b, func, [step], [param], [DgtMax])
Approximates the zero of a monovariable function f(x) with the secant method
0)( =xf
This function needs two starting points [a, b] bracketing the zero.
This version accepts also a worksheet function.
The parameter "Func" is a math string containing the symbolic function f(x) or a range of two
adjacent cells containing the worksheet functions [ f(a) , f(b) ] (see example).
Examples of correct symbolic function definitions are:
-2*ln(x) , 2*cos(x)-x , 3*x^2-10*exp(-4*x) , etc.
The optional parameter "Step" sets the maximum number of iterations. If omitted, the function
iterates still the convergence. Step = 1 is useful for investigation about the algorithm.
"Param" contains labels and values for parameters substitution (if there are)
Both parameters "step" and "param" are ignored if funct is not a symbolic function.
The optional parameter "DgtMax" set the number of the precision digits. If omitted, the function
works in the faster double precision.
At the first step, the function returns a new segment
[ a1, b1] where a1 < x0 < b1
At the second step, the function return a new segment
[ a2, b2] where a1< a2 < x0 < b2 < b1.
After several iterations, the interval [an , bn ], with n>>1, will be very closed to the value x0
Use the CTRL+SHIFT+ENTER sequence to paste this function
Example: Find the approximated zero of the following equation and show the first steps of the
secant method.
0)sin())2ln(3exp( =⋅−⋅− xx π
The plot indicates one zeros into the interval 0 < x < 0.5
Xnumbers Tutorial
237
-1
-0.5
0
0.5
1
1.5
0 0.2 0.4 0.6 0.8 1
exp(-6*x*Ln(2))-sin(pi*x)
Starting the algorithm with a = 0 and b = 0.5 we get x0 = 0.166666666666667
The root approximates the
exact zero x0 = 1/6 with
error < 1E-15
Let’s see now the iteration trace setting the parameter step = 1
As we can see this convergence is much faster than the one of the bisection method. On the
other hand, it is no guaranteed that the zero always remains bracketed into the interval.
Zero finder for worksheet function
Example we want to find the zero of the function f(x) = x − cos(x) , using the standard
worksheet function = COS(x). Arrange a worksheet like the following
Xnumbers Tutorial
238
Insert in A3:B3 the array function zero_sec(a, b, func) using the sequence Ctrl+Shift+Enter
In this case, none parameter is necessary because the function always returns one step of the
secant algorithm.
Now insert the functions in the range C3:D3 by simply dragging-down the range C2:D2
Insert the function =ABS(A3-B3) in E3. This is not necessary but it is usefull for checking the
convergence. Now, let's select the last row A3:E3 and drag it down until the convergence
shows the minimal error
We can compute this result also in multiprecision. In this case we have use, of course, the
equivalent
x-functions; in cell D2 = xsub(A4, xcos(A4)), E2 = xsub(B4, xcos(B4)) and in cell E3 =
ABS(xsub(A5, B5)).
In the range A3:B3 insert the array function {Zero_sec(A4, B4, C4:D4, , , 30)}.
The parameter DgtMax = 30 switches the function in multiprecision mode
Following the other steps we get the result below
As we can see, the final value is
x = 0.739085133215160641655312087673
that is the zero of the function f(x) = x − cos(x) with 30 significant digits
Xnumbers Tutorial
239
Derivatives
First Derivative
=Diff1(x, func, [lim], [param])
Approximates the first derivative of an univariate function f(x) at the given point x
)()(' xf
dx
d
xf =
The parameter “func” is a math expression string containing the symbolic function f(x)
Examples of function definition are:
-2*Ln(x) , 2*cos(x) , 3*x^2-10*exp(-4*x) , x^2+4*x+1 , etc.
The optional parameter “lim” (default = 0) sets the way how the limit approach to x. If lim = 1, it
approaches from the right; if lim = -1, it approaches from the left;
if lim = 0 , it approaches centrally. That is, it returns the following derivatives







=
=
=
=
+
+→
→
−
−→
)(')(lim
)(')(lim
)(')(lim
)('
0
0
0
xfxf
xfxf
xfxf
xf
h
h
h
"Param" contains labels and values for parameters substitution (if there are)
This function uses the following formulas to approximate each derivative
( ))4(3)3(16)2(36)(48)(25
12
1
)(' hxfhxfhxfhxfxf
h
xf −+−−−+−−≅−
( ))2()(8)(8)2(
12
1
)(' hxfhxfhxfhxf
h
xf +−++−−−≅
( ))4(3)3(16)2(36)(48)(25
12
1
)(' hxfhxfhxfhxfxf
h
xf +++−+++−≅+
Example. Evaluate numerically the left, right and central derivatives of the given function at the
point x = 0, and check if the given function is differentiable in that point
1||
)( 2
++
=
xx
x
xf
As we can see all derivatives are equal, so the function is differentiable in x = 0
Xnumbers Tutorial
240
Second Derivative
=Diff2(x, func, [param])
It approximates the second derivative of an univariate function f(x) at the given point
)()('' 2
2
xf
dx
d
xf =
The parameter "func" is a math expression string containing the symbolic function f(x)
Examples of function definition are:
-2*Ln(x) , 2*cos(x) , 3*x^2-10*exp(-4*x) , x^2+4*x+1 , etc.
"param" contains labels and values for parameters substitution (if there are)
Example: Evaluate the first and second
derivatives at the point x = 2 for the
following function
1
3
)( 2
+
+
=
x
x
xf
Gradient
=Grad(p, func, [param])
Approximates the gradient of a multivariate function f(x, y, z) at the given point






∂
∂
∂
∂
∂
∂
=∇
z
f
y
f
x
f
zyxf ,,),,(
The parameter "p" is the vector of the variables [x, y, z]
The parameter "func" is an expression string containing the function f(x, y, z). Examples of
function definition are:
-2*ln(x+3y), 2*exp(-x)*cos(3*t), 3*x^2-y^2+z^2, (x^2+y^2)^(1/3),etc.
For performance problem, the number of variables is restricted to 4, “x”, “y”, “z”, “t”. The
variables values must be always passed in this order.
"param" contains labels and values for parameters substitution (if there are)
Example. Evaluate the gradient of
the following function at the point
P(1, 1)
22
5
1
),(
yx
yxf
+
=
Xnumbers Tutorial
241
Jacobian matrix
=Jacobian (p, func, [param])
Approximates the Jacobian’s matrix of a multivariate vector-function F(x, y, z) at the given point
(x, y, z)










=
),,(
),,(
),,(
),,(
3
2
1
zyxf
zyxf
zyxf
zyxF


















∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
∂
=
z
f
y
f
x
f
z
f
y
f
x
f
z
f
y
f
x
f
zyxJ
333
222
111
,,
,,
,,
),,(
The parameter "p" is the vector of the variables [x, y, z]
The parameter "func" is an expression string containing the function f(x, y, z). Examples of
function definition are:
-2*ln(x+3y), 2*exp(-x)*cos(3*t), 3*x^2-y^2+z^2, (x^2+y^2)^(1/3),etc.
For performance problem, the number of variables is restricted to 4, “x”, “y”, “z”, “t”. The
variables values must be always passed in this order.
"param" contains labels and values for parameters substitution (if there are)
Example. Evaluate the Jacobian’s matrix of the following vector-function at the point P(1, 1)
2221
5
1
),,(
zyx
zyxf
++
= )2ln(),,(2 yxzzyxf +⋅= xyzzyxf 4),,(3 =
Hessian matrix
=Hessian (p, func, [param])
Approximates the Hessian’ matrix of a multivariate function f(x, y) at the given point p(x, y)












∂
∂
∂∂
∂
∂∂
∂
∂
∂
=
2
22
2
2
2
,
,
),(
y
f
xy
f
yx
f
x
f
yxH
The parameter "p" is the vector of the variables [x, y]
The parameter "Func" is an expression string containing the function f(x, y, z). Examples of
function definition are:
-2*ln(x+3y), 2*exp(-x)*cos(3*t), 3*x^2-y^2+z^2, (x^2+y^2)^(1/3), etc.
Xnumbers Tutorial
242
For performance problem, the number of variables is restricted to 4, “x”, “y”, “z”, “t”. The
variables values must be always passed in this order.
"Param" contains labels and values for parameters substitution (if there are)
This function returns a square a matrix (n x n) of the second derivatives
The accuracy of this function is about 1E-10
Example. Approx. the Hessian’s matrix of the following function at the point (2,1,1)
222
5
1
),,(
zyx
zyxf
++
=
Parameters. All the differential functions accept parameters into the string definition. They
must be different from the canonical variables "x", "y", "z", "t" but apart this, can have any name
that you like.
Example: Calculate the Hessian matrix of the function
ayx
zk
zyxf
++
⋅
= 22
),,(
for (x, y, z) = (1, -3, 4) and for parameters k = 1.5 and a = -1
Note the "param" range G2:H3: the parameters must be always passed with theirs labels "k"
and "a". On the contrary the variables do not need labels but must always be passed in the
order x, y, z.
Using the trick of the scaling for 243, the inverse of the determinant, we get an integer matrix,
that represent the numerator of the Hessian itself. Then, simplifying each element for 243, we
obtain the Hessian matrix in the following exact fractional form
Xnumbers Tutorial
243
Non-linear equation solving with derivatives
Derivatives play a strategic role in solving non-linear equation and non-linear system
The most efficient algorithms use the derivatives information in order to speed up the
convergence to the final solution. From the point of view of numeric calculus, derivatives are
rarely used because they tend to magnify the truncation error. This is general true and a many
rootfinding algorithm avoid the derivatives. In solving non-linear problem, however, the
derivatives can be very useful because they can greatly improve the convergence without
influence the final result accuracy, that depends only by the evaluation function f(x)
Let's see and example.
Solve the following equation xx sin2
= with an accuracy better then 1e-25.
First of all we build the function
xxxf sin)( 2
−=
and draw its plot. The point x where 0)( =xf is
the solution of the given equation We see that the
zero exists and it is near the point 1.
We note also that in the interval [0.5 1.5] the
function is monotonic
In this interval the Newton-Raphson iterative algorithm, starting from x = 1.5, should work fine.
...21,0,n
)('
)(
n
n
n1n =−=+
xf
xf
xx
To implement this algorithm we need the evaluation function f(x) with about 30 significant digits.
For that, it comes in handy the multiprecision function xeval. For the derivative we have to way:
computing the function f'(x) by hand and evaluating it by xeval or approximating the derivative
by the function diff1 in standard precision. Because we are a bit lazy and the derivatives is not
so immediate, we chose the second way. A simple spreadsheet arrangement may be the
following
As we can see the convergence is superb!. After few iteration the solution is
x ≅ 0.9496166887146629471509830317 with |f(x)| < 1e-28
This excellent result has been obtained in spite of the limited precision (1e-13) of the derivative.
The reason is simple: the accuracy of the derivative does not influence the final accuracy of the
root. We note that the derivative, after very few iterations, remains constant: we might
substitute this value with an even more approximated values, i.e. f' = 1.57, for all iterations. The
final accuracy will not change. We will need only more few steps, at the most.
Xnumbers Tutorial
244
But this method show its power overall for non-linear systems. For a 2 variables problem the
Newton-Raphson method becomes



=
=
0),(
0),(
yxg
yxf
⇒
n
1
n1n1n












−





=





−
++
g
f
gg
ff
y
x
y
x
yx
yx
The (2 x 2) matrix is the Jacobian calculated at the point (xn, yn). In Xnumbers it can be
evaluated by the function Jacobian
Example. Solve the following system




=−++
=−−+−
−
01
0625
yxe
xyx
yx
setting:
6),( 25
−−+−= xyxyxf
1),( −++= −
yxeyxg yx
the contour plots of the functions f = 0 and g = 0 show two
intersection points: one near the point (-1 2) and another
near (-1, -2)
The function f(x,y) and g(x,y) are
evaluated and converted in double
precision by the nested functions
=xcdbl(xeval(B4,B7:B8))
=xcdbl(xeval(B5,B7:B8))
At the begin, insert the starting point
(-1, 2) in the cells B7:B8.
The new point is calculated in the
cells E7:E8. Copy this range and re-
insert in the range B7:B8. At each
iteration the increments dx,dy of the
range E10:E11 becomes more and
more small.
Starting from (-1, 2) and (-1, -2) the iteration algorithm converges to the correct solutions
x y x y
-1 2 -1 -2
-1.0201151219 1.9698273171 -0.7964138633 -2.3053792051
-1.0196483063 1.9693084022 -0.8079928505 -2.2042117521
-1.0196480758 1.9693081215 -0.8107932120 -2.1997452584
-1.0196480758 1.9693081215 -0.8108021826 -2.1997248438
Xnumbers Tutorial
245
Conversions
Decibel
=dBel(A, [MinLevel])
Converts a positive number A into decibel
( )AA 10dB log20=
If zero, A is substituted with the value contained in the parameter "MinLevel" (default 1E-15)
Example
A A dB
1 0
0.5 -6.0206
0.1 -20
0.05 -26.021
0.01 -40
0.001 -60
0.0001 -80
0 -300
Base conversion
cvDecBin(DecNum) base 10 ⇒ base 2
cvBinDec(BinNum) base 2 ⇒ base 10
cvDecBase(DecNum, Base) base 10 ⇒ any base (2-16)
cvBaseDec(BaseNum, Base) any base (2-16) ⇒ base 10
baseChange(number, old_base, new_base) 1
any base (2 - 36) ⇒ any base (2 - 36)
xBaseChange 2
Any base (2 .. >1 million)
These functions perform the number conversion between different bases.
Example: Converts the decimal number n = 902023485 into bases 2 and 3.
cvDecBin(902023485) = 110101110000111100100100111101 (base 2)
cvDecBase(902023485, 3 )= 2022212022112121020 (base 3)
Example: Converts the hexadecimal number n = 35CFFF3D into decimal
cvBaseDec(35CFFF3D) = 902823741 (base 10)
You can also convert directly base-to-base, nesting two functions.
Example convert n = 35CFFF3D from base 16 into 8
cvDecBase(cvBaseDec(35CFFF3D,16),8) = 6563777475 (base 8)
For this scope you can also use the baseChange function
1
The function baseChange appears thanks to Richard Huxtable
2
The function xBaseChange appears thanks to Ton Jeursen
Xnumbers Tutorial
246
In spite of its digits limitation (15), this function has several interesting features
It converts any number into many different bases (up to 36). The digits greater then 9 are
indicated as A, B, C, D E, F, G, H, etc. It converts also decimal numbers. It formats the result
consistently with the source cell. Let's see how it works
The cell C4 is formatted with 7
digits and consequently its results
have the same format; the cell D4
is formatted with 2 decimals and its
result has the same format.
Multiprecision Base Conversion
= xBaseChange(xBC_OldNum, [xBC_NewBase], [deep])
This function1
converts a number from any base (2 .. >1 million)
xBC_OldNum is the number to convert in the following formats
3642455[7] (integer number in base 7)
10C2AE[16] (integer number in base 16)
1098414 (integer number in base 10)
213.02[4] (decimal number in base 4)
The oldnumber is assumed to be base 10; otherwise oldbase is provided between square
brackets [].
xBC_NewBase is the new base. If omitted it is assumed 10 as default.
If the base > 35 the letters are no more sufficients and the digits are represented as group of
digits separate with colon :
9:34:9:23[36] (integer number in base 36)
6:54:122:42:65[200] (integer number in base 200)
100:368.356:980[1000] (decimal number in base 1000)
deep is an optional parameter (0 to 50) setting the max number of decimals showed (default
50)
Example: xBaseChange("9:34:9:23[36]", 7)
Results in: 3642455[7]
Example: xBaseChange(1098414 , 16)
Results in: 10C2AE[16]
1
The function xBaseChange appears thanks to Ton Jeursen.
Xnumbers Tutorial
247
Example: xBaseChange(1098414 , 46)
Results in: 11:13:4:26[46]
Example: xBaseChange("1F0C2AE[16]")
Results in: 32555694[10]
Example xBaseChange(10092.102 , 2048 , 6)
Results in 4:1900.208:1835:16:786:884:1507...[2048]
Note that, as in the last case, the digits groups can have different lenght: one , two, or more
digits
If we wan to force all the groups having the same number of digits simply insert the new base
with prefix "0"
Example xBaseChange(10092.102 , "02048" , 6)
Results in 0004:1900.0208:1835:0016:0786:0884:1507...[02048]
Log Relative Error
= mjkLRE(q, c, NoSD)
= xLRE(q, c, NoSD, [DgtMax])
This function1
returns the log relative error (LRE) for an estimated value (q) and a certified
value (c), which has a specified number of significant digits (NoSD). The LRE is a measure of
the number of correct significant digits only when the estimated value is “close” to the exact
value. Therefore, each estimated quantity must be compared to its certified value to make sure
that they differ by a factor of less than two, otherwise the LRE for the estimated quantity is
zero.
Definition. The base-10 logarithm of the relative error is defined as:



<−=
>=
=
1|q|),)log(min(LRE
1|q|if0LRE
0cif
NoSDq





=
==
≤≤≠−−=
≠
lseif0LRE
qcifLRE
2|q/c|1/2andqcif),|)|/|log(|min(LRE
0cif
e
NoSD
NoSDccq
Example: Assume that you want to compare an approximate value with a 15 digits certified
value of π. LRE metric can show this in a easy way
Certified value C = 3.14159265358979
Approx. value Q = 3.14159265300001
mjkLRE(C, Q, 15) = 9.7
This means that two values are close for about 10 significant digits. LRE metric rejects non
significant digits. Look at this example:
Certified value C = 0.000133333333333333
1
These functions appear by courtesy of Michael J. Kozluk. This algorithm was first programmed into an Excel user
function, by Michael, in standard 32 bit precision. As it works fine also for comparing long extended numbers (NoSD>
15), we have now developed its multiprecision version xLRE().
Xnumbers Tutorial
248
Approx. value Q = 0.000133333333333311
mjkLRE(C, Q, 15) = 12.8
The two numbers appear exact up to the 17th
digit, but the relative error is about 1E-13
LRE is very useful when you work with long string of extended numbers.
For example, compare this approximation of "e" (Napier’s number)
Certified value C = 2.71828182845904523536028747111
Approx. value Q = 2.71828182845904523536028747135
xLRE(C, Q, 30) = 28.1
At the first sight it is hard to say, but the LRE function shows immediately a precision of about
28 digits
Xnumbers Tutorial
249
Special Functions
The computation of special functions is a fundamental aspect of numerical analysis in virtually
all areas of engineering and the physical sciences.
All these special functions have a high-fixed-precision. Because most of these special functions
are in the form of infinite series or infinite integrals, their numeric solutions are quite
complicated, and we have spent many times for selecting and testing many different algorithms
in order to achieve the highest possible accuracy in 32 bit arithmetic.
Error Function Erf(x)
errfun(x)
Returns the error function
∫
−
=
x
t
dtexerf
0
22
)(
π
Accuracy: about 1E-14 per x > 0
Exponential integral Ei(x)
exp_integr(x)
Returns the exponential integral
∫
∞
−
−
−=
x
t
dt
t
e
xEi )(
Accuracy: about 1E-14 for any x ≠ 0
This version1
accept also negative argument
For definition is
0)()( 1 >=− xxExEi
Exponential integral En(x)
expn_integr(x, n)
Returns the exponential integral of n-th order
dt
t
e
xE n
xt
∫
∞ −
−=
1
n )(
Accuracy: about 1E-14 for x> 0 and n > 0
1
This extension appears thanks to the courtesy of Hans Günter
Xnumbers Tutorial
250
Euler-Mascheroni Constant γ
xGm([Digit_Max])
Returns the Euler-Mascheroni gamma constant.
The optional parameter Digit_Max sets the maximum digits (default 30, max 415)






−+++=
∞→
)log(
1
...
3
1
2
1
1lim n
nn
γ
Example: compute the gamma constant with 40 significant digits
xGm(40) = 0.5772156649015328606084804798767149086546
Gamma function Γ(x)
xGamma(x, [Prec])
Returns the gamma function with precision up to 30 digits.
Definition:
∫
∞
−−
=Γ
0
1
)( dtetx tx
Optional parameter Prec sets the maximun precision from 15 (default) to 30 digits
When Prec = 15 the routine works in faster double arithmetic.
This routine uses the excellent Lanczos series approximation1






+
+




 ++
≅Γ ∑=
+ N
i
i
x
g
ix
c
c
e
gx
e
x
1
0
2
1 2
1
2
)(
π
where: g = 607/128, ci are the Lanczos' coefficients and 14 < N < 22.
Relative accuracy in double precision is better than 10^-14, (except very near to the poles x =
0, -1. -2, -3, etc.). In extended precision the accuracy is better then 1E-30
xGamma(5.1) = 27.9317537383684
xGamma(5.1, 30) = 27.9317537383683833586731052773
This function works also with large argument because it uses the multiprecision format to avoid
the overflow for arguments greater than 170.
Example,
x xgamma(x) Rel. Error
0.0001 9.99942288323162E+3 1.00E-15
0.001 9.99423772484596E+2 1.02E-15
0.01 9.94325851191507E+1 1.00E-15
0.1 9.51350769866874 9.33E-16
1 1 0
10 3.6288E+5 0
100 9.33262154439441E+155 5.64E-16
1
This high accuracy algorithm has been extracted form a very good note by Paul Godfrey, Intersil , C.2001
Xnumbers Tutorial
251
1,000 4.02387260077093E+2564 1.92E-15
10,000 2.84625968091705E+35655 1.58E-15
100,000 2.82422940796034E+456568 2.75E-15
1,000,000 8.26393168833122E+5565702 2.54E-15
Note that relative accuracy is better than 5E-15 in any case.
You can convert in double only the values with x ≤ 170, otherwise you will get #VALUE! (error).
You can manipulate these large values only by the "x-functions", or, separating mantissa and
exponent (see xsplit())
FACTORIAL: Thanks to its efficence and accuracy, this function can also be used to calculate
the factorial of a big integer number, using the relation
n! = Γ(n+1)
Example:
xfact(10002) = 2.84711361574652325360317551421E+35667 30 digits, slower
xgamma(10003) = 2.84711361574651E+35667 15 digits, faster
Log Gamma function
xGammaln(x)
xGammalog(x)
These function return the natural and decimal logarithm of the gamma function.
xgammaln(100000) = 1051287.7089736568948
xgammalog(100000) = 456568.45089997090835
Relative accuracy is better than 10^-(14+|log(x)|) for x>0
These functions are added only for compatibility with Excel and other math packages. They are
useful to avoid overflow in standard precision arithmetic for large arguments of gamma
function. However if you use directly the xgamma() and multiprecision arithmetic, you need no
more to use these functions.
Gamma quotient
xGammaQ(x1, x2)
Performs the division of two gamma functions.
q = Γ(x1) / Γ(x2)
Relative accuracy is better than 1E-14, for x1 > 0 and x2 > 0
Example: suppose you have to calculate for v =1,000,000 the following quotient
)(
)(
2
2
1
v
v
q
Γ
Γ
=
+
Taking x1 = 500,000.5 and x2 = 500,000 , we have easily
xgammaq(500000.5 , 500000) = 707.106604409874 (rel error = 5.96E-16 )
Note that if you have used the standard GAMMALN() function, you should have:
Xnumbers Tutorial
252
EXP(GAMMALEN(500000.5) - GAMMALEN(500000)) = 707.106604681849
(with a rel. error = 3.846E-10)
As we can see, In this case, the error is more than 500,000 times bigger that the previous one!
Gamma F-factor
xGammaF(x1, x2)
Returns the gamma factor of the Fischer distribution.






Γ⋅





Γ





 +
Γ
=
22
2
21
21
xx
xx
k
Relative accuracy is better than 1E-14, for x1 > 0 and x2 > 0
Digamma function
digamma(x)
Returns the logarithmic derivative of the gamma function
( )
)(
)('
)(ln)(
x
x
x
dx
d
x
Γ
Γ
=Γ=Ψ
Relative accuracy is better than 1E-14, for x > 0
Example
digamma(x) value rel. error
0.01 -100.560885457869 3.24E-15
0.1 -10.4237549404111 2.23E-15
1 -0.577215664901532 1.49E-15
10 2.25175258906672 4.92E-16
100 4.60016185273809 5.65E-16
1000 6.90725519564881 2.97E-16
Note that Ψ(1) = − γ (Eulero- constant)
Beta function
xBeta(a, b)
Returns the beta function
∫
−−
−=
1
0
11
)1(),( dtttbaB ba
Relative accuracy is better than 1E-14, for a > 0 and b > 0
Xnumbers Tutorial
253
Incomplete Gamma function
xGammaI(a, x, [sel])
Returns one of the following Incomplete Gamma functions specified by the "sel" parameter
(default 1)
Sel =1 Sel =2 Sel = 3 Sel = 4 (Tricomi)
∫
−−
=
x
ta
dtetxa
0
1
),(γ ∫
∞
−−
=Γ
x
ta
dtetxa ),( 1
)(
),(
),(
a
xa
xaP
Γ
=
γ
)(
),(
),(*
ax
xa
xa a
Γ
=
γ
γ
Relative accuracy is better than 1E-14 for : a > 0, 0≥x
Incomplete Beta function
xBetaI(x, a, b, [sel])
Returns one of the following Incomplete Beta functions specified by the "sel" parameter
(default 1)
Sel = 1 Sel = 2
∫
−−
−=
x
ba
x dtttbaB
0
11
)1(),(
),(
),(
),(
baB
baB
baI x
x =
where B(a, b) is the Beta function
Relative accuracy is better than 1E-14 for a > 0, b > 0. 10 ≤≤ x .
Combinations function
xcomb_big(n, k)
Returns the combination, or binomial coefficients, for large integer numbers
!)!(
!
,
nkn
n
k
n
C kn
−
=





=
Relative accuracy is better than 1E-14, for n >> 0 and k >> 0
This function uses the gamma function to calculate the factorials. It is much faster than xcomb
function. For this reason is adapted for large integer values (10,000 - 1,000,000)
xcomb(5000,2493) = 1.5627920156854189438574778889E+1503 (30 digits, slow)
xcomb_big(5000,2493) = 1.56279201568542E+1503 (15 digits , fast)
Xnumbers Tutorial
254
Bessel functions of integer order
BesselJ (x, [n]) Bessel function of 1° kind, order n: Jn(x)
BesselY (x, [n]) Bessel function of 2°kind, order n: Yn(x)
BesseldJ (x, [n]) First derivative of Bessel functions of 1° kind, order n: J'n(x)
BesseldY (x, [n]) First derivative of Bessel functions of 2° kind, order n: Y'n(x)
BesselI (x, [n]) Modified Bessel function of 1° kind, order n: In(x)
BesselK (x, [n]) Modified Bessel function of 2°kind, order n: Kn(x)
BesseldI (x, [n]) First derivative of mod. Bessel functions of 1° kind, order n: I'n(x)
BesseldK (x, [n]) First derivative of mod. Bessel functions of 2° kind, order n: K'n(x)
Relative accuracy is better than 1E-13, for x > 0 and n any integer
These routines1
have a high general accuracy. Look at the following example. We have
compared results obtained from our BesselJ with the standard Excel similar function
x J0(x) (BesselJ) Rel. Error J0(x) (Excel standard) Rel. Error
0.1 0.997501562066040 1.11E-16 0.997501564770017 2.71E-09
0.5 0.938469807240813 1.06E-15 0.938469807423541 1.95E-10
1 0.765197686557967 7.25E-16 0.765197683754859 3.66E-09
5 -0.177596771314338 2.66E-15 -0.177596774112343 1.58E-08
10 -0.245935764451374 1.06E-13 -0.245935764384446 2.72E-10
50 0.055812327669252 3.98E-15 0.055812327598901 1.26E-09
As we can se, the general accuracy is better than 200,000 times!
Cosine Integral Ci(x)
CosIntegral(x)
Returns the Cosine integral defined as:
dt
t
t
x
x
)cos(
)(ci ∫
∞
−=
Relative accuracy is better than 1E-13, for x > 0
Sine Integral Si(x)
SinIntegral(x)
Returns the sine integral defined as:
dt
t
tsin
x
x
)(
)(si
0
∫=
Relative accuracy is better than 1E-13, for x > 0
1
All these special functions are provided thanks to the FORTRAN 77 Routines Library for Computation of Special
Functions developed by Shanjie Zhang and Jianming Jin . The programs and subroutines contained in this library are
copyrighted. However, authors kindly gave permission to the user to incorporate any of these routines into his
programs.
Xnumbers Tutorial
255
Fresnel sine Integral
Fresnel_sin(x)
Returns the Fresnel's sine integral defined as:
dttsinxS
x
)()( 2
0
2
1
π∫=
Relative accuracy is better than 1E-13, for x > 0
Remember also the following relation
( )∫ ⋅=⋅
x
zkSdttk
0
2
)(sin where:
π
2
=k
Fresnel cosine Integral
Fresnel_cos(x)
Returns the Fresnel's cosine integral defined as:
dttxC
x
)cos()( 2
0
2
1
π∫=
Relative accuracy is better than 1E-13, for x > 0
Remember also the following relation
( )∫ ⋅=⋅
x
zkCdttk
0
2
)cos( where:
π
2
=k
Fibonacci numbers
xFib(n, [DgtMax])
Returns the Fibonacci's numbers defined by the following recurrent formula:
2n1nn21 ,2,1 −− +=== FFFFF
Example:
xFib(136) = 11825896447871834976429068427
xFib(4000) = 3.99094734350044227920812480949E+835
Xnumbers Tutorial
256
Hypergeometric function
Hypgeom(a, b, c, x)
Returns the Hypergeometric function
The parameter "a" is real, "b" is real, "c" is real and different form 0, -1, -2, -3 ...
The variable "x" is real with |x| < 1
Relative accuracy is better than 1E-14, for -1 < x < 1
The hypergeometric function is the solution of the so called Gaussian-hypergeometric
differential equation
( ) ( )( ) 011 =+′++−+′′− yabyxbacyxx
An integral form of the hypergeometric function is
( ) ( )∫
−−−−
−−
−ΓΓ
Γ
=
1
0
11
11
)()(
)(
).,,( dtxttt
bcb
c
xcbaF
abcb
More known is the series expansion that converges for !x| < 1
...
!6)2)(1(
)2)(1()2)(1(
!2)1(
)1()1(
!1
1).,,(
32
+
++
++++
+
+
++
++=
x
ccc
bbbaaax
cc
bbaax
c
ab
xcbaF
Special result are:
p
xxpF −
−= )1(),1,1,(
x
x
xF
)1ln(
),2,1,1(
+
=−
6.1),,,( 32
27
6
5
3
2
3
1
=F
Zeta function ζ(s)
Zeta(s)
The Riemann zeta function ζ(s) is an important special function of mathematics and physics
which is intimately related with results surrounding the prime number, series, integrals, etc.
Relative accuracy is better than 1E-14, for any s ≠ 1
It uses the fast Borwein formula.
Definition. For |s|>1 the function is defined as:
∑
∞
=
=
1
1
)(
k
s
k
sζ
Analytic continuation. It can be defined for 0< s <1 by the following analytic continuation:
∑
∞
=
−
−
−
−
=
1
1
1
)1(
21
1
)(
k
s
k
s
k
sζ
For s< 0 the function is defined by the following relation:
)()()cos()2(2)1( 2
1
ssss s
ζππζ Γ=− −
Xnumbers Tutorial
257
Same known exact results are: ζ(2) = (π2
) /6 , ζ(4) = (π4
)/90
Zeta function is very useful in computing series. Look at this example:
4
5
)2(
2
1
1
11
)2(
1
2
1
2
2
2
0
2
−=−−==
+
∑∑∑
∞
=
∞
=
∞
=
ζ
kkk kkk
So, the final result is (π2
)/6 - 5/4
Airy functions
= AiryA(x)
= AiryB(x)
= AiryAD(x)
= AiryBD(x)
Return the Airy functions A(x), B(x) and theirs derivatives A'(x), B'(x)
A(x) =
( )( )( ) 




 ++Γ
∑
∞
= 3
)1(2
sin3
!
1
3
1 3/1
0
3
1
3/2
π
π
n
x
n
n n
n
B(x) =
( )( )( ) 




 ++Γ
∑
∞
= 3
)1(2
sin3
!
1
3
1 3/1
0
3
1
6/1
π
π
n
x
n
n n
n
Variable x is real. Relative accuracy is better than 1E-14
Elliptic Integrals
= IElliptic1(φ, k)
= IElliptic2(φ, k)
Return the Jacobian Elliptic Integrals of 1st and 2nd kind
IElliptic1 = ∫ −
=
φ
θ
θ
φ
0
22
sin1
),(
k
d
kF
IElliptic2 = θθφ
φ
dkkE sin1),(
0
22
∫ −=
Variable φ is real, 0 < k < 1. Relative accuracy is better than 1E-14
Xnumbers Tutorial
258
Kummer confluent hypergeometric functions
= Kummer1(a, b, z)
= Kummer2(a, b, z)
They return the Kummer conflent hypergeometric functions of 1st and 2nd kind, also called
M(a, b, z), and U(a, b, z) respectively.
The confluent hypergeometric functions give the complete solutions of the confluent
hypergeometric differential equation.
0)(2
2
=−−+ aw
dz
dw
ab
dz
wd
z
having the complete solution
),,(),,( 21 zbaUqzbaMqw +=
The parameters "a" and "b" can be positive or negative but always different from 0, -1, -2, -3 ...
The variable z must be positive for Kummer2
For a > 0 and b > 0, the confluent hypergeometrix functions have an integral representation
(Abramowitz and Stegun 1972, p. 505)
∫
−−−
−
Γ−Γ
Γ
=
1
0
11
)1(
)()(
)(
),,( dttte
bab
b
zbaM abazt
∫
∞
−−−−
+
Γ
=
0
11
)1(
)(
1
),,( dttte
a
zbaU abazt
a b z M(a,b,z) U(a,b,z)
0.5 0.2 1 5.85803416181 0.70928623024
0.5 0.2 5 601.287114305 0.40255172241
0.5 0.2 10 112016.644576 0.29849708947
0.5 0.2 20 3063046479.13 0.21689337800
0.5 0.2 30 7.6396125E+13 0.17882728338
8.2 1.4 1 41.1797940694 1.39280467E-06
-0.9 0.4 10 -345.535973205 7.72952303674
Xnumbers Tutorial
259
Integral of sine-cosine power
= IntPowSin(n, x)
= IntPowCos(n, x)
These functions return the integral of sine and cosine raised to the n integer power
∫=
x
n
dttnxSPI
0
)(sin),( ∫=
x
n
dttnxCPI
0
)(cos),(
Variable x ≥ 0 is real, n = 0, 1, 2, 3 ... is integer. Relative accuracy is better than 1E-15
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
1.4
0 0.5 1 1.5 2 2.5 3 3.5 4
CPI(x)
SPI(x)
n = 8
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
-6 -4 -2 0 2 4 6
CPI(x)
SPI(x)
n = 8
These functions use the fast recursive algorithms
)2,(
1)(sincos
),(
1
−
−
+−=
−
nxSPI
n
n
n
xx
nxSPI
n
, xxSPI cos1)1,( −= , xxSPI =)0,(
)2,(
1)(cossin
),(
1
−
−
+=
−
nxCPI
n
n
n
xx
nxCPI
n
, xxCPI sin)1,( = , xxCPI =)0,(
The recursive formula of CPI(x, n) is very stable and accurate for all values x ≥ 0 , n ≥ 0, n ∈ N
On the contrary, the recursive formula of SPI(x, n) degrades precision for small value of x
In that case, the function automatically switches on the power expansion series






++
+
+
= ∑∫
∞
=
+
1
21
0
121
1
)(sin
k
kkn
x
n
y
nk
a
n
ydtt , 1<x
where
)2...(642
)12...(531
2
12
1 k
k
i
i
a
k
i
k
⋅⋅
−⋅⋅
=




 −
= ∏=
, xy sin=
Xnumbers Tutorial
260
Spherical Bessel functions of integer order
= BesselSphJ(x, [n])
= BesselSphY(x, [n])
These functions return the Spherical Bessel functions of 1st
and 2nd
kind of integer order n = 0,
1, 2... (default n = 0), and for any real x > 0, definited as:
)(
2
)(
2
1 xJ
x
xj nn +
=
π
Spherical Bessel functions of 1st
kind
)(
2
)(
2
1 xY
x
xy nn +
=
π
Spherical Bessel functions of 2nd
kind (also Neuman function)
The Spherical Bessel functions are the solutions of the Helmholtz radial equation in spherical
coordinates
0)(2 22
2
2
2
=−−++ ynnx
dx
dy
x
dx
yd
x
n x jn(x) yn(x)
--------------------------------------------------------------------------------
0 0.5 0.958851077208406 -1.755165123780750
1 1 0.301168678939757 -1.381773290676040
2 3 0.298637497075734 -0.267038335264499
Xnumbers Tutorial
261
Formulas Evaluation
Multiprecision Expression Evaluation
These functions realize a little math shell, putting together the power of multiprecision numeric
computation with the ease of symbolic calculus. Sometime we may want to perform the
computation using symbolic formulas. We would pass these strings to a routine for evaluation,
returning the numerical results with a given accuracy. These functions perform this useful task.
xeval( Formula, [Var], [DgtMax], [Angle],)
xevall( Formula, [Var1, Var2 …] )
These functions return the evaluation of a math expression in multiprecision arithmetic. They
use the same algorithm1
and have the same variable accuracy. They differ only for the input
parameters.
The parameter "Formula" is a math expression string containing variables, operators,
parenthesis and other basic functions. Examples.
3+1/(x^2+y^2), sin(2*pi*t)+4*cos(2*pi*t), (x^4+2x^3+6x^2-12x-10)^(1/2)
The optional parameter "Var" is an array containing one or more value for variables
substitution. Before computing, the parser substitutes each symbolic variable with its
correspondent value. It can be a single value, an array of values or, even an array of values +
labels (see examples).
The optional parameter "Var1", "Var2"... are single values or array as "Var" but without labels,
because the function xevall automatic finds by itself the appropriate labels. (See example)
The optional parameter "DgtMax" – from 1 to 200 - sets the maximum number of precision
digits (default=30). Setting DgtMax = 0 will force the function to evaluate in faster standard
precision.
The optional parameter "Angle" sets the angle unit "RAD” (default) “DEG”, “GRAD”.of for
trigonometric computation:
Example:
xeval("(1+sqr(2))/2+5^(1/3))") = 2.91708272786324451375395323463
xeval("(1+cos(x))/2+x^y" , {5, 1.2}) = 7.5404794000376872941836369067
xeval("(a+b)*(a-b)", {2, 3})= (2+3)*(2-3) = -5
All the function parameters can also be passed by cell references, like A1, $B$2, etc.
Example. Tabulate the following function for x = 1, 1.5, 2, … with 30 significant digits
2
1
1
)(
x
x
xf
+
+
=
1
The algorithm is divided into two steps: parsing and evaluation. The first step is performed by the MathParser class.
The evaluation is performed with the x-functions of XNUMBERS.
Xnumbers Tutorial
262
Note how the use of this function is simple and straight comparing with the correspondent
nested formulas
=xdiv(xadd(1,A6),xsqr(xadd(1,xpow(A6,2))))
Calculating functions with more than one variable a bit complication arises, because we have to
pay attention which values are assigned to the variables. Let’s see this example
Calculate the following bivariate function for x = 2.4, y = 5.5
2
1
)ln(
),(
x
xyy
yxf
+
+
=
In order to pass to the parameter "Var" the correct value for each variable we select the
variables range B2:C3 including the labels "x" and "y" (header). The labels must contain the
same symbols contained into the formula string
Note. If we pass the range B3:C3 without labels, the function assigns the values to the
variables in the same order that they appear in the formula, from left to right. In our example
the first variables is “y” and the second is “x”, so the function assignes the first value 2.4 to “y”
and the second value 5.5 to “x”
To by-pass the variable order rule, the function uses the trick of the “variables labels”. On the
contrary, for one or none variable it is impossible to make confusion so the header can be
omitted.
Variables order. The function returns the variables order in the Excel function insertion panel
In our example we see the string “y x” , that means you have to pass the first value for the
variable “y” and the second value for the variables “x”
Xnumbers Tutorial
263
The variables order is by-passed by labels. Using labels you do not need to worry about the
variables order
Let’s see another example. Calculat,e with 30 digits precision, the following function
)2cos()sin(),,( xbxabaxf −=
for x = 30° deg , a = 1 , b = -2
Note that we have set the angle unit = “DEG”
Note also that in this case the variable order would be “a, x, b”, but with the aid of the labels the
function can associate the exact parameters in the right way.
Sometime it is not possible to add a label near its value (in the middle of a table, for example).
Neither all parameters are located adjacent each others. For these cases we can use the
second evaluator function xevall
This function accepts separate parameters or separate array. We do not need to pass variables
labels. The function automatically finds all labels present on the active worksheet
Of course all this has a cost. the function xevall is about 10 times slower then xeval.
Let’s see how it works with an example. Tabulate the given trigonometric function, from t = 0 to
0.5, with step = 0.1 and an error less then 1E-20
)cos()cos()( tbtatf ⋅⋅+⋅⋅= ππ
where a = 0.5 and b = -2
The above sheet shows a possible arrangement. If we look the last cell B8 we discover that the
parameters are:
Var1 the cell A8 containing the value of the independent variable “t”
Var2 the range “C4:D4”, containing the values of the parameters “a” and “b”
Var3 range “C7:D7”, containing the internal parameter “DgtMax” and “AngleSet”
The internal parameters “DgtMax” and “AngleSet” are reserved word and must write as is.
Note also that the cell A8 has no label, but the function performs the correct assignment to the
“t” variable.
Xnumbers Tutorial
264
Label Rules. Labels must stay always at the
top or at the left of the corresponding values.
Labels can have any alphanumeric name
starting with any letter and not containing any
blank.
In the example:
t = 0.1, a = 0.5 , DgtMax = 30
The function xevall only assigns the cell value to the variable on top to the correspondent
column. Othewise it assigns the value to the variable on the left to the correspondent row.
Xnumbers Tutorial
265
Complex Expression Evaluation
=cplxeval( Formula, [Var1, Var2 …] )
This function1
evaluates a math expression in complex arithmetic.
The parameter "Formula" is a math expression string containing variables, operators,
parenthesis and other basic functions.
(3+8j)*(-1-4j) , (1+i)*ln(1+3i) , ((x+3i)/(x+4-2i))^(1-i)
The optional parameter "Var1", "Var2",... can be single or complex value. See How to insert a
complex number for better details
Example: Evaluate the given complex polynomial for z = 2 – i
)52()3(2
iziz ++++
Note that we use the complex rectangular format, i.e. "3+2j", only in the symbolic math
formula.
When we pass a complex value to a variable
we must always use the double cell format.
Note also that we can write “i” or “j” as well for
imaginary symbol, the parser can recognize
both of them.
For complex numbers labels are not supported. When we have formulas with two or more
variables, we must provide the values for variable substitutions in the exact order that they
appear in the formula, starting from left to right. The formula wizard will easily help you. Look at
this example.
Example. Compute the expression for the given complexes values
))((
)(
sk
bsas
e
sF
−−
= s = 1 + j , a = 1 − 4j , b = 3 + 6j ,
k = − 0.5
In the cell B2 we have inserted the string “exp(k*s)/((s-a)(s-b)”
When we enter the formula, the parser
recognizes the variables symbols and shows
us the exact order in which we have to pass to
the function itself.
In this case: k, s, a, b
1
This function uses the clsMathParserC class by A. De Grammont and L. Volpi
Xnumbers Tutorial
266
Multiprecision Excel Formula Evaluation
= xCalc(Formula, [DgtMax])
This function1
is useful for performing multiprecision calculation directly in a worksheet
Formulas can be entered just like in Excel and it may also contain references to other cells.
The optional parameter "DgtMax" – from 1 to 200 – sets the maximum number of precision
digits (default=30).
This smart functions has several advantages: it is easy to use and can handle long formula; the
formula is not changed at all and can keep the references to other cells. The only drawback
remains its speed because the function have to parse the formula and evaluate the result using
the multiprecision arithmetic; therefore the evaluation takes about 5-6 times slower then basic
x-functions.
Of course, not all the Excel functions can be converted. The functions recognized are.
+ - * / ^ ABS ACOS ACOSH ASIN ASINH ATAN ATANH
COMBIN COS COSH EXP FACT INT LN LOG MOD PI
ROUND SIGN SIN SINH SQRT TAN TANH
Nota. The functions are indicated with their original English names, that usually are different from the local names.
Let's see how it works. Assume to have in a cell the following Excel formula
=(A2*EXP(B2)+A4*EXP(B4))/(A2^2+A4^2)
Note that the formula references the values of other 4 cells: A2, B2, A4, B4
The formula value, calculated in standard precision, is 0.0140820785524786
We can get the value in 30 significant digits applying the xCalc function
=xCalc( (A2*EXP(B2)+A4*EXP(B4))/(A2^2+A4^2) )
For that, simply, select the cell D2 and move the mouse to the input field; insert xCalc( ... ) just
around the original formula.
The result in the cell D4 has now a precision of 30 significat digits
Observe that the the external cells links remain active and the formula is unchanged as in a
standard Excel formula
1
The function xCalc appears thanks to Ton Jeursen.
Xnumbers Tutorial
267
Note that, using the x-function, the above formula should be translated as
=xdiv(xadd(xmult(A2,xexp(B2)),xmult(A4,xexp(B4))),xadd(xpow(A2,"2"),xpow(A4,"2")))
The result is equivalent but it is evident the great advantage to keep the original formula.
Xnumbers Tutorial
268
Math expression strings
Functions like Integr, Series, xeval, xevall, cplxeval operate with symbolic math expressions
by the aid of clsMathParser and claMathparserC (two internal class modules).
These programs (for real and complex numbers) accept in input any string representing an
arithmetic or algebraic expression with a list of variable values and return a multiprecision
numeric result. Typical math expressions are:
1+(2-5)*3+8/(5+3)^2 sqr(2)+asin(x)
(a+b)*(a-b) x^2+3*x+1
1.5*exp(-t/12)*cos(pi*t + pi/4) (1+(2-5)*3+8/(5+3)^2)/sqr(5^2+3^2)
2+3x+2x^2 0.25x + 3.5y + 1
sqr(4^2+3^2) 1/(1+e# ) + Root(x,6)
(-1)^(2n+1)*x^n/n! |x-2|+|x-5|
And((x<2),(x<=5)) sin(2*pi*x)+cos(2*pi*x)
Variables can be any alphanumeric string and must start with a letter
x, y, a1, a2, time, alpha , beta
Also the symbol "_" is accepted to build variable names in "programming style".
time_1, alpha_b1 , rise_time
Capitals are accepted but ignored. Names such as "Alpha", "alpha", "ALPHA" indicate the
same variable.
Implicit multiplication is not supported because of its intrinsical ambiguity. So "xy" stands for
variable named "xy" and not for x*y. The multiplication symbol "*" generally cannot be omitted.
It can be omitted only for coefficients of the classic math variables x, y, z. It means that string
like 2x and 2*x are equivalent
2x, 3.141y, 338z^2 ⇔ 2*x, 3.141*y, 338*z^2
On the contrary, the following expressions are illegal in this context.
2a, 3(x+1), 334omega
Constant numbers can be integer, decimal, or exponential
2 -3234 1.3333 -0.00025 1.2345E-12
Note: This version support also the comma "," as decimal separator, depending on your system
internation option setting.
1,3333 -0,00025 1,2345E-12
Logical expressions are now supported
x<1 x+2y >= 4 x^2+5x-1>0 t<>0 (0<x<1)
Logical expressions return always 1 (True) or 0 (False). Compact expressions, like “0<x<1” ,
are now supported; you can enter: (0<x<1) as well (0<x)*(x<1)
Numerical range can be inserted using logical symbols and Boolean functions. For example:
For 2<x<5 insert (2<x)*(x<5) or also (2<x<5)
For x<2 , x>=10 insert OR(x<2, x>=10) or also (x<2)+(x>=10)
For -1<x<1 insert (x>-1)*(x<1) , or (-1<x<1) , or also |x|<1
Xnumbers Tutorial
269
Piecewise Functions. Logical expressions can also be useful for defining a piecewise
function, such as:
 2x-1-ln(2) x ≤ 0.5
f(x) =  ln(x) 0.5< x < 2
 x/2-1+ln(2) x ≥ 2
The above function can be written as:
f(x) = (x<=0.5)*(2*x-1-ln(2))+(0.5<x<2)*ln(x)+(x>=2)*(x/2-1+ln(2))
The parser adopts a new algorithm for evaluating math expressions depending on logical
expressions, which are evaluated only if the logical conditions are true (Conditioned-Branch
algorithm). Thus, the above piecewise expression can be evaluated for any real value x without
any domain error. Note that without this features the formula could be evaluated only for x>0.
Another way to compute piecewise functions is splitting it into several formulas (see example 6)
Math Constants supported are: Pi Greek (π), Euler-Napier (e)
pi = 3.14159265358979 or pi# = 3.14159265358979
e# = 2.71828182845905
Angle expression
This version supports angles in RAD radians, DEG degree, or GRAD degree.
For example if you set the unit "DEG", all angles will be read and converted into degrees
sin(120) => 0.86602540378444
asin(0.86602540378444) => 120
rad(pi/2) => 90 , grad(400) => 360 , deg(360) => 360
Angles can also be write in DMS format like for example 29d 59m 60s
sin(29d 59m 60s) => 0.5 , 29d 59s 60m => 30
Note This format is only for sexagesimal degree. It is independent from the unit set
Complex number can be indicated in a formula string as an ordered couple of number
enclosed into parenthesis “(..)” and divided by a comma “,” like for example:
(2, 3) (a, b) (-1, -0.05) (-1.4142135623731, -9.94665E-18)
On the other hand, complex numbers can also be indicate by the common rectangular form:
3+3j a+bj -1 − 0.05j -1.4142135623731 − 9.94665E-18j
You note that the second form is suitable for integer numbers, while, on the contrary, for
decimal or exponential number the first one is clearer. The parenthesis form is more suitable
also in nested results like
((2+3*4), (8-1/2)) that gives the complex number (14, 7.5)
Note: Pay attention if you want to use the rectangular convention in nested formulas.
wrong (2+3*4)+(8-1/2)j. correct (2+3*4)+(8-1/2)*j .
Do not omit the product symbol “*” before j because the parser recognize it as an expression,
not a complex number. The product symbol can be omitted only when before the letter “j” is a
constant number
Note: You can use both “j” and “i” for indicating the imaginary number 1−
Functions
Functions are called by their function-name followed by parentheses. Arguments can be:
numbers, variables, expressions, or even other functions.
sin(x) log(x) cos(2*pi*t+phi) atan(4*sin(x))
max(a,b) root(x,y) comb(n,k) beta(x,y)
Xnumbers Tutorial
270
For functions having more than one argument, the successive arguments are separated by
commas "," or alternatively, by semicolon ";" if your system has comma as decimal separator.
max(a;b) root(x;y) comb(n;k) beta(x;y) (only if comma "," is decimal sep.)
max(a,b) root(x,y) comb(n,k) beta(x,y) (only if point "." is decimal sep.)
Xnumbers Tutorial
271
List of basic functions and operators
The following table lists all functions and operators recongize in a math expression string
Use: R = Real, C = Complex, M = Multiprecision
The functions with M can be evaluated either in standard or multiprecision
The functions with C alone must be used with cplxeval only
The functions with R alone must be used with xeval, xevall setting DgtMax = 0 (standard
precision)
Function Use Description Note
- R C M subtraction
! R C M factorial 5!=120 (the same as fact)
% R M percentage 35% = 0.35 , 100+35% =103.5
* R C M multiplication
/ R C M division 35/4 = 8.75
 R C M integer division 354 = 8
^ R C M raise to power 3^1.8 = 7.22467405584208
| | R C M absolute value |-5|=5 (the same as abs)
+ R C M addition
< R M less than return 1 (true) 0 (false)
<= R M equal or less than returns 1 (true) 0 (false)
<> R M not equal returns 1 (true) 0 (false)
= R M equal returns 1 (true) 0 (false)
> R M greater than returns 1 (true) 0 (false)
>= R M equal or greater than returns 1 (true) 0 (false)
abs(x) R C M absolute value abs(-5)= 5
acos(x) R C M inverse cosine argument -1 ≤ x ≤ 1
acosh(x) R C M inverse hyperbolic cosine argument x ≥ 1
acot(x) R M inverse cotangent
acoth(x) R M inverse hyperbolic cotangent argument x<-1 or x>1
acsc(x) R M inverse cosecant
acsch(x) R M inverse hyperbolic cosecant
alog(z) C complex exponential with base 10 10^z
and(a,b) R logic and returns 0 (false) if a=0 or b=0
arg(z) C polar angle of complex number -pi < a <= pi
asec(x) R M inverse secant
asech(x) R M inverse hyperbolic secant argument 0 ≤ x ≤ 1
asin(x) R C M inverse sine argument -1 ≤ x ≤ 1
asinh(x) R C M inverse hyperbolic sine
atanh(x) R C M inverse hyperbolic tangent argument -1 < x < 1
atn(x), atan(x) R C M inverse tangent
beta(x,y) R C beta argument x>0 y>0
betaI(x,a,b) R Beta Incomplete function x >0 , a >0 , b >0
cbr(x) R cube root cbr(2) =1.25992104989487, also 2^(1/3)
clip(x,a,b) R Clipping function
return a if x<a , return b if x>b, otherwise
return x.
comb(n,k) R C M combinations comb(6,3) = 20
conj(x) C conjugate
cos(x) R C M cosine argument in radians
cosh(x) R C M hyperbolic cosine
cot(x) R M cotangent
argument (in radians) x≠ k*π with k = 0, ±
1, ± 2…
coth(x) R M hyperbolic cotangent argument x>2
csc(x) R M cosecant
argument (in radians) x≠ k*π with k = 0, ±
1, ± 2…
csch(x) R M hyperbolic cosecant argument x>0
dec(x) R M decimal part dec(-3.8) = -0.8
deg(x) R M degree sess. conversion conv. degree (60) into current unit of
Xnumbers Tutorial
272
angle
digamma(x) R C digamma argument x>0
Ei(x) R C exponential integral function argument x>0
Ein(x,n) R Exponential integral of n order x >0 , n = 1, 2, 3…
Elli1(x) R Elliptic integral of 1st kind ∀ φ , 0 < k < 1
Elli2(x) R Elliptic integral of 2st kind ∀ φ , 0 < k < 1
erf(x) R C error Gauss's function argument x>0
erfc(x) R C
error Gauss's function for complex
value
eu R M Euler-Mascheroni constant Gamma constant 0.577..
exp(x) R C M exponential exp(1) = 2.71828182845905
fact(x) R M factorial argument x >0
fix(x) R M integer part fix(-3.8) = 3
FresnelC(x) R Fresnel's cosine integral ∀ x
FresnelS(x) R Fresnel's sine integral ∀ x
gamma(x) R C gamma argument x>0
gammaln(x) R C logarithm gamma argument x>0
gcd(a,b) R greatest common divisor The same as mcd
grad(x) R degree cent. conversion
conv. degree (100) into current unit of
angle
HypGeom(x,a,b,c) R Hypergeometric function -1 < x <1 a,b >0 c ≠ 0, −1, −2…
im(z) C immaginary part of complex number
int(x) C integer part int(-3.8) = 4
integral(f,z,a,b) C Def. integral of complex function f(z) Integral('1/z^2','z',1-i,1+i)
inv(x) C inverse of a number 1/x
lcm(a,b) R M lowest common multiple The same as mcm
ln(x), log(x) R C M logarithm natural argument x>0
max(a,b,...) R M maximum max(-3.6, 0.5, 0.7, 1.2, 0.99) = 1.2
min(a,b,...) R M minimum min(13.5, 24, 1.6, 25.3) = 1.6
mcd(a,b,...) R M maximun common divisor mcm(4346,174) = 2
mcm(a,b,...) R M minimun common multiple
mcm(1440,378,1560,72,1650) =
21621600
mod(a,b) R M modulus mod(29, 6) = 5 mod(-29, 6) = 1
nand(a,b) R logic nand returns 1 (true) if a=1 or b=1
neg(z) C opposit of complex -z
nor(a,b) R logic nor returns 1 (true) only if a=0 and b=0
not(a) R logic not returns 0 (false) if a ≠ 0 , else 1
nxor(a,b) R logic exclusive-nor returns 1 (true) only if a=b
or(a,b) R logic or returns 0 (false) only if a=0 and b=0
pi R M Pi greek 3.141….
PolyCh(x,n) R Chebycev's polynomials ∀x , orthog. for -1 ≤ x ≤1
PolyHe(x,n) R Hermite's polynomials ∀ x , orthog. for −∞ ≤ x ≤ +∞
PolyLa(x,n) R Laguerre's polynomials ∀ x , orthog. for 0 ≤ x ≤1
PolyLe(x,n) R Legendre's polynomials ∀ x , orthog. for -1 ≤ x ≤1
rad(x) R M radians conversion converts radians into current unit of angle
re(x) C real part of complex number
rnd(x) R random
returns a random number between x and
0
root(x,n) R C M n-th root argument x ≥ 0 (the same as x^(1/n)
round(x,d) R M round a number with d decimal round(1.35712, 2) = 1.36
sec(x) R M secant
argument (in radians) x≠ k*π/2 with k = ±
1, ± 2…
sech(x) R C M hyperbolic secant argument x>1
serie(….) C serie expansion of complex value z Serie('z/n','n',1,10,'z',1+3i)
sgn(x) R C sign returns 1 if x >0 , 0 if x=0, -1 if x<0
sin(x) R C M sin argument in radians
sinh(x) R C M hyperbolic sine
Xnumbers Tutorial
273
sqr(x) R C M square root sqr(2) =1.4142135623731, also 2^(1/2)
tan(x) R C M tangent
argument (in radians) x≠ k*π/2 with k = ±
1, ± 2…
tanh(x) R C M hyperbolic tangent
xor(a,b) R logic exclusive-or returns 1 (true) only if a ≠ b
BesselI(x,n) R
Bessel's function of 1st kind, nth
order, mod.
x >0 , n = 0,1, 2, 3…
BesselJ(x,n) R
Bessel's function of 1st kind, nth
order
x ≥0 , n = 0, 1, 2, 3…
BesselK(x,n) R
Bessel's function of 2nd kind, nth
order, mod.
x >0 , n = 0,1, 2, 3…
BesselY(x,n) R
Bessel's function of 2nd kind, nth
order
x ≥0 , n = 0,1, 2, 3…
J0(x) R Bessel's function of 1st kind x ≥0
Y0(x) R Bessel's function of 2nd kind x ≥0
I0(x) R Bessel's function of 1st kind, modified x >0
K0(x) R
Bessel's function of 2nd kind,
modified
x >0
Si(x) R Sine integral ∀ x
Ci(x) R Cosine integral x >0
zeta(x) R Riemman's zeta function argument x<-1 or x>1
AiryA(x) R Airy function Ai(x)
∀x, example cbr(2) = 1.2599, cbr(-2) = -
1.2599
AiryB(x) R Airy function Bi(x)
argument (in radian) x≠ k*π/2 with k = ±
1, ± 2…
Mean(a,b,...) R M Arithmetic mean mean(8,9,12,9,7,10) = 9.1666
Meanq(a,b,...) R M Quadratic mean meanq(8,9,12,9,7,10) = 9.300
Meang(a,b,...) R M Arithmetic mean meang(8,9,12,9,7,10) = 9.035
Var(a,b,...) R M Variance var(1,2,3,4,5,6,7) = 4.6666
Varp(a,b,...) R M Variance pop. varp(1,2,3,4,5,6,7) = 4
Stdev(a,b,...) R M Standard deviation Stdev(1,2,3,4,5,6,7) = 2.1602
Stdevp(a,b,...) R M Standard deviation pop. Stdevp(1,2,3,4,5,6,7) = 2
Step(x,a) R Haveside's step function Returns 1 if x ≥ a , 0 otherwise
DSBeta(x, a, b, [j]) R Beta distribution (j=1 cumulative) 0 < x < 1 , a > 0, b > 0,
DSBinomial(k, n, p, [j]) R Binomial distribution (j=1 cumulative) k integer, n integer , 0 < p < 1
DSCauchy(x, m, s, n, [j]) R
Cauchy (generalized) distribution (j=1
cumul.)
n integer , s > 0
DSChi(x, r, [j]) R Chi distribution (j=1 cumulative) r integer, x > 0
DSErlang(x, k, l, [j])) R Erlang distribution (j=1 cumulative) k integer, x > 0
DSGamma(x, k, l, [j])) R Gamma distribution (j=1 cumulative) x > 0, k > 0, l > 0
DSLevy(x, l, [j])) R Levy distribution (j=1 cumulative) x > 0, l > 0
DSLogNormal(x, m, s, [j])) R
Log-normal distribution (j=1
cumulative)
x > 0, m ≥ 0, s > 0
DSLogistic(x, m, s, [j])) R Logistic distribution (j=1 cumulative) x > 0, m ≥ 0, s > 0
DSMaxwell(x, a, [j])) R
Maxwell-Boltzman distribution (j=1
cumulative)
x > 0, a > 0
DSMises(x, k, [j])) R
Von Mises distribution (j=1
cumulative)
k > 0, -π < x < π
DSNormal(x, m, s, [j])) R Normal distribution (j=1 cumulative) s > 0
DSPoisson(k, z, [j])) R Poisson distribution (j=1 cumulative) k integer, z > 0
DSRayleigh(x, s, [j])) R Rayleigh distribution (j=1 cumulative) x > 0, s > 0
DSRice(x, v, s, [j])) R Rice distribution (j=1 cumulative) x > 0, v ≥ 0 , s > 0
DSStudent(t, v, [j])) R Student distribution (j=1 cumulative) v integer degree of freedom
DSWeibull(x, k, l, [j])) R Weibull distribution (j=1 cumulative) x > 0, k integer, l > 0
Symbol "!" is the same as "Fact", symbol "" is the integer division, symbols “|x|” is the same as Abs(x)
Logical function and operators returns 1 (true) or 0 (false)
Note: the arguments separator of the functions changes automatically from "," to ";" if your system decimal separator is
comma ",".
Xnumbers Tutorial
274
Function Optimization
Macros for optimization on site
These macros has been ideated for performing the optimization task directly on the worksheet.
This means that you can define any function that you want simply using the standard Excel
built-in functions.
Objective function. For example: if you want to search the minimum of the bivariate function
( ) ( )2
100
352
100
51
),( −+−= yxyxf
insert in the cell E4 the formula "=(B4-0.51)^2+(C4-0.35)^2", where the cells B4 and C4
contain the current values of the variables x and y respectively. Changing the values of B4 e/o
C4 the function value E4 also changes consequently.
For optimization, you can choose two different algorithms
Downhill-Simplex 1
The Nelder–Mead downhill simplex algorithm is a popular derivative-free
optimization method. Although there are no theoretical results on the
convergence of this algorithm, it works very well on a wide range of
practical problems. It is a good choice when a one-off solution is wanted
with minimum programming effort. It can also be used to minimize
functions that are not differentiable, or we cannot differentiate.
It shows a very robust behavior and converges for a very large set of
starting points. In our experience is the best general purpose algorithm,
solid as a rock, it's a "jack" for all trades.
For mono and
multivariate
functions
Divide-Conquer 1D
For monovariable function only, it is an high robust derivative free
algorithm. It is simply a modified version of the bisection algorithm
Adapt for every function, smooth or discontinue.
It converges for very large segments. Starting point not necessary
For monovariable
function only. It
needs the segment
where the max or
min is located
Example assume to have to minimize the following function for x > 0
)4cos()3sin()( 3
xexexf xx −−
+=
1
The Downhill-Simplex of Nelder and Maid routine appears by the courtesy of Luis Isaac Ramos Garcia
Xnumbers Tutorial
275
We try to search the minimum in the range 0 < x < 10
Choose a cell for the variable x , example B6, and insert the function
= SIN(3*B6)*EXP(- 2*B6) + COS(4*B6)*EXP(-B6)
in a cell that you like, for example C6.
After this, add the constrain values into another range, for example B3:C3
The values of the variables at the start are not important
Select the cell of the function C6 and start the macro "1D divide and conquer", filling the input
field as shown
Stopping limit. Set the maximum evaluation points allowed.
Max/Min. The radio buttons switches between the minimization and maximization algorithm
The "Downhill-Simplex" macro is similar except that:
• The constrain box is optional.
• It accepts up to 9 variables (range form 1 to 9 cells)
• The algorithm starts from the point that you give in the variable cells. If the constrain
box is present, the algorithm starts from a random point inside the box
Let's see how it works with some examples1
.
1
The following examples are extracted from "Optimization and Nonlinear Fitting" , Foxes Team, Nov. 2004
Xnumbers Tutorial
276
Example 1 - Rosenbrock's parabolic valley
This family of test functions is well known to be a minimizing problem of high difficult
( ) ( )222
1),( xxymyxf −+−⋅=
The parameter "m" tunes the difficult: high value means high difficult in minimum searching.
The reason is that the minimum is located in a large flat region with a very low slope. The
following 3D plot shows the Rosenbrock's parabolic valley for m = 100
The following contour plot is obtained for m = 10
The function is always positive except in the point (1, 1) where it is 0. it is simple to
demonstrate it, taking the gradient
( )
( )



=−
=−⋅−+⋅
⇒=∇
02
022124
0 2
3
xym
ymxxm
f
Xnumbers Tutorial
277
From the second equation, we get
( ) 22
02 xyxym =⇒=−
Substituting in the first equation, we have
( ) 1022022124 23
=⇒=−⇒=−⋅−+⋅ xxxmxxm
So the only extreme is the point (1, 1) that is the absolute minimum of the function
To find numerically the minimum, let's arrange a similar sheet.
We can insert the function and the parameters as we like
Select the cell D4 - containing the objective function - and start the macro "Downhill-Simplex".
The macro fills automatically the variables-field with the cells related to the objective function.
But, In that case, the cell A4 contains the parameter m that the macro must not change. So
insert only the range B4:C4 int the variables field.
The cells B4:C4 will change for minimizing the
objective function in the cell D4
Leave empty the constraint input box and press "Run"
Starting from the point (0, 0) we obtain the following good results
m Algorithm x y error time
10 Simplex 1 1 2.16E-13 2 sec
100 Simplex 1 1 4.19E-13 2 sec
where the error is calculated as |x-1|+|y-1|
Example 2 - Constrained minimization
Example: assume to have to minimize the following function
710442),( 22
+−+−+= yyxxyxyxf
with the ranges constrains
5.00,20 ≤≤≤≤ yx
The Excel arrangement can be like the following
Xnumbers Tutorial
278
Compare with the exact solution x = 1.5, y = 0.5
Note that the function has a free minimum at x = 1, y = 1
Repeat the example living empty the constrains box input, for finding those free extremes.
Xnumbers Tutorial
279
Example 3 - Nonlinear Regression with Absolute Sum
This example explains how to perform a nonlinear regression with an objective function
different from the "Least Squared". In this example we adopt the "Absolute Sum".
We choose the exponential model
xk
eakaxf ⋅−
⋅=),,(
The goal of the regression is to find the best couple of parameters (a, k) that minimizes the sum
of the absolute errors between the regression model and the given data set.
∑ −= |),,(| kaxfyAS ii
The objective function AS depends only by parameter a, k. Giving in input this function to our
optimization algorithm we hope to solve the regression problem
A possible arrangement of the worksheet may be:
We hope that changing the parameters "a" and "k" int the cells E2 and F3, the objective
function (yellow cell) goes to its minimum value. Note that the objective function depends
indirectly by the parameters a and k.
0
0.2
0.4
0.6
0.8
1
1.2
0 0.2 0.4 0.6 0.8 1 1.2
y
y*
The starting condition is the following, where y
indicates the given data and y* is the
regression plot (a flat line at the beginning)
Start the Downhil-Simplex and insert the
appropriate ranges: objective function = G3
and variables = E3:F3
Starting form the point (1, 0) you will see the cells changing quickly until the macro stops itself
leaving the following "best" fitting parameters and the values of the regression y*
Best fitting parameters
a k
1 -2
The plot of the y* function and the samples
y are shown in the graph. As we can see
the regression fits perfectly the given
dataset.
Xnumbers Tutorial
280
Example 4 - Optimization of Integral function
The method of optimization on-site used by Xnumbers macros and by the Excel Solver is very
flexible. This is expecially useful when the optimization function has a complicated form.
Assume, for example, to have to maximize the following integral respect to the parameter α
∫
−
−
1
0
)( dxexx xα
α
The function integr returns the integral in
the cell B5. The integration limits and the
parameter α are contained in the cells B2,
B3 and B4 respectively.
Start the optimization macro that you like,
setting the objective cell B5 and changing
the cell B4
Here, we have choose three different algorithms. They seem work very fine for this problem
Algorithm α (max) error
Divide-conquer 2.210360464 9.8E-09
Downhill-Simplex 2.210360527 5.4E-08
Excel Solver 2.210361438 9.6E-07
Another example. Find the maximum of the following function for 0 < ω < 2
∫=
π
ωω
0
)sin()( dxxf
Here we use the Excel Solver for finding the
value of the cell D2 (the variable ω)
maximizing the cell E2 (the integral),
subjected to the constraints 0 < ω < 2 .
The final result w = 0.74201929194068
has an high precision, better 1E-8.
Xnumbers Tutorial
281
How to call Xnumbers functions from VBA
There are two ways for calling Xnumbers functions from other user VBA macros
• by reference method
• by Application.Run method
Both methods have advantages and drawbacks. Let's see.
Reference method
Prepare a new worksheet called, for example test.xls
First of all , after we have installed
and loaded Xnumbers.xla in Excel,
turn on the VBA editor by shift+F11
and add a new module.
Our VBA editor should look like this.
In particular we must see the
XNUMBERS (xnumbers.xla) project
and our new project VBAProject
(test.xls)
Now we add a reference link in our
VBAProject to XNUMBERS project
From the menu tool/ references...
Switch on the box of XNUMBERS library and click OK
Note that a references object has been added to our project
From now on all functions (except the functions declared private) of XNUMBERS are available
in our macros. Of course all functions that you use in worksheet are public. But not all the
functions that we can use in a worksheet can be called from VBA and vice versa.
The link that we have added is permanent. This means that when we load our test.xls file, the
Xnumbers.xla will also be automatically loaded.
If you want to remove the link to Xnumbers.xla simply return to menu tools/references and
deselect the box
Now prepare the following simple macro that reads two numbers from the sheet and returns the
division in multiprecision
Xnumbers Tutorial
282
Sub Test()
Dim a, b, c, DgtMax
DgtMax = Range("B2")
a = Range("B3")
b = Range("B4")
c = XNUMBERS.xdiv(a, b,
DgtMax)
Range("B5") = "'" & c
End Sub
Note that the above code works also if we write the compact statement
c = xdiv(a, b, DgtMax)
The XNUMBERS prefix is not necessary; it is useful only - at the developing time - to generate
automatically the list of the functions when we write the dot "." after XNUMBERS.
The result of the Test macro will be:
Of course the x-functions can be nested as any other function.
For example assume now to have to compute the expression = a * Ln(b)
The code will be
Sub Test1()
Dim a, b, c, DgtMax
DgtMax = Range("B2")
a = Range("B3")
b = Range("B4")
c = xmult(a, xln(b, DgtMax), DgtMax)
Range("B5") = "'" & c
End Sub
The output of the Test1 macro will be
Xnumbers Tutorial
283
As we have seen calling x-function using the reference method is very easy and straightforward
because it works as we have "virtually" copied all the Xnumbers.xla code in our project
The main drawback is that the link set in our workbook is static. This mean that if we move or
install the Xnumbers.xla addin in another directory, also the link must be removed and then
rebuilt.
Application.Run method
A metod for calling external functions that always works, independently where you have
installed the addin, uses the Excel Application.Run method. Using this method we do not need
to add any reference link. We have only to load the addin in Excel.
Therefore our VBA environment looks like as simple as possible and the code of the macro
Test will modified as the following
Sub test()
Dim a, b, c, DgtMax
DgtMax = Range("B2")
a = Range("B3")
b = Range("B4")
With Application
c = .Run("xdiv", a, b,
DgtMax)
End With
Range("B5") = "'" & c
End Sub
The first argument of the "Run" method is the name of the function that we want to call, that is
"xdiv" in our case (note the quotes around the name).
The other arguments are, in sequence, the parameters that we pass to the function xdiv.
The "Run" method returns in the variable c the result of the xdiv function.
This method is both simple and efficient. The only drawback is that it cannot accept nested
functions. Thus the Test1 macro becomes a bit more tricky.
Sub test1()
Dim a, b, c, DgtMax
DgtMax = Range("B2")
a = Range("B3")
b = Range("B4")
With Application
c = .Run("xLn", b, DgtMax)
c = .Run("xmult", a, c, DgtMax)
End With
Range("B5") = "'" & c
End Sub
The final result, of course, is the same.
Xnumbers Tutorial
284
References & Resources
"MSIEVE: A Library for Factoring Large Integers", Jason Papadopoulos , v1.17, March 2007
"LAPACK -- Linear Algebra PACKage 3.0", Updated: May 31, 2000
"EISPACK Guide, Matrix Eigensystem Routines”, Smith B. T. at al. 1976
"Numerical Analysis" F. Sheid, McGraw-Hill Book Company, New-York, 1968
"Handbook for Autom. Computation - Linear Algebra", vol II, Wilkinson, Martin, Peterson, 1970
"Matrix Analysis and Applied Linear Algebra", C. D. Mayer, Siam, 2000
"Linear Algebra", J. Hefferon, Saint Michael’s College, Colchester, Vermont, 2001,
"Numerical Methods that usually work", F. S. Acton, The Mathematica Association of America, 1990
"Analysis Nunerical Methods", E. Isaacson, H. B. Keller, Wiles & Sons, 1966
"Calculo Numérico", Neide M. B. Franco, 2002
"Metodos Numericos" Sergio R. De Freitas, 2000
"Numerical Mathematics in Scientific Computation", G. Dahlquist, Å. Björck, vol II.
"Numerical Recipes in FORTRAN 77- The Art of Scientific Computing - 1986-1992 by Cambridge
University Press. Programs Copyright (C) 1986-1992 by Numerical Recipes Software
"Computation of Special Functions", by Shanjie Zhang and Jianming Jin - John Wiley and Sons, Inc
"Lanczos Implementation of the Gamma Function" by Paul Godfrey, Intersil Corp, 2001
"clsMathParser - A Class for Math Expressions Evaluation in Visual Basic", Leonardo Volpi , Michael
Ruder, Thomas Zeutschler, Lieven Dossche, . 3.2 Jan. 2003, by .Volpi
"clsMathParserC -A Class for Complex Math Expressions Evaluation in Visual Basic", Arnaud de
Grammont, Leonardo Volpi, v. 3.2 , Jan. 2003
"F F T (Fast Fourier Transform)", Paul Bourke, June 1993, http://astronomy.swin.edu.au
"2 Dimensional FFT" , Paul Bourke, June 1998, http://astronomy.swin.edu.au
“Solutions Numeriques des Equations Algebriques”, E., Durand, Tome I, Masson,Paris ,1960.
”A modified Newton method for Polynomials” , W.,Ehrilich, Comm., ACM, 1967, 10, 107-108.
“Ein Gesamschrittverfahren zur Berechnung der Nullstellen eines Polynoms”, O., Kerner, Num.Math.,
1966, 8, 290-294.
“Iteration methods for finding all the zeros of a polynomial simultaneously”, O. Aberth, Math. Comp. ,1973,
27, 339-344.
“The Ehrlich-Aberth Method for the nonsymmetric tridiagonal eigenvalue problem”, D. A. Bini, L.
Gemignani, F, Tisseur, AMS subject classifications. 65F15
“Progress on the implementetion of Aberth’s method”, D. A. Bini, G. Fiorentino, , 2002, The FRISCO
Consortiunm (LTR 21.024)
“A Method for Finding Real and Complex Roots of Real Polynomials With Multiple Roots”, by C. Bond, July
10, 2003
“A Robust Strategy for Finding All Real and Complex Roots of Real Polynomials”, by C. Bond, April 24,
2003
"Nonlinear regression", Gordon K. Smyth Vol. 3, pp 1405/1411, in Encyclopedia of Environmetrics (ISBN
0471 899976), Edited by John Wiley & Sons, Ltd, Chichester, 2002
"Optimization", Gordon K. Smyth Vol. 3, pp 1481/1487, in Encyclopedia of Environmetrics (ISBN 0471
899976), Edited by John Wiley & Sons, Ltd, Chichester, 2002
"Process Modeling", The National Institute of Standards and Technology (NIST) website for Statistical
Reference Datasets, (http://www.itl.nist.gov/div898/handbook/pmd/pmd)
"Metodos numericos con Matlab"; J. M Mathewss et al.; Prentice Hall
"Genetic and Nelder-Mead", E. Chelouan, et al, EJOR 148(2003) 335-348
"Convergence properties", J.C. Lagarias, et al, SIAM J Optim. 9(1), 112-147
Xnumbers Tutorial
285
"Optimization for Engineering Systems", Ralph W. Pike, 2001, Louisiana State University
(http://www.mpri.lsu.edu/bookindex)
"Zeros of Orthogonal Polynomials" , Leonardo Volpi, Foxes Team
(http://digilander.libero.it/foxes/Documents)
"How to tabulate the Legendre’s polynomials coefficients" Leonardo Volpi, Foxes Team
(http://digilander.libero.it/foxes/Documents)
"DE-Quadrature (Numerical Automatic Integrator) Package", by Takuya OOURA, Copyright(C) 1996
“Advanced Excel for scientific data analysis", Robert de Levie, 2004, Oxford University Press
"Microsoft Excel 2000 and 2003 Faults, Problems, Workarounds and Fixes", David A. Heiser, web site
http://www.daheiser.info/excel/frontpage.html
"NIST/SEMATECH e-Handbook of Statistical Methods", January 26, 2005
(http://www.itl.nist.gov/div898/handbook)
"DE-Quadrature (Numerical Automatic Integrator) Package", by Takuya OOURA, Copyright(C) 1996,
Research Institute for Mathematical Sciences, Kyoto University, Kyoto 606-01, Japan
(http://momonga.t.u-tokyo.ac.jp/~ooura)
"A Comparison of three high-precision quadrature schemes", David H. Bailey and Xiaoye S. Li, Copyright
2003, University of California (http://repositories.cdlib.org/lbnl/LBNL-53652)
"Asymptotic Bit Cost of Quadrature Formulas Obtained by Variable Transformation" P. Favati, Appl. Math.
Lett. Vol. 10, No. 3, pp. 1-7, 1997, Pergamon Copyright 1997
"Gli algoritmi della crittografia a chiave pubblica", Giovanni Fraterno, settembre 2000.
http://digilander.libero.it/crittazione
Bailey, D. H.; Borwein, J. M.; Calkin, N. J.; Girgensohn, R.; Luke, D. R.; and Moll, V. H. "Integer Relation
Detection." §2.2 in Experimental Mathematics in Action. Wellesley, MA: A K Peters, pp. 29-31, 2006.
Bailey, D. H. and Broadhurst, D. J. "Parallel Integer Relation Detection: Techniques and Applications."
Math. Comput. 70, 1719-1736, 2001.
Bailey, D. H. and Ferguson, H. R. P. "Numerical Results on Relations Between Numerical Constants Using
a New Algorithm." Math. Comput. 53, 649-656, 1989.
Bailey, D. and Plouffe, S. "Recognizing Numerical Constants."
http://www.cecm.sfu.ca/organics/papers/bailey/.
Borwein, J. M. and Lisonek, P. "Applications of Integer Relation Algorithms." To appear in Disc. Math.
http://www.cecm.sfu.ca/preprints/1997pp.html.
Weisstein, Eric W. "Integer Relation." From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/IntegerRelation.html
Helaman R. P. Ferguson and David H. Bailey , "A Polynomial Time, Numerically Stable Integer Relation
Algorithm" RNR Technical Report RNR-91-032, July 14, 1992
Xnumbers Tutorial
286
Credits
Software developed
Xnumbers contains code developed by the following authors that kindly contributed to this
collection.
Luis Isaac Ramos Garcia Orthogonal Polynomials and Downhill Simplex Nelder-Mead routine
Olgierd Zieba Cubic Spline and documentation
Alfredo Álvarez Valdivia Robust Method fitting routines
Michael J. Kozluk Log Relative Error function, Linear regression debugging, and
documentation (StRD benchmark)
Ton Jeursen Format function, Xnumber debugging; Xnumber for Excel95,
xBaseChange - Multiprecision BaseChange
xCalc - multiprecision conversion function
Richard Huxtable ChangeBase and Prime functions
Michael Ruder MathParser improvement and debugging
Thomas Zeutschler MathParser improvement and debugging
Lieven Dossche Class MathParser development
Arnaud de Grammont Complex MathParser developement
Rodrigo Farinha MathParser improvement and debugging
Vladimir Zakharov Installation and initialization improvement
Hans Günter Extension of exponential integral for negative x
David A. Heiser Weilford's algorithm for statistic standard deviation
John Jones Modular arithmetic and Xnumber converter parser
Software translated
Xnumbers contains VB code translated from the following packages:
Takuya OOURA DE-Quadrature (Numerical Automatic Integrator) Package
Shanjie Zhang - Jianming Jin FORTRAN routines for computation of Special Functions
Software used
This documentation was developed thanks to:
Microsoft Help Workshop 4 Compiler for Help-on line
Microsoft Word and Equation Editor RTF main document and formulas
Winplot ( Peanut Software by Richard Parris) Math plots and graphical elaboration
Derive 5 (Texas Instrument) Math graph and symbolic elaboration
MathGV 3.1 (by Greg VanMullem) Math graphs and bitmap elaboration
Msieve 1.17 by Jason Papadopoulos. Large number factoring
Many thanks to everybody that have contributed to this document and made it more intelligible.
I am especially grateful for invaluable help on many occasions to Robert de Levie.
Xnumbers Tutorial
287
Analytical Index
Analytical index
A
Absolute; 28
Adams; 211; 212
Adams-Bashfort-Moulton; 211
Addition; 23
Airy functions; 252
AiryA; 252
AiryAD; 252
AiryB; 252
AiryBD; 252
Aitken; 158; 183
Arccos; 62
Arcsine; 62
Arctan; 62
Arithmetic Mean; 40
B
Base conversion; 240
baseChange; 240
Bessel functions; 249
BesselSphJ; 255
Beta function; 247
Bisection; 231
Bivariate Polynomial; 88
Brouncker-Pell Equation; 120
C
Cebychev; 78
Change sign; 28
Check digits; 32
Check Integer; 112
Check odd/even; 112
Check Prime; 109
Cholesky; 131
Coefficients of Orthogonal Polynomials; 96
Combinations; 39
Combinations function; 248
Complement of right angle; 63
Complex absolute; 99
Complex Addition; 98
Complex ArcCos; 102
Complex ArcSin; 102
Complex ArcTan; 102
Complex Complementary Error Function; 104
Complex conjugate; 101
Complex Cos; 102
Complex digamma; 103
Complex Division; 98
Complex Error Function; 104
Complex Exp; 101
Complex Exponential Integral; 103
Complex Expression Evaluation; 260
Complex Function Integration (Romberg
method); 156
Complex Gamma Function; 104
Complex Hyperbolic Cosine; 103
Complex Hyperbolic Sine; 102
Complex Hyperbolic Tan; 103
Complex inverse; 101
Complex Inverse Hyperbolic Cos; 103
Complex Inverse Hyperbolic Sin; 103
Complex Inverse Hyperbolic Tan; 103
Complex Log; 101
Complex Logarithm Gamma Function; 104
Complex Multiplication; 98
Complex negative; 101
Complex power; 100
Complex Quadratic Equation; 105
Complex Roots; 100
Complex Series Evaluation; 184
Complex Sin; 102
Complex Subtraction; 98
Complex Tangent; 102
Complex Zeta Function; 104
Constant e; 59
Constant Ln(10); 59
Constant Ln(2); 59
Constant pi; 62
Continued Fraction; 108
Continued Fraction of Square Root; 109
Convert extended into double; 34
Convol; 189
corrector; 211
Corrector; 212; 214
Cos; 61
Cosine Integral Ci(x); 249
CosIntegral; 249
cplxabs; 99
cplxacos; 102
cplxacosh; 103
cplxadd; 98
cplxasin; 102
cplxasinh; 103
cplxatan; 102
cplxatanh; 103
cplxconj; 101
cplxcos; 102
cplxcosh; 103
cplxdigamma; 103
cplxdiv; 98
cplxei; 103
cplxEquation2; 105
cplxerf; 104
cplxerfc; 104
cplxeval; 260
cplxExp; 101
cplxgamma; 104
cplxgammaln; 104
Xnumbers Tutorial
288
cplxintegr; 156
cplxinv; 101
cplxLn; 101
cplxmult; 98
cplxneg; 101
cplxpolar; 99
cplxpow; 100
cplxrect; 99
cplxroot; 100
cplxserie; 184
cplxsin; 102
cplxsinh; 102
cplxsub; 98
cplxtan; 102
cplxtanh; 103
cplxzeta; 104
Crout; 131
cspline_coeff; 199
cspline_eval; 197
cspline_interp; 197
cspline_pre; 197
Cubic Spline 2nd derivatives; 197
Cubic Spline Coefficients; 199
cvBaseDec; 240
cvBinDec; 240
cvDecBase; 240
cvDecBin; 240
D
Data Conditioning; 53
Data Integration (Newton Cotes); 157
dBel; 240
Decibel; 240
Decimal part; 29
DFSP; 140
DFSP_INV; 140
DFT; 137; 142
DFT_INV; 139
Diff1; 234
Diff2; 235
digamma; 247
Digamma function; 247
Digit_Max; 22; 35
Digits count; 31
Digits sum; 33
DigitsAllDiff; 32
Diophantine; 119
Diophantine Equation; 119
DiophEqu; 119
Discrete 2D Fourier Transform; 141
Discrete Convolution; 189
Discrete Fourier Inverse Transform; 139
Discrete Fourier Spectrum; 140
Discrete Fourier Transform; 137
Division; 24
Double Data integration; 179
Double Exponential; 152
Double Integral; 168
Double integration function; 174
Double Series; 185
Downhill; 268
DPOLYN; 74
E
Elliptic Integrals; 252
errfun; 244
Error Function Erf(x); 244
Euler; 211
Euler constant γ; 60
Euler-Mascheroni Constant; 245
Euler's Totient function; 121
exp_integr; 244
expn_integr; 244
Exponential; 58
Exponential any base; 58
Exponential integral Ei(x); 244
Exponential integral En(x); 244
Extended Number Check; 32
F
Factor; 116
Factorial; 39
Factorial with double-step; 39
Factorize; 113
Factorize function; 116
FFT; 137; 142
FFT_INV; 139
FFT2D; 141
FFT2D_INV; 141
Fibonacci numbers; 250
First Derivative; 234
Flip; 33
Format Extended Number; 32
Fourier; 165
Fourier_cos; 164
Fourier_cos; 166
Fourier_cos; 166
Fourier_sin; 164
fract; 107
Fract_Interp; 195
Fract_Interp_Coef; 195
FractCont; 108
FractContSqr; 109
Fresnel cosine Integral; 250
Fresnel sine Integral; 250
Fresnel_cos; 250
Fresnel_sin; 250
Function Integration (Double Exponential
method); 152
Function Integration (mixed method); 154
Function Integration (Newton-Cotes); 159
Function Integration (Romberg method); 151
G
Gamma F-factor; 247
Gamma function); 245
Gamma quotient; 246
GCD; 106
Geometric Mean; 40
Grad; 235
Gradient; 235
Greatest Common Divisor; 106
H
Hermite; 78
Hessian; 236
Hessian matrix; 236
Hyperbolic Arc Cosine; 60
Hyperbolic Arc Sine; 59
Xnumbers Tutorial
289
Hyperbolic Arc Tangent; 60
Hyperbolic Cosine; 60
Hyperbolic Sine; 59
Hyperbolic Tangent; 60
Hypergeometric function; 251
Hypgeom; 251
I
I BesselSphY; 255
IElliptic1; 252
IElliptic2; 252
Incomplete Beta function; 248
Incomplete Gamma function; 248
Infinite integral; 177
Infinite Integration of oscillating functions; 165
Integer Division; 24
Integer part; 29
Integer polynomial; 86
Integer relation; 122
Integer Remainder; 25
Integer roots; 70
integr; 166
Integr; 154
Integr_2D; 174
Integr_fcos; 162
Integr_fsin; 162
Integr_nc; 159
Integr_ro; 151
Integr2D; 168
Integr3D; 171
Integral function; 147
Integral of sine-cosine power; 254
Integral_Inf; 177
Integration of oscillating functions (Filon
formulas); 162
Integration of oscillating functions (Fourier
transform); 164
IntegrData; 147
IntegrData2D; 179
IntegrDataC; 157
Interp_Mesh; 200
Interpolation with continue fraction; 195
Interpolation with Cubic Spline; 197
IntPowCos; 254
IntPowSin; 254
Inverse; 24
Inverse 2D Discrete Fourier Transform; 141
Inverse Discrete Fourier Spectrum; 140
J
Jacobian; 236
Jacobian matrix; 236
K
Kummer confluent hypergeometric functions;
253
Kummer1; 253
Kummer2; 253
L
LCM; 106
Least Common Multiple; 106
Legendre; 78
Linear Regression - Coefficients; 50
Linear Regression - Standard Deviation of
Estimate; 45
Linear Regression Coefficients; 43
Linear Regression Covariance Matrix; 47
Linear Regression Evaluation; 49
Linear Regression Formulas; 46
Linear Regression Min-Max; 56
Linear Regression Statistics; 48
Linear Regression with Robust Method; 55
Log Gamma function; 246
Log Relative Error; 242
Logarithm in any base; 58
Logarithm natural (Napier’s); 58
M
Macro for Double Integration; 168
Macro for Multiprecision Matrix operations; 135
Macro for Triple Integration; 171
Macro Integer relation finder; 126
Macro Mesh Fill; 201
Macro Poly Regression; 51
Macro Regression; 51
Macro Sampler; 145
Macros for optimization on site; 267
Macros X-Edit; 35
Math expression strings; 263
MathParser; 265
matrix; 135
Matrix Addition; 129
Matrix Determinant; 129
Matrix Inverse; 129
Matrix LL
T
decomposition; 131
Matrix LU decomposition; 131
Matrix Modulus; 130
Matrix Multiplication; 129
Matrix Power; 130
Matrix Subtraction; 129
Maximum Common Divisor; 106
MCD; 106
MCM; 106
Mesh Interpolation 2D; 200
Minimum Common Multiple; 106
mjkLRE; 242
Modular Addition; 110
Modular Division; 110
Modular Multiplication; 110
modular power; 110
Modular Power; 110
Modular Subtraction; 110
Multiple roots; 72
Multiplication; 24
Multiprecision Base Conversion; 241
Multiprecision Excel Formula Evaluation; 261
Multiprecision Expression Evaluation; 256
N
Next Prime; 109
NextPrime; 109
Non Linear Equation Solving; 238
N-Root; 28
Numbers comparison; 31
Xnumbers Tutorial
290
O
Objective function; 267
OD Linear System; 222
ODE Implicit Predictor-Corrector; 219
ODE Multi-Steps; 211
ODE Predictor-Corrector 4; 217
ODE Runge-Kutta 4; 207
ODE_COR; 214
ODE_PC2I; 219
ODE_PC4; 217
ODE_PRE; 214
ODE_RK4; 207
ODE_SYSL; 222
Orthogonal polynomials; 92
Orthogonal Polynomials; 92
Orthogonal Polynomials evaluation; 93
oscillating; 165
P
Partial; 89
PECE; 214; 216
Perfect Square; 112
permutation; 40
Permutations; 40
Polar Conversion; 99
Poly_ChebychevT; 93
Poly_ChebychevU; 93
Poly_Gegenbauer; 93
Poly_Hermite; 93
Poly_Jacobi; 93
Poly_Laguerre; 93
Poly_Legendre; 93
Poly_Weight_ChebychevT; 95
Poly_Weight_ChebychevU; 95
Poly_Weight_Gegenbauer; 95
Poly_Weight_Hermite; 95
Poly_Weight_Jacobi; 95
Poly_Weight_Laguerre; 95
Poly_Weight_Legendre; 95
PolyAdd; 76
PolyBuild; 83
PolyBuildCfx; 85
PolyCenter; 81
PolyDiv; 77
PolyInt; 86
PolyInterp; 191
PolyInterpCoef; 191
PolyMult; 76
POLYN; 73
POLYN2; 88
Polynomial addition; 76
Polynomial building from roots; 83
Polynomial building with multi-precision; 85
Polynomial center; 81
Polynomial coefficients; 75
Polynomial derivatives; 74
Polynomial division quotient; 77
Polynomial division remainder; 77
Polynomial evaluation; 73
Polynomial interpolation; 191
Polynomial multiplication; 76
Polynomial Regression - Standard Deviation of
Estimates; 50
Polynomial Regression Statistics; 50
Polynomial roots radius; 82
Polynomial shift; 81
Polynomial solving; 86
Polynomial subtraction; 77
Polynomial System of 2nd degree; 87
Polynomial writing; 76
PolyRadius; 82
PolyRem; 77
PolyShift; 81
PolySolve; 86
PolySub; 77
Polyterms; 80
PolyTerms; 75
predictor; 211
Predictor; 212
Predictor; 214
Prime; 109
Prime Numbers Generator; 117
Prime Test; 117
PrimeGenerator; 117
Product; 27
PSLQ; 122
Q
Quadratic Mean; 40
R
Raise to power; 27
Rational Fraction approximation; 107
Rectangular Conversion; 99
RegLinMM; 56
RegLinRM; 55
regression; 272
Relative Rounding; 30
Root Error Estimation; 68
Rounding; 29
S
Scalar Product; 130
Scientific Format; 34
Secant; 232
Second Derivative; 235
Serie_trig; 186
Serie2D_trig; 188
Series acceleration with ∆^2; 183
Series Evaluation; 182
sign; 31
Significant Digits count; 31
Similarity Transform; 130
Simplex; 268
Sin; 61
Sine Integral Si(x); 249
SinIntegral; 249
Solve Linear Equation System; 132
SortRange; 33
Spherical Bessel functions of integer order; 255
Split scientific format; 34
Square Delta Extrapolation; 133
Square Root; 28
Standard Deviation; 40
Sub-Tabulation; 53
Subtraction; 23
Sum; 27
sumDigits; 33
SYSPOLY2; 87
Xnumbers Tutorial
291
T
Tan; 62
tanh-sinh transformation; 152
Totient; 121
Trigonometric double serie; 188
Trigonometric series; 186
Truncating; 29
U
Univariate Statistic; 42
V
Variance; 41
Vector flip; 33
Vector Product; 132
W
Weight of Orhogonal Polynomials; 95
X
x2pi; 62
xabs; 28
xacos; 62
xacosh; 60
xadd; 23
xaddmod; 110
xanglecompl; 63
xasin; 62
xasinh; 59
xatan; 62
xatanh; 60
xBaseChange; 241
xBeta; 247
xBetai; 248
xCalc; 261
xcdbl; 34
xcomb; 39
xcomb_big; 248
xcomp; 31
xcos; 61
xcosh; 60
xcplxabs; 99
xcplxadd; 98
xcplxconj; 101
xcplxdiv; 98
xcplxExp; 101
xcplxinv; 101
xcplxLn; 101
xcplxmult; 98
xcplxneg; 101
xcplxpolar; 99
xcplxpow; 100
xcplxrect; 99
xcplxroot; 100
xcplxsub; 98
xcvexp; 34
xdec; 29
xDgt; 31
xdiv; 24
xdivint; 24
xdivmod; 110
xdivrem; 25
xe; 59
xeu; 60
xeval; 26; 256
xevall; 256
xexp; 58
xfact; 39
xfact2; 39
xFib; 250
xFormat; 32
xfrac; 107
xFractCont; 108
xGammaF; 247
xGammaI; 248
xGammaln; 246
xGammalog; 246
xGammaQ; 246
xGm; 245
xgmean; 40
xint; 29
xinv; 24
xIsInteger; 112
xIsOdd; 112
xIsSquare; 112
xIsXnumber; 32
xLn; 58
xLn10; 59
xLn2; 59
xLog; 58
xLRE; 242
xMatAbs; 130
xMatAdd; 129
xMatBAB; 130
xMatDet; 129
xMatInv; 129
xMatLL; 131
xMatLU; 131
xMatMult; 129
xMatPow; 130
xMatSub; 129
xMCD; 106
xMCM; 106
xmean; 40
xmult; 24
xmultmod; 110
xneg; 23; 28
xpi; 62
xpi2; 62
xpi4; 62
xpow; 27
xpowmod; 110
xProdScal; 130
xProdVect; 132
xqmean; 40
xRegLinCoef; 43
xRegLinCoef; 45
xRegLinEval; 49
xroot; 28
xround; 29
xroundr; 30
xSerie2D; 185
xsin; 61
xsinh; 59
xsplit; 34
xsqr; 28
xstatis; 42
xstdev; 40
xstdevp; 40
Xnumbers Tutorial
292
xsub; 23
xsubmod; 110
xsum; 27
xSYSLIN; 132
xtan; 62
xtanh; 60
xtrunc; 29
xUnformat; 32
xvar; 41
xvarp; 41
Z
Zero_bisec; 231
Zero_sec; 232
Zeros of Orthogonal Polynomials; 95
Zeta; 251
Zeta function; 251
Xnumbers Tutorial
293
 2007, by Foxes Team
ITALY
Oct 2007

More Related Content

DOCX
fenomenos de transporte (1)
PPTX
La transformada de Laplace directa e inversa.pptx
PPTX
Series de fourier en el área de la ingenieria
PDF
Taller flex y bison
PPTX
Materiales fotónicos
PPTX
Defectos y no estequiometría
PPTX
1.2.A. ECUACIÓN GENERAL DE BALANCE.pptx.
PDF
Sistemas+masa+ +resorte+movimiento+libre+amortiguado
fenomenos de transporte (1)
La transformada de Laplace directa e inversa.pptx
Series de fourier en el área de la ingenieria
Taller flex y bison
Materiales fotónicos
Defectos y no estequiometría
1.2.A. ECUACIÓN GENERAL DE BALANCE.pptx.
Sistemas+masa+ +resorte+movimiento+libre+amortiguado

What's hot (20)

PPTX
Transformada de una Derivada
PDF
Automatas y compiladores clase1
PDF
Portafolio Lenguajes y Autómatas Unidad 1
ODP
Tema 5 maquina frigo y bomba calor
PDF
Analisis sintactico
PDF
Diseño y seleccion de materiales / Tecnología de Materiales
PPTX
La revolucion que nos rodea Diseñando el Futuro
PPTX
Intercambiadores de calor
PDF
Sedimentacion pis
PDF
Guia reacciones múltiples
PDF
Primera ley de Termodinámica
PPTX
Tolerancias y especificaciones
PPTX
Expocicion de matematica para ingenieros iii ecuacion del calor
PDF
Practica 11 Perfil de Temperatura para una Barra de Metal
DOCX
Definición de aleta
DOC
95381898 pracitca-2-tubos-concentricos
PPTX
Extraccion liquido liquido G-6.pptx
PPTX
Muestreo Aceptacion
DOCX
G2 monografia transformada de laplace
PPTX
Seis grandes-perdidas-en-mantenimiento-tpm (1)
Transformada de una Derivada
Automatas y compiladores clase1
Portafolio Lenguajes y Autómatas Unidad 1
Tema 5 maquina frigo y bomba calor
Analisis sintactico
Diseño y seleccion de materiales / Tecnología de Materiales
La revolucion que nos rodea Diseñando el Futuro
Intercambiadores de calor
Sedimentacion pis
Guia reacciones múltiples
Primera ley de Termodinámica
Tolerancias y especificaciones
Expocicion de matematica para ingenieros iii ecuacion del calor
Practica 11 Perfil de Temperatura para una Barra de Metal
Definición de aleta
95381898 pracitca-2-tubos-concentricos
Extraccion liquido liquido G-6.pptx
Muestreo Aceptacion
G2 monografia transformada de laplace
Seis grandes-perdidas-en-mantenimiento-tpm (1)
Ad

Recently uploaded (20)

PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Module 8- Technological and Communication Skills.pptx
PPTX
Fundamentals of Mechanical Engineering.pptx
PPT
Total quality management ppt for engineering students
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
Software Engineering and software moduleing
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Soil Improvement Techniques Note - Rabbi
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
Exploratory_Data_Analysis_Fundamentals.pdf
Module 8- Technological and Communication Skills.pptx
Fundamentals of Mechanical Engineering.pptx
Total quality management ppt for engineering students
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Software Engineering and software moduleing
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Soil Improvement Techniques Note - Rabbi
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
III.4.1.2_The_Space_Environment.p pdffdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
Information Storage and Retrieval Techniques Unit III
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
Ad

Xnumbers tutorial1

  • 1. FOXES TEAM Reference for Xnumbers.xla Numeric Calculus in Excel Volume 1x
  • 2. R E F E R ENC E F O R XN U MB ER S . X L A Numeric Calculus in EXCEL Oct 2007
  • 3. Xnumbers Tutorial 2 Index About this Tutorial ..................................................................................................................... 8 Array functions........................................................................................................................... 9 What is an array-function? ....................................................................................................... 9 How to insert an array function ................................................................................................ 9 How to get the help on line...................................................................................................... 12 Xnumbers installation.............................................................................................................. 13 How to install.......................................................................................................................... 13 How to uninstall...................................................................................................................... 14 Installation troubles ................................................................................................................ 14 Multiprecision Floating Point Arithmetic............................................................................... 15 Why using extended precision numbers? .............................................................................. 15 Multiprecision methods........................................................................................................... 17 How to store long number ...................................................................................................... 17 Functions .................................................................................................................................. 19 General Description................................................................................................................ 19 Using Xnumbers functions ..................................................................................................... 19 Using extended numbers in Excel.......................................................................................... 20 Functions Handbook .............................................................................................................. 21 Precision................................................................................................................................. 22 Formatting Result................................................................................................................... 22 Arithmetic Functions............................................................................................................... 23 Addition............................................................................................................................................. 23 Subtraction........................................................................................................................................ 23 Multiplication..................................................................................................................................... 24 Division ............................................................................................................................................. 24 Inverse.............................................................................................................................................. 24 Integer Division................................................................................................................................. 24 Integer Remainder ............................................................................................................................ 25 Sum .................................................................................................................................................. 27 Product ............................................................................................................................................. 27 Raise to power.................................................................................................................................. 27 Square Root...................................................................................................................................... 28 N th - Root ........................................................................................................................................... 28 Absolute............................................................................................................................................ 28 Change sign...................................................................................................................................... 28 Integer part ....................................................................................................................................... 29 Decimal part...................................................................................................................................... 29 Truncating......................................................................................................................................... 29 Rounding .......................................................................................................................................... 29 Relative Rounding ............................................................................................................................ 30 Extended Numbers manipulation........................................................................................... 31 Digits count....................................................................................................................................... 31 Significant Digits count...................................................................................................................... 31 Numbers comparison........................................................................................................................ 31 Extended number check ................................................................................................................... 32 Format extended number.................................................................................................................. 32 Check digits ...................................................................................................................................... 32 SortRange......................................................................................................................................... 33 Digits sum......................................................................................................................................... 33 Vector flip.......................................................................................................................................... 33 Scientific format ................................................................................................................................ 34 Split scientific format......................................................................................................................... 34 Converting multiprecision into double ............................................................................................... 34 Macros X-Edit ................................................................................................................................... 35 Macro X-Converter ........................................................................................................................... 36 Statistical Functions ............................................................................................................... 39 Factorial............................................................................................................................................ 39 Factorial with double-step................................................................................................................. 39
  • 4. Xnumbers Tutorial 3 Combinations.................................................................................................................................... 39 Permutations..................................................................................................................................... 40 Arithmetic Mean................................................................................................................................ 40 Geometric Mean ............................................................................................................................... 40 Quadratic Mean ................................................................................................................................ 40 Standard Deviation ........................................................................................................................... 40 Variance............................................................................................................................................ 41 Probability distributions..................................................................................................................... 41 Univariate Statistic ............................................................................................................................ 46 Linear Regression Coefficients......................................................................................................... 47 Linear Regression - Standard Deviation of Estimates ...................................................................... 49 Linear Regression Formulas............................................................................................................. 50 Linear Regression Covariance Matrix............................................................................................... 51 Linear Regression Statistics ............................................................................................................. 52 Linear Regression Evaluation........................................................................................................... 53 Polynomial Regression - Coefficient ................................................................................................. 54 Polynomial Regression - Standard Deviation of Estimates............................................................... 54 Polynomial Regression Statistics...................................................................................................... 54 Macro - Regression .......................................................................................................................... 55 Linear Regression with Robust Method ............................................................................................ 59 Linear Regression Min-Max.............................................................................................................. 60 NIST Certification Test...................................................................................................................... 61 Transcendental Functions...................................................................................................... 62 Logarithm natural (Napier’s) ............................................................................................................. 62 Logarithm for any base ..................................................................................................................... 62 Exponential....................................................................................................................................... 62 Exponential for any base .................................................................................................................. 62 Constant e ....................................................................................................................................... 63 Constant Ln(2).................................................................................................................................. 63 Constant Ln(10)................................................................................................................................ 63 Hyperbolic Sine................................................................................................................................. 63 Hyperbolic ArSine ............................................................................................................................. 63 Hyperbolic Cosine............................................................................................................................. 64 Hyperbolic ArCosine ......................................................................................................................... 64 Hyperbolic Tangent........................................................................................................................... 64 Hyperbolic ArTangent ....................................................................................................................... 64 Euler constant γ ................................................................................................................................ 64 Trigonometric Functions......................................................................................................... 65 Sin .................................................................................................................................................... 65 Cos ................................................................................................................................................... 65 Tan ................................................................................................................................................... 66 Arcsine.............................................................................................................................................. 66 Arccosine.......................................................................................................................................... 66 Arctan ............................................................................................................................................... 66 Constant π ........................................................................................................................................ 66 Complement of right angle................................................................................................................ 67 Polynomial Rootfinder ............................................................................................................ 68 Input parameters............................................................................................................................... 69 Output............................................................................................................................................... 70 How to use rootfinder macros........................................................................................................... 71 Integer roots...................................................................................................................................... 74 Multiple roots .................................................................................................................................... 76 Polynomial Functions ............................................................................................................. 77 Polynomial evaluation....................................................................................................................... 77 Polynomial derivatives ...................................................................................................................... 78 Polynomial coefficients ..................................................................................................................... 79 Polynomial writing............................................................................................................................. 80 Polynomial addition........................................................................................................................... 80 Polynomial multiplication................................................................................................................... 80 Polynomial subtraction...................................................................................................................... 81 Polynomial division quotient.............................................................................................................. 81 Polynomial division remainder .......................................................................................................... 81 Polynomial shift................................................................................................................................. 85 Polynomial center ............................................................................................................................. 85 Polynomial roots radius..................................................................................................................... 86 Polynomial building from roots.......................................................................................................... 87 Polynomial building with multi-precision............................................................................................ 89 Polynomial Solving ........................................................................................................................... 90 Integer polynomial ............................................................................................................................ 90
  • 5. Xnumbers Tutorial 4 Polynomial System of 2 nd degree...................................................................................................... 91 Bivariate Polynomial ......................................................................................................................... 92 Orthogonal Polynomials evaluation .................................................................................................. 97 Weight of Orhogonal Polynomials..................................................................................................... 99 Zeros of Orthogonal Polynomials...................................................................................................... 99 Coefficients of Orthogonal Polynomials .......................................................................................... 100 Complex Arithmetic and Functions ...................................................................................... 101 How to insert a complex number .................................................................................................... 101 Complex Addition............................................................................................................................ 102 Complex Subtraction ...................................................................................................................... 102 Complex Multiplication.................................................................................................................... 102 Complex Division ............................................................................................................................ 102 Polar Conversion ............................................................................................................................ 103 Rectangular Conversion ................................................................................................................. 103 Complex absolute ........................................................................................................................... 103 Complex power............................................................................................................................... 104 Complex Roots ............................................................................................................................... 104 Complex Log................................................................................................................................... 105 Complex Exp .................................................................................................................................. 105 Complex inverse............................................................................................................................. 105 Complex negative ........................................................................................................................... 105 Complex conjugate ......................................................................................................................... 105 Complex Sin ................................................................................................................................... 106 Complex Cos .................................................................................................................................. 106 Complex Tangent ........................................................................................................................... 106 Complex ArcCos............................................................................................................................. 106 Complex ArcSin.............................................................................................................................. 106 Complex ArcTan ............................................................................................................................. 106 Complex Hyperbolic Sine................................................................................................................ 106 Complex Hyperbolic Cosine............................................................................................................ 107 Complex Hyperbolic Tan................................................................................................................. 107 Complex Inverse Hyperbolic Cos.................................................................................................... 107 Complex Inverse Hyperbolic Sin..................................................................................................... 107 Complex Inverse Hyperbolic Tan.................................................................................................... 107 Complex digamma.......................................................................................................................... 107 Complex Exponential Integral......................................................................................................... 107 Complex Error Function.................................................................................................................. 108 Complex Complementary Error Function........................................................................................ 108 Complex Gamma Function ............................................................................................................. 108 Complex Logarithm Gamma Function ............................................................................................ 108 Complex Zeta Function................................................................................................................... 108 Complex Quadratic Equation.......................................................................................................... 109 Number Theory .................................................................................................................... 110 Maximum Common Divisor............................................................................................................. 110 Minimum Common Multiple ............................................................................................................ 110 Rational Fraction approximation ..................................................................................................... 111 Continued Fraction ......................................................................................................................... 112 Continued Fraction of Square Root ................................................................................................ 113 Check Prime ................................................................................................................................... 113 Next Prime...................................................................................................................................... 113 Modular Addition............................................................................................................................. 114 Modular Subtraction........................................................................................................................ 114 Modular Multiplication ..................................................................................................................... 114 Modular Division ............................................................................................................................. 114 Modular Power................................................................................................................................ 114 Perfect Square................................................................................................................................ 116 Check odd/even.............................................................................................................................. 116 Check Integer ................................................................................................................................. 116 Macro - Factorize............................................................................................................................ 117 Batch Factorization with Msieve ..................................................................................................... 118 Factorization function...................................................................................................................... 120 Macro - Prime Numbers Generator................................................................................................. 121 Prime Test ...................................................................................................................................... 121 Diophantine Equation ..................................................................................................................... 123 Brouncker-Pell Equation ................................................................................................................. 124 Euler's Totient function ................................................................................................................... 125 Integer relation................................................................................................................................ 126 Macro Integer Relation Finder ........................................................................................................ 130 Linear Algebra Functions ..................................................................................................... 133 Matrix Addition................................................................................................................................ 133
  • 6. Xnumbers Tutorial 5 Matrix Subtraction........................................................................................................................... 133 Matrix Multiplication ........................................................................................................................ 133 Matrix Inverse ................................................................................................................................. 133 Matrix Determinant ......................................................................................................................... 133 Matrix Modulus ............................................................................................................................... 134 Scalar Product ................................................................................................................................ 134 Similarity Transform........................................................................................................................ 134 Matrix Power................................................................................................................................... 134 Matrix LU decomposition ................................................................................................................ 135 Matrix LL T decomposition ............................................................................................................... 135 Vector Product................................................................................................................................ 136 Solve Linear Equation System........................................................................................................ 136 Square Delta Extrapolation............................................................................................................. 137 Macro for Multiprecision Matrix Operations..................................................................................... 139 Integrals & Series................................................................................................................. 141 Discrete Fourier Transform............................................................................................................. 141 Discrete Fourier Inverse Transform ................................................................................................ 143 Discrete Fourier Spectrum.............................................................................................................. 144 Inverse Discrete Fourier Spectrum ................................................................................................. 144 2D Discrete Fourier Transform ....................................................................................................... 145 2D Inverse Discrete Fourier Transform........................................................................................... 145 Macro DFT (Discrete Fourier Transform)........................................................................................ 146 Macro Sampler ............................................................................................................................... 149 Integral function .............................................................................................................................. 151 Zeros of integral function ................................................................................................................ 154 Function Integration (Romberg method) ......................................................................................... 155 Function Integration (Double Exponential method)......................................................................... 156 Function Integration (mixed method) .............................................................................................. 158 Complex Function Integration (Romberg method).......................................................................... 160 Data Integration (Newton-Cotes) .................................................................................................... 161 Function Integration (Newton-Cotes) .............................................................................................. 163 Integration: symbolic and numeric approaches............................................................................... 165 Integration of oscillating functions (Filon formulas) ......................................................................... 166 Integration of oscillating functions (Fourier transform) .................................................................... 168 Infinite Integration of oscillating functions ....................................................................................... 169 Double Integral ............................................................................................................................... 172 Macro for Double Integration .......................................................................................................... 172 Macro for Triple Integration............................................................................................................. 175 Double integration function ............................................................................................................. 178 Infinite integral ................................................................................................................................ 181 Double Data integration .................................................................................................................. 183 Series Evaluation............................................................................................................................ 186 Series acceleration with ∆^2 ........................................................................................................... 187 Complex Series Evaluation............................................................................................................. 188 Double Series ................................................................................................................................. 189 Trigonometric series ....................................................................................................................... 190 Trigonometric double serie ............................................................................................................. 192 Discrete Convolution....................................................................................................................... 193 Interpolation.......................................................................................................................... 195 Polynomial interpolation.................................................................................................................. 195 Interpolation schemas..................................................................................................................... 197 Interpolation with continued fraction ............................................................................................... 199 Interpolation with Cubic Spline........................................................................................................ 201 Cubic Spline 2nd derivatives........................................................................................................... 201 Cubic Spline Coefficients................................................................................................................ 203 2D Mesh Interpolation..................................................................................................................... 204 Macro Mesh Fill .............................................................................................................................. 205 Differential Equations ........................................................................................................... 211 ODE Runge-Kutta 4........................................................................................................................ 211 ODE Multi-Steps ............................................................................................................................. 215 Multi-step coefficents tables............................................................................................................ 216 Predictor- Corrector ........................................................................................................................ 218 PECE algorithm of 2 nd order ........................................................................................................... 218 ODE Predictor-Corrector 4.............................................................................................................. 221 ODE Implicit Predictor-Corrector .................................................................................................... 223 Differential Systems........................................................................................................................ 226 OD Linear System .......................................................................................................................... 226 High order linear ODE..................................................................................................................... 227 ODE for integral function solving .................................................................................................... 230
  • 7. Xnumbers Tutorial 6 Macro ODE Solver.......................................................................................................................... 232 Macro ODE - Slope Grid................................................................................................................. 233 Nonlinear Equations............................................................................................................. 235 Bisection ......................................................................................................................................... 235 Secant............................................................................................................................................. 236 Derivatives............................................................................................................................ 239 First Derivative................................................................................................................................ 239 Second Derivative........................................................................................................................... 240 Gradient.......................................................................................................................................... 240 Jacobian matrix............................................................................................................................... 241 Hessian matrix................................................................................................................................ 241 Non-linear equation solving with derivatives................................................................................... 243 Conversions ......................................................................................................................... 245 Decibel............................................................................................................................................ 245 Base conversion ............................................................................................................................. 245 Multiprecision Base Conversion...................................................................................................... 246 Log Relative Error........................................................................................................................... 247 Special Functions................................................................................................................. 249 Error Function Erf(x) ...................................................................................................................... 249 Exponential integral Ei(x)................................................................................................................ 249 Exponential integral En(x)............................................................................................................... 249 Euler-Mascheroni Constant γ......................................................................................................... 250 Gamma function Γ(x) ...................................................................................................................... 250 Log Gamma function ...................................................................................................................... 251 Gamma quotient ............................................................................................................................. 251 Gamma F-factor.............................................................................................................................. 252 Digamma function........................................................................................................................... 252 Beta function................................................................................................................................... 252 Incomplete Gamma function........................................................................................................... 253 Incomplete Beta function ................................................................................................................ 253 Combinations function .................................................................................................................... 253 Bessel functions of integer order ................................................................................................... 254 Cosine Integral Ci(x) ....................................................................................................................... 254 Sine Integral Si(x) ........................................................................................................................... 254 Fresnel sine Integral ....................................................................................................................... 255 Fresnel cosine Integral.................................................................................................................... 255 Fibonacci numbers ......................................................................................................................... 255 Hypergeometric function................................................................................................................. 256 Zeta function ζ(s) ........................................................................................................................... 256 Airy functions .................................................................................................................................. 257 Elliptic Integrals............................................................................................................................... 257 Kummer confluent hypergeometric functions.................................................................................. 258 Integral of sine-cosine power.......................................................................................................... 259 Spherical Bessel functions of integer order..................................................................................... 260 Formulas Evaluation............................................................................................................. 261 Multiprecision Expression Evaluation ............................................................................................. 261 Complex Expression Evaluation ..................................................................................................... 265 Multiprecision Excel Formula Evaluation ........................................................................................ 266 Math expression strings.................................................................................................................. 268 List of basic functions and operators .............................................................................................. 271 Function Optimization........................................................................................................... 274 Macros for optimization on site ....................................................................................................... 274 Example 1 - Rosenbrock's parabolic valley .................................................................................... 276 Example 2 - Constrained minimization ........................................................................................... 277 Example 3 - Nonlinear Regression with Absolute Sum................................................................... 279 Example 4 - Optimization of Integral function ................................................................................. 280 How to call Xnumbers functions from VBA ......................................................................... 281 References & Resources....................................................................................................... 284 Analytical index...................................................................................................................... 287
  • 9. Xnumbers Tutorial 8 About this tutorial About this Tutorial This document is the reference guide for all functions and macros contained in the Xnumbers addin. It is a printable version of the help-on-line, with a larger collection of examples. XNUMBERS.XLA is an Excel addin containing useful functions for numerical calculus in standard and multiprecision floating point arithmetic up to 200 significant digits. The main purpose of this document is to be a reference guide for the functions of this package, showing how to work with multiprecision arithmetic in Excel. The most part of the material contained in this document comes from the Xnumbers help-on-line. You may print it in order to have a handle paper manual. Of course it speaks about math and numerical calculus but this is not a math book. You rarely find here theorems and demonstrations. You can find, on the contrary, many explaining examples. Special thanks to everyone that have kindly collaborated. Leonardo Volpi Chapter 1
  • 10. Xnumbers Tutorial 9 Array functions What is an array-function? A function that returns multiple values is called "array-function". Xnumbers contains many of these functions. Each function returning a matrix or a vector is an array functions. Function performing matrix operations such as inversion, multiplication, sum, etc. are examples of array- functions. Also complex numbers are arrays of two cells. On the contrary, in the real domain, the logarithm, the exponential, the trigonometric functions, etc. are scalar functions because they return only one value. In a worksheet, an array-function always returns a (n x m) rectangular range of cells. To enter it, you must select this range, enter the function as usually and and give the keys sequence CTRL+SHIFT+ENTER. Keep down both keys CTRL and SHIFT (do not care the order) and then press ENTER. How to insert an array function The following example explains, step-by-step, how to insert an array-function The System Solution Assume to have to solve a 3x3 linear system. The solution is a vector of 3 values. Ax = b Where: The function SYSLIN returns the vector solution x, that is an (3 x 1) array. To see the three values you must select before the area where you want to insert these values. Now insert the function either from menu or by the icon Select the area of the matrix A "A5:C7" and the constant vector b "E5:E7"           = 431 221 111 A           = 3 2 4 b
  • 11. Xnumbers Tutorial 10 Now - attention! - give the "magic" keys sequence CTRL+SHIFT+ENTER That is: • Press and keep down the CTRL and SHIFT keys • Press the ENTER key All the values will fill the cells that you have selected. Note that Excel shows the function around two braces { }. These symbols mean that the function return an array (you cannot insert them by hand). An array function has several constrains. Any cell of the array cannot be modified or deleted. To modify or delete an array function you must selected before the entire array cells. Adding two matrices The CTRL+SHIFT+ENTER rule is valid for any function and/or operation returning a matrix or a vector Example - Adding two matrices       +      − 10 01 12 21 We can use directly the addition operator "+". We can do this following these steps. 1) Enter the matrices into the spreadsheet. 2) Select the B8:C9 empty cells so that a 2 × 2 range is highlighted. 3) Write a formula that adds the two ranges. Either write =B4:C5+E4:F5 Do not press <Enter>. At this point the spreadsheet should look something like the figure below. Note that the entire range B8:C9 is selected.
  • 12. Xnumbers Tutorial 11 4) Press and hold down <CTRL> + <SHIFT> 5) Press <ENTER>. If you have correctly followed the procedure, the spreadsheet should now look something like this This trick can also work for matrix subtraction and for the scalar-matrix multiplication, but not for the matrix-matrix multiplication. Let's see this example that shows how to calculate the linear combination of two vectors Functions returning optional values Some function, such as for example the definite integral of a real function f(x), can return one single value and optional extra data (iterations, error estimation, etc...) If you do not want to see this additional information simply select one cell and insert the function with the standard procedure. On the contrary, if you want to see also the extra information, you must select the extra needed cells and insert it as an array-function
  • 13. Xnumbers Tutorial 12 How to get the help on line Xnumbers provides the help on line that can be recalled in the same way of any other Excel function. When you have selected the function that you need, press the F1 key or click on the “guide hyperlink” There is also another way to get the help-on-line. It is from the Xnumbers Function Handbook Select the function that you want and press the Help button You can also recall the help guide from the function wizard window Of course you can open the help on-line from the Xnumber menu or directly by double clicking on the Xnumbers.hlp file
  • 14. Xnumbers Tutorial 13 Xnumbers installation How to install This addin for Excel XP is composed by the following files: Addin file (It contains the Excel macros and functions) Help file (It contains the help notes) Handbook file (It contains the macros and functiosn description list for the Xnumbers Handbook) This installation is entirely contained in the folder that you specify. Put all these files in a same directory as you like. Open Excel and follow the usually operations for the addin installation: 1) Select <addins...> from <tools> menu, 2) Excel shows the Addins Manager, 3) Search for the file xnumbers.xla, 4) Press OK, After the first installation, Xnumbers.xla will be add to the Addin Manager By this tool, you can load or unload the addins that you want, simply switching on/off the check- boxes. At the starting, the checked addins will be automatically loaded If you want to stop the automatic loading of xnumbers.xla simply deselect the check box before closing Excel If all goes right you should see the welcome popup of Xnumbers. This appears only when you activate the check box of the Addin Manager. When Excel automatically loads Xnumbers, this popup is hidden .
  • 15. Xnumbers Tutorial 14 How to uninstall If you want to uninstall this package, simply delete its folder. Once you have cancelled the Xnumbers.xla file, to remove the corresponding entry in the Addin Manager, follow these steps: 1) Open Excel 2) Select <Addins...> from the <Tools> menu. 3) Once in the Addins Manager, click on the Xnumbers entry 4) Excel will inform you that the addin is missing and ask you if you want to remove it from the list. Give "yes". Installation troubles If you do not see the Xnumbers icon or the welcome popup the installation has gone wrong and probably it is due to same configuration parameter of your Windows / Excel environment The default security setting of Excel XP 2002/2003 doesn't allowe to install Xnumbers as is. To prevent errors it is necessary to open the Excel menu option: Tools > Options > Security > Macro Security, and set: "Average Level security" and "Trust Access to Visual Basic Project". Do not worry about it. Xnumbers contains only safe macros; they do not alter your PC in any way. If you want to remove Xnumbers simply delete all the files contained in the package. In addition we have to point out that, nowadays, nobody use to hide virus, spy-software, trojan and related things in a macro Excel: this things belong to the prehistory of informatics! Nowadays, E-mail and Internet are by far the most important media.
  • 16. Xnumbers Tutorial 15 Multiprecision Floating Point Arithmetic Any computer having hardware at 32-bit can perform arithmetic operations with 15 significant digits1 . The only way to overcome this finite fixed precision is to adopt special software that extends the accuracy of the native arithmetic Why using extended precision numbers? First of all, for example, to compute the following operation: 90000000002341 x 8067 = -------------------- 726030000018884847 . Any student, with a little work, can do it. Excel, as any 32-bit machine, cannot! It always gives the (approximate) result 726030000018885000 , with a difference of +153. But do not ask Excel for the difference. It replies 0! The second, deeper, example regards numeric analysis. Suppose we have to find the roots of the following 9th degree polynomial. ∑= = n i i i xaxP 0 )( There are excellent algorithms for finding a numerical solution of this problem. We can use the Newton-Raphson method: starting from x = 32 and operating with 15 significant digits (the maximum for Excel), we have: Coefficients a9 1 a8 -279 a7 34606 a6 -2504614 a5 116565491 a4 -3617705301 a3 74873877954 a2 -996476661206 a1 7738306354988 a0 -26715751812360 xn P(x) (15 digit) P' (x) (15 digit) -P/P' |xn-x| 32 120 428 0,280373832 1 31,71962617 43,77734375 158,9873047 0,275351191 0,7196262 31,44427498 15,69921875 60,93164063 0,257652979 0,444275 31,186622 4,78125 29,46289063 0,162280411 0,186622 31,02434159 0,65625 24,10644531 0,02722301 0,0243416 30,99711858 -0,07421875 24,01953125 -0,003089933 0,0028814 31,00020851 0,23828125 24,04980469 0,009907825 0,0002085 30,99030069 -0,52734375 23,98925781 -0,021982495 0,0096993 31,01228318 0,2421875 24,02050781 0,01008253 0,0122832 31,00220065 -0,03515625 23,99023438 -0,00146544 0,0022007 As we can see, the iteration approaches the solution x = 31 but the error |xn - x| remains too high. Why? Multiple roots? No, because P'(x) >> 0. Algorithm failed? Of course not. This method is very well tested. The only explanation is the finite precision of the computing. In fact, repeating the calculus of P(x) and P'(x) with 25 significant digits, we find the excellent convergence of this method. 1 The basic structure is the IEEE-754 which allows for a 64 bit standard floating point double precision number. The later has a mantissa of 53 bits (one is the implied bit) which becomes equivalent to 15.4 digits. Excel reports a maximum of 15 digits. For a good, accurate note see "Excel Computation and Display Issue" by David. A. Heiser, http://www.daheiser.info/excel/frontpage.html
  • 17. Xnumbers Tutorial 16 xn P(x) (25 digit) P' (x) (25 digit) -P/P' |xn-x| 32 120 428 0,28037383 1 31,71962617 43,71020049043 158,979858019937 0,27494175 0,719626 31,44468442 15,71277333004 61,059647049872 0,25733482 0,444684 31,1873496 4,83334748037 29,483621556222 0,1639333 0,18735 31,02341629 0,56263326884 24,082301045236 0,02336294 0,023416 31,00005336 0,00128056327 24,000000427051 5,3357E-05 5,34E-05 31 0,00000000053 23,999999999984 2,2083E-11 1,54E-11 31 0,00000000004 23,999999999995 1,6667E-12 6,66E-12 The graph below resumes the effect of computation with 15 and 25 significant digits. 1E-12 1E-10 1E-08 1E-06 0,0001 0,01 1 1 2 3 4 5 6 7 8 9 10 |Xn -X| 15 digits 25 digits Iteration The application field of multi-precision computation is wide. Especially it is very useful for numeric algorithms testing. In the above example, we had not doubt about the Newton- Raphson method, but what about the new algorithm that you are studying? This package helps you in this work.
  • 18. Xnumbers Tutorial 17 Multiprecision methods Several methods exist for simulating variable multi-precision floating point arithmetic. The basic concept consists of breaking down a long number into two or more sub-numbers, and repeating cyclic operations with them. The ways in which long numbers are stored vary from one method to another. The two most popular methods use the "string" conversion and the "packing" How to store long number String Extended Numbers In this method, long numbers are stored as vectors of characters, each representing a digit in base 256. Input numbers are converted from decimal to 256 base and vice versa for output. All internal computations are in 256 base. this requires only 16 bit for storing and a 32 bit accumulator for computing. Here is an example of how to convert the number 456789 into string ( ) ( )25610 85248,,6456789 ≡ String = chr(6)&chr(248)&chr(85) This method is very fast, and efficient algorithms for the input-output conversion have been realized. A good explanation of this method can be found in "NUMERICAL RECIPES in C - The Art of Scientific Computing", Cambridge University Press, 1992, pp. 920-928. In this excellent work you can also find efficient routines and functions to implement an arbitrary-precision arithmetic. Perhaps the most critical factor of this method is the debug and test activity. It will be true that the computer does not care about the base representation of numbers, but programmers usually do it. During debugging, programmers examine lots and lots of intermediate results, and they must always translate them from base 256 to 10. For this kind of programs, the debugging and tuning activity usually takes 80 - 90% of the total develop time. Packet Extended Numbers This method avoids converting the base representation of long numbers and stores them as vectors of integers. This is adopted in all FORTRAN77 routines of "MPFUN: A MULTIPLE PRECISION FLOATING POINT COMPUTATION PACKAGE" by NASA Ames Research Center. For further details we remand to the refined work of David H. Bailey published in "TRANSACTIONS ON MATHEMATICAL SOFTWARE", Vol. 19, No. 3, SEPTEMBER, 1993, pp. 286-317. Of course this add-in does not have the performance of the mainframe package (16 million digits) but the method is substantially the same. Long numbers are divided into packets of 6 or 7 digits. For example, the number 601105112456789 in packet form of 6 digits becomes the following integer vector: 456789 105112 601 As we can see, the sub-packet numbers are in decimal base and the original long number is perfectly recognizable. This a great advantage for the future debugging operation. An example of arithmetic operation - the multiplication A x B = C - between two packet numbers is shown in the following: A B 456789 654321 105112 X 601 The schema below illustrates the algorithm adopted:
  • 19. Xnumbers Tutorial 18 carry A B C' C 0 + 456789 x 654321 = 298886635269 => 635269 298886 + 105112 x 654321 = 68777287838 => 287838 68777 + 601 x 654321 = 393315698 => 315697 393 + 0 x 654321 = 393 => 393 The numbers in the accumulator C' are split into two numbers. The last 6 digits are stored in C, the remaining left digits are copied into the carry register of the next row. As we can see, the maximum number of digits is reached in accumulator C'. In the other vectors, the numbers require only six digits at most. The maximum number of digits for a single packet depends of the hardware accumulator. Normally, for a 32-system, is 6 digits.. This is equivalent to conversion from a decimal to a 10^6 representation base. This value is not critical at all. Values from 4 to 7 affect the computation speed of about 30 %. But it does not affect the precision of the results in any case.
  • 20. Xnumbers Tutorial 19 Functions General Description Xnumbers is an Excel addin (xla) that performs multi-precision floating point arithmetic. Perhaps the first package providing functions for Excel with precision from 15 up to 200 significant digits. It is compatible with Excel XP and consists of a set of more than 270 functions for arithmetic, complex, trigonometric, logarithmic, exponential and matrix calculus covering the following main subjects. The basic arithmetical functions: addition, multiplication, and division were developed at the first. They form the basic kernel for all other functions. All functions perform multiprecision floating point computations for up to 200 significant digits. You can set a precision level separately for each function by an optional parameter. By default, all functions use the precision of 30 digits, but the numerical precision can easily be regulated continually from 1 to 200 significant digits. In advance some useful constants like π, Log(2), Log(10) are provided with up to 400 digits. Using Xnumbers functions These functions can be used in an Excel worksheet as any other built-in function. After the installation, look up in the functions library or click on the icon Upon "user's" category you will find the functions of this package. From version 2.0 you can manage functions also by the Function Handbook. It starts by the Xnumbers menu All the functions for multi-precision computation begin with "x". The example below shows two basic functions for the addition and subtraction. As any other functions they can also be nested to build complex expressions. In the example below we compute x^4 with 30 digits precision As we can see, Xnumbers is powerful, but it does slow down the computation considerably Therefore, use the multiprecision x-functions only when they are really necessary.
  • 21. Xnumbers Tutorial 20 Using extended numbers in Excel If you try to enter a long number with more than 15 digits in a worksheet cell, Excel automatically converts it in standard precision eliminating the extra digits. The only way to preserve the accuracy is to convert the number in a string. It can be done by prefixing it with the hyphen symbol ' . This symbol is invisible in a cell but avoid the conversion. Example: enter in a cell the number 1234567890123456789. We have inserted the same number with the hyphen in B2 and without the hyphen in B3. Excel treats the first number as a string and the second as a numbers Note also the different alignment We have inserted a long numbers with full precision as a string in B2 If we try to multiply the cell B2 for another number, example for 2, Excel converts the string into number before performing the multiplication. In this way the originally accuracy is destroyed The only way to perform arithmetic operations preserving the precision is to use the multiprecision functions of the Xnumbers library. In that case we use the function xmult Note from the alignment that the result is still a string You can also insert extended numbers directly in the function. Only remember that, for preserving Excel to convert them, you must insert extended numbers like string, within quote "...". 2469135780246913578 =xmult( "1234567890123456789" , 2 )
  • 22. Xnumbers Tutorial 21 Functions Handbook Xnumbers includes an application for searching and pasting the Xnumbers functions, cataloged by subject. This application can also submit the Xnumbers macros. You can activate the Functions Handbook from the menu bar Help > Function manager. Category: you can filter macros by category (Arithmetic, Statistical, Trigonometric, etc.) Macro Type: filters by macro Functions, by macro Subroutines, or both Paste Into: choose the cell you want to paste a function, default is the active cell Search: searches macros by words or sub-words contained into the name or description. For example, if you input "div" you list all macros that match words like (div, divisor, division,...) You can associate more words in AND/OR. Separate words with comma "," for OR, with plus "+", for AND. For example, if you type "+div +multi" you will get all the rows containing words like (div, divisor, division,...) and words like (multi, multiprecision,...). On the contrary, if you type "div, multi", you get all the rows that contain words like (div, divisor, division,...) or also the words like (multi, multiprecision,...). Remember to choose also the Category and Macro Type. Example, if you enter the word “hyperbolic”, setting the Category “complex”, you find the hyperbolic functions restricted to the complex category. Help: recalls the help-on-line for the selected function. OK: insert the selected function into the worksheet ". This activates the standard Excel function wizard panel. If the macro selected is a "sub", the OK button activates the macro.
  • 23. Xnumbers Tutorial 22 Precision Most functions of this package have an optional parameter - Digit_Max - setting the maximum number of significant digits for floating point computation, from 1 to 200 (default is 30). The default can be changed from the menu X-EditDefaut Digits This parameter also determines how the output is automatically formatted. If the result has fewer integer digits than Digit_Max, then the output is in the plain decimal format ( 123.45, -0.0002364, 4000, etc.), otherwise, if the number of integer digits exceeds the maximum number of digits allowed (significant or not), the output is automatically converted in exponential format (1.23456789E+94). The exponent can reach the extreme values of +/- 2,147,483,.647. The output format is independent of the input format. In synthesis, the Digit_Max parameter limits: The significant digits of internal floating point computation The maximum number output digits, significant or not. The default of Digit_Max can be changed from the X-Edit menu . It affects all multiprecision functions and macros. Formatting Result The user can not format an extended number with standard Excel number format tools, because, it is a string for Excel. You can only change the alignment. To change it you can use the usual standard Excel format tools. It is possible to separate the digits of a x-numbers in groups, by the user function xFormat() and xUnformat() 1 . It work similar at the built-in function Format(x, "#,##0.00") 2,469,135,780,246,913,578 = xformat("2469135780246913578",3) . 1 These functions were original developed by Ton Jeursen for the add-in XNUMBER95, the downgrade version of XNUMBERS for Excel 5. Because they are very useful for examining long string of number, we have imported them in this package
  • 24. Xnumbers Tutorial 23 Arithmetic Functions Addition xadd(a, b, [Digit_Max]) Performs the addition of two extended numbers: xadd(a, b) = a + b. Subtraction xsub(a, b, [Digit_Max]) Performs the subtraction of two extended numbers: xsub(a, b) = a − b. NB. Do not use the operation xadd(a, -b) if “b” is an extended number. Excel converts “b” into double, then changes its sign, and finally calls the xadd routine. By this time the original precision of “b” is lost. If you want to change sign at an extended number and preserve its precision use the function xneg() Accuracy lack by subtraction The subtraction is a critical operation from the point of view of numeric calculus. When the operands are very near each others, this operation can cause a lack of accuracy. Of course this can happen for addition when the operands are near and have opposit signs. Let’s see this example. Assume one performs the following subtraction where the first operand has a precision of 30 significant digits (digits) 800000.008209750361424423316366 30 800000 6 0.008209750361424423316366 25 The subtraction is exact (no approximation has been entered). But the final result have 25 total digits, of wich only 22 are significant. 8 significant digits are lost in this subtraction. We cannot do anything about this phenomenon, except to increase the precision of the operands, when possible.
  • 25. Xnumbers Tutorial 24 Multiplication xmult(a, b, [Digit_Max]) Performs the multiplication of two extended numbers: xmult(a, b) = a x b. The product can often lead to long extended numbers. If the result has more integer digits than the ones set by Digit_Max, then the function automatically converts the result into the exponential format. Division xdiv(a, b, [Digit_Max]) Performs the division of two extended numbers: xdiv(a, b) = a / b. If b = 0 the function returns “?”. The division can return long extended numbers even when the operands are small. In the example below we see the well-known periodic division 122 / 7 = 17,428571 …. with 30 significant digits. Inverse xinv(x, [Digit_Max]) It returns the inverse of an extended number. If x = 0, the function returns “?”. xinv(x) = 1 / x Integer Division xdivint(a, b) Returns the quotient of the integer division for a>0 , b>0. If b = 0 the function returns “?”. xdivint(a, b)= q , where: a = b*q + r , with 0 ≤ r < b
  • 26. Xnumbers Tutorial 25 Integer Remainder xdivrem(a, b) Returns the remainder of the integer division for a>0 , b>0. If b = 0 the function returns “?”. xdivrem(a, b)= r , where: a = b*q + r , with 0 ≤ r < b This function is also called "a mod b " How to test multiprecision functions ? This test is the most important problem in developing multiprecision arithmetic. This activity, absorbs almoust the 60% of the totally realization effort. Apart the first immediate random tests, we can use many known formulas and algorithms. The general selecting criterions are: 1. Formulas should be iterative 2. Formulas should have many arithmetic elementary operations 3. Final results should be easyly verified 4. Intermediate results should be easily verified 5. Algorithms should be stable 6. Efficency is not important For example, a good arithmetic test is the Newton algoritm to compute the square root of a number. The iterative formula: n nn n n n x xx x x x ⋅ +⋅ =+=+ 2 21 2 1 converges to 21/2 , starting from x0 = 1. We have rearranged the formula in order to increase the number of operations (remember: the efficency is not important). In this way we can test mutliplication, division and addition. x0 = 1 x1 = 1.5 x2 = 1.41… x3 = 1.41421…… Here is a possible Excel arrangement. We have limited the number of the significant digits to 100 only for the picture dimensions, but there is no difficult to repeat it with the maximum digits. For each iterate only the blu digits are exacts. We see the progressive convergence. By the way, we note that this algorithm is also very efficent. The rate of convergence is quadratic. The number of digits approximately doubles at each iteration (In fact this is just the algorithm used by the xsqr multiprecision function) But, as said, for testing, the efficiency has no influence. It is important only that the algorithm involves the most multiprecision functions as possible.
  • 27. Xnumbers Tutorial 26 Another algorithm quite suitable for testing multiprecision accuracy is the π approximatation by continuous fraction1 . Initialize X = 31/2 , Y = 1/2, T = 6 Iteration X = (2 + X)1/2 Y = Y/X T = 2T P = Y*T*(5Y6 /112 + 3Y4 /40 + Y2 /6 + 1) Accuracy: approximately 12 decimal digits every 5 iterations) Below, step by step, a possible Excel arrangement: The Digit_Max parameter is in the A1 cell. By this parameter we can modulate the arithmetic accuracy. We have set 30 digits only for the picture dimensions. But you can try with 60, 100 or more. Note that, in order to have a more compact form, we have used the xeval function for calculating the X and P formulas that are inserted into the cells B3 and E3 rispectively. Selecting the last row (range A6:F6) and dragging it down, we get the following iteration table The convergence to π is evident. 1 This version, studied by David Sloan (2003), full of many arithmetic operations, permitted us to detect a very hidden bug in Xnumbers
  • 28. Xnumbers Tutorial 27 Sum xsum(v, [Digit_Max]) This is the extended version of the Excel built-in function SUM. It returns the sum of a vector of numbers. The argument is a standard range of cells. ni i vvvv ...21 ++=∑ Note that you can not use the standard function SUM, because it recognizes extended numbers as strings and it excludes them from the calculus. Product xprod(v, [Digit_Max]) Returns the product of a vector of numbers. nii vvvv ...21 ⋅⋅=∏ Note that the result is an extended number even if all the factors are in standard precision Raise to power xpow(x, n, [Digit_Max]) Returns the integer power od an extended number. xpow(x, n) = x n xpow("0.39155749636098981077147016011",90) = 1.9904508921478176508981155284E-7 xpow(5,81,60) = 5 81 = 413590306276513837435704346034981426782906055450439453125 xpow(122.5 ,1000) = 122.5 1000 = 1.36800819983562905361091390093E+2088 Note the exponent +2088 of the third result. Such kind of numbers can be managed only with extended precision functions because they are out side of the standard limits for 32bit double precision. For not integer power see the exponential functions xexp and xexpa
  • 29. Xnumbers Tutorial 28 Square Root xsqr(x, [Digit_Max]) Returnts the square root of an extended number xsqr(x) = x The example below shows how to compute the 2 with 30 and 60 significant digits: xsqr(2) = 1.41421356237309504880168872420969807 xsqr(2, 60) = 1.41421356237309504880168872420969807856967187537694807317667973799 Nth - Root xroot(x, n, [Digit_Max]) Returns the nth root of an extended number xroot(x, n)= n x The root's index must be a positive integer. The example below shows how to compute the 9 100 with 30 and 60 significant digits: xroot(100,9) = 1.66810053720005875359979114908 xroot(100,9,60) = 1.66810053720005875359979114908865584747919268415239470704499 Absolute xabs(x) Returns the absolute value of an extended number xabs(x)= |x| Do not use the built-in function "abs", as Excel converts x in double, then takes the absolute value but, at that time, the original precision of x is lost. Change sign xneg(x) Returns the opposite of an extended number: xneg(x) = −x Do not use the operator “−“ (minus) for extended numbers. Otherwise Excel converts the extended numbers into double and changes its sign but, at that time, the original precision is lost. In the following example the cell B8 contains an extended number with18 digits. If you use the “−“ as in the cell B9, you lose the last 3 digits. The function xneg(), as we can see in the cell B10, preserves the original precision.
  • 30. Xnumbers Tutorial 29 Integer part xint(x) Returns the integer part of an extended number, that is the greatest integer less than or equal to x. Examples: xint(2.99) = 2 xint(2.14) = 2 xint(-2.14) = −3 xint(-2.99) = −3 xint(12345675.00000001) = 12345675 xint(−12345675.00000001) = −12345676 Decimal part xdec(x) Returns the decimal part of an extended number Examples: xdec(2.99) = 0.99 xdec(-2.14) = - 0.14 Truncating xtrunc(x, [dec]) Returns the truncated number; the parameter "dec" sets the number of decimals to keep (default 0). It works like Excel function TRUNC. “dec” can be negative; in that case x is cut to the integer number, counting back from the decimal point. See the following examples. Examples: number dec number truncated 4074861.076055370173 -2 4074800 4074861.076055370173 -1 4074860 4074861.076055370173 0 4074861 4074861.076055370173 1 4074861 4074861.076055370173 2 4074861.07 4074861.076055370173 3 4074861.076 Rounding =xround(x, [dec]) Rounds an extended number, the parameter "dec" sets the decimal number of is to keep (default 0). It works like standard round function. “dec” can be negative, in that case x is rounded to the integer number, starting to count back from decimal point. See the following examples.
  • 31. Xnumbers Tutorial 30 number to round dec number rounded 6.2831853071795864769 0 6 6.2831853071795864769 1 6.3 6.2831853071795864769 2 6.28 6.2831853071795864769 3 6.283 6.2831853071795864769 4 6.2832 100352934.23345 0 100352934 100352934.23345 -1 100352930 100352934.23345 -2 100352900 When the number is in exponential format, it is internally converted into decimal before the rounding. number to round Decimal format Dec number rounded 1.238521E-17 0.00000000000000001238521 16 0 1.238521E-17 0.00000000000000001238521 17 1.E-17 1.238521E-17 0.00000000000000001238521 18 1.2E-17 1.238521E-17 0.00000000000000001238521 19 1.24E-17 Relative Rounding =xroundr(x, [dgt]) Returns the relative round of a number. The optional parameter Dec sets the significant digits to keep. (default = 15) This function always rounds the decimal place no matter what the exponent is number to round dgt number rounded 1.23423311238765E+44 15 1.23423311238765E+44 1.23423311238765E+44 14 1.2342331123876E+44 1.23423311238765E+44 13 1.234233112388E+44 1.23423311238765E+44 12 1.23423311239E+44 1.23423311238765E+44 11 1.2342331124E+44 1.23423311238765E+44 10 1.234233112E+44
  • 32. Xnumbers Tutorial 31 Extended Numbers manipulation Digits count xdgt(x) Returns the number of digits, significant or not, of an extended number. It is useful for counting the digits of long numbers xdgt(4.47213595499957939281834733746) = 30 xdgt(3.99999999999999999999999990000) = 30 xdgt(100000) = 6 xdgt(5856.51311933374) = 15 xdgt(1.2345678E-12) = 8 Significant Digits count xdgts(x) Returns the number of significant digits of a number( trailing zeros are not significant). xdgts("1240100000") = 5 xdgts(3.99999999999999999999999990000) = 26 xdgts(100000) = 1 xdgt(1.2345678E-12) = 8 Numbers comparison xcomp(a [b]) Compares two extended numbers. It returns the value y defined by:      <⇒− =⇒ >⇒ = ba ba ba y 1 0 1 The number b is optional (default b=0) If the second argument is omitted, the function returns the sign(a) xcomp(300, 299)= 1 xcomp(298, 299)= -1 xcomp(300, 300)= 0 if b is missing, the function assumes b = 0 for default and then it returns the sign(a) xcomp(3.58E-12)= 1 xcomp(0)= 0 xcomp(-0.0023)= -1
  • 33. Xnumbers Tutorial 32 Extended number check xIsXnumber(x) Returns TRUE if x is a true extended number. That is, x cannot be converted into double precision without losing of significant digits. It happens if a number has more than 15 significant digits. xIsXnuber(1641915798169656809371) = TRUE xIsXnuber(1200000000000000000000) = FALSE Format extended number =xFormat(str, [Digit_Sep]) =xUnformat(str) This function1 separates an extended number in groups of digits by the separator character of you local system ( e.g. a comma "," for USA, a dot "." for Italy). Parameter "str" is the string number to format, Digit_Sep sets the group of digits ( default is 3) The second function removes any separator character from the string Example (on Italian standard): x = 1230000012,00002345678 xFormat(x,3) = 1.230.000.012,000.023.456.79 xFormat(x,6) = 1230.000012,000023.45679 Example (on USA standard): xFormat(x,3)= 1,230,000,012.000,023,456,78 xFormat(x,6)= 1230,000012.000023,45678 Check digits DigitsAllDiff(number) This function2 return TRUE if a number has all digits different. DigitsAllDiff(12345) = TRUE DigitsAllDiff(123452) = FALSE Argument can be also a string. Example DigitsAllDiff(12345ABCDEFGHIM) = TRUE DigitsAllDiff(ABCDA) = FALSE 1 These functions were original developed by Ton Jeursen for his add-in XNUMBER95, the downgrade version of XNUMBERS for Excel 5. Because it works well and it is very useful for examining long string of number, I have imported it in this package. 2 This function appears by the courtesy of Richard Huxtable
  • 34. Xnumbers Tutorial 33 SortRange =SortRange (ArrayToSort, [IndexCol], [Order], [CaseSensitive]) This function returns an array sorted along a specified column ArrayToSort: is the (N x M ) array to sort IndexCol: is the index column for sorting (1 default) Order: can be "A" ascending (default) or "D" descending CaseSensitive: True (default) or False. It is useful for alphanumeric string sorting Example: The left table contains same points of a function f(x,y). The right table is ordered from low to high function values (the 3-th column) Digits sum sumDigits(number) This useful1 function returns the digits sum of an integer number (extended or not) sumDigits(1234569888674326778876543) = 137 Vector flip Flip(v) This function returns a vector in inverse order [a1, a2, a3, a4] ⇒ [a4, a3, a2, a1] 1 This function appears by the courtesy of Richard Huxtable
  • 35. Xnumbers Tutorial 34 Scientific format xcvexp(mantissa, [exponent]) This function converts a number into scientific format. Useful for extended numbers that, being string, Excel cannot format. xcvexp(-6.364758987642234, 934) = -6.364758987642234E+934 xcvexp(1.2334567890122786, ) = 1.2334567890122786E-807 This function is useful also to convert any xnumbers into scientific notation, simply setting exponent = 0 (default) xcvexp(12342330100876523, 0) = 1.2342330100876523E+16 xcvexp(0.000023494756398348) = 2.3494756398348E-5 Split scientific format xsplit(x) This function returns an array containing the mantissa and exponent of a scientific notation. If you press Enter this function returns only the mantissa. If you select two cells and give the CTRL+SHIFT+ENTER sequence, you get both mantissa and exponent Note that, in the last case, you cannot convert directly into double (for example, using the VALUE function), even if the number of digits is less than 15. The exponent is too large for the standard double precision. Converting multiprecision into double =xcdbl(x) This function converts an extended number into standard double precision It can be nested with other functions and/or array-functions. Usually the extended numbers are too long for a compact visualization. So, after, the multiprecision computation, we would like to convert the result in a compact standard precision. For example, if you invert the matrix 2 1 4 7 12 4 -4 0 8 using the multiprecision xMatInv function, you will get a matrix like the following 0.3076923076923076923076923 -0.02564102564102564102564103 -0.1410256410256410256410256 -0.2307692307692307692307692 0.1025641025641025641025641 0.06410256410256410256410256 0.1538461538461538461538462 -0.01282051282051282051282051 0.05448717948717948717948718
  • 36. Xnumbers Tutorial 35 If you use the functions xcdbl nested with the multiprecision function, the matrix will be rounded in standard precision and the output will have a more compact format Note that xcdbl affects only the output. The internal computing is always performed in multiprecision. Macros X-Edit These simple macros are very useful for manipulating extended numbers in the Excel worksheet. They perform the following operations: Format Separates groups of digits Unformat Removes the separator character Double Conversion Converts multiprecision numbers into standard double precision Round Rounding multiprecision numbers Relative Round Relative rounding multiprecision numbers Mop-Up Converts small numbers into 0 From this menu you can also change the default Digit_Max parameter Using these macros is very simple. Select the range where you want to operate and then start the relative macro. They work only over cells containing only numeric values, extended or standard. Cells containing function are ignored Tip. For stripping-out a formula from a cell and leaving its value, you can select the cell and then click in sequence (copy + paste values) Here are same little examples: Format - group 6 31415926.53589793238462643 31,415926.535897,932384,62643 19831415926.53589793238462 ⇒ 19831,415926.535897,932384,62 0.535897932384626433832734 0.535897,932384,626433,832734 Double Conversion 31415926.53589793238462643 31415926.5358979 19831415926.53589793238462 ⇒ 19831415926.5358 0.535897932384626433832734 0.535897932384626 Rounding 3 decimals. 31415926.53589793238462643 31415926.536 19831415926.53589793238462 ⇒ 19831415926.536 0.535897932384626433832734 0.536
  • 37. Xnumbers Tutorial 36 Relative rounding - significant digits 15. 4.5399929762484851535591E-5 4.53999297624849E-05 1.0015629762484851535591E-6 ⇒ 1.00156297624849E-06 0.539929762484851535591E-12 5.39929762484852E-13 Mop-Up - Error limit 1E-15. 31415926.53589793238462643 31415926.53589793238462643 -1.00E-15 ⇒ 0 5.78E-16 0 -1.40E-18 0 Note that the function mopup is used overall for improving the readability. The cells having values greater than the limit are not modified. Macro X-Converter This macro1 (*) converts a well formed Excel formula to the equivalent in terms of Xnumber multiprecision functions (xadd, xmult, etc.). The advantage over x-evaluate is that the code can be debugged in normal excel using small values and in a familiar form. When the spreadsheet works it is ' converted to using nested x-calls for the precision work. The intention is to tag the conversion onto the copy worksheet function ' so that you end up with a multi-precision copy of the original. Its main features are: • converts a range of cells directly on site or in a new worksheet. • skips cells having functions not convertible. • skips array functions except MINVERSE and MMULT that are substituted with the correspondent x-functions xMatInv and xMatMult. • Errors (if any ) are shown on the panel The digits parameter can be set in 4 different ways: 1) an integer number i.e. 30 2) a fixed reference of a cell, i.e. $A$1 3) a name of a cell, i.e. "digits" 4) simply nothing. In that case all the x-functions use the internal default = 30 Of course not all the Excel Functions can be converted. The list of the Excel functions2 converted is: * , / , + , - , ^ , ABS , ACOS , ACOSH , ASIN , ASINH , ATAN , ATANH , AVERAGE , COMBIN , COS , COSH , EXP , FACT , INT , LN , LOG , MDETERM , MINVERSE , MMULT , MOD , PI , PRODUCT , ROUND , SIN , SINH , SQRT , STDEV , STDEVP , SUM , TAN , TANH , TRUNC , VAR , VARP 1 The conversion engine of this macro was originally developed by John Jones in an smart m4 macro language which makes the VB native j-code more simple (!) to write. It appears in this package thanks to his courtesy 2 The functions are indicated with their original English names, that usually are different from the local names.
  • 38. Xnumbers Tutorial 37 Example. Assume to have an Excel formula in the cell B6 calculating the norm of a 3dim vector like the following worksheet. We would transform this formula into a multiprecision one using the x-functions of the Xnumbers add-in Select the cell B6 and start the macro X-Converter from the menu X-Edit Range to convert: the range containing the one or more formulas to convert Digit Parameter: specifies the global precision digits for all the functions under conversion. You can leave "(Default)" or blank meaning that all the function will use the internal default precision (usually 30 digits). But you can set a reference cell (example $A$1) where you have set your own precision. Or you can insert a name of a cell (example "mydigits") that you have defined. You can also insert directly a number (example 25) Output Option: sets the output where the multiprecision function will be copied. Select "on site" if we want to convert the formula directly in the selected cell. Select "new sheet" if we want to perform the conversion in a new worksheet. The original formula will be preserved. Let's select "on site" and click "run". The worksheet look will look like as the following As we can see the original standard formula =SQRT(B2+B3+B4) in cell B6 has been substituted with the multiprecision formula
  • 39. Xnumbers Tutorial 38 =xsqr(xadd(xadd(B2,B3),B4)). working with the default precision digits (30) Note that there are 3 nested functions in the converted formula. The maximum number of nested functions in Excel is 8. If an error raises during the cell conversion, the formula is left unchanged and the text is turned in red. The macro always shows a short list of the errors encountered during the conversion. The macro works also with a range selection Example. In the following worksheet we have same functions in the range A3:A18 Note that the cell A18 contains the function COUNTA that are not the similar x-function Note that the range A14:A15 contains an array function { ... }, thus the cells A14 and A15 are inseparable. The cells A7 contain a simple constant We want to convert them, where possible, in a new worksheet without affect the original worksheet. For that, select all the range A3:A18 and start the X-Converter Select "new sheet" and click "run". The new worksheet look like as the following Note that the cell A7, A14, A15, A18 are unchanged Note that the original cells A9 = B7^12 and A13 =A11^2.6 having the same operator symbol "^", are substituted with two different x-functions: xpow for integer power and xexpa for float power.
  • 40. Xnumbers Tutorial 39 Statistical Functions Factorial xfact(n, [Digit_Max]) Returns the factorial of an integer number xfact(n)= n! This example shows all 99 digits of 69! xfact(69, 100) = 711224524281413113724683388812728390922705448935203693936480- 40923257279754140647424000000000000000 If the parameter Digit_Max is less than 99, the function returns the approximate result in exponential format: xfact(69) = 1.71122452428141311372468338881E+98 For large number (n>> 1000) you can use the faster function xGamma(x). The relation between the factorial and the gama function is: Γ(n) = (n-1)! Factorial with double-step xfact2(n, [Digit_Max]) Returns the factorial with double step. if n is odd ⇒ xfact2(n) = 1⋅3⋅5⋅7⋅9...n if n is even ⇒ xfact2(n) = 2⋅4⋅6⋅8⋅ ...n Note: In many books, this function is indicate improperly as "double factorial", or - even worse - with the confusing symbol "!!". Combinations xComb(n, k, [Digit_Max]) Returns the binomial coefficients, a combination of n, class k. xcomb = C n,k The example below shows all the 29 digits of the combination of 100 objects grouped in class of 49 elements: xComb(100,49) = 98913082887808032681188722800 Combinations of N = 100 objects in class of 10, 20, … 90 Note the typical parabolic outline of the binomial coefficients For large argument (n and k >>1000) use the faster function xcomb_big(n,k) .
  • 41. Xnumbers Tutorial 40 Permutations xPerm(n, [k], [Digit_Max]) Returns the permutation of n, class k. xperm(n,k)= Pn,k. If k is omitted, the function assume k = n and in this case will be P(n) = n! Examples: xPerm(100, 20, 60) = 1303995018204712451095685346159820800000 xPerm(100) = 9.33262154439441526816992388562E+157 Arithmetic Mean xmean(x, [Digit_Max]) Returns the arithmetic mean of n numbers, extended or not. The argument is a range of cells. n x M i∑= Geometric Mean xgmean(x, [Digit_Max]) Returns the geometric mean of n numbers, extended or not. n nxxxx ...GM 221= Quadratic Mean xqmean(x, [Digit_Max]) Returns the quadratic mean of n numbers, extended or not. n x QM i∑= 2 Standard Deviation xstdev(range, [Digit_Max]) xstdevp(range, [Digit_Max]) Return the deviation of n numbers, extended or not. Range is a range of cells. The optional parameter Digit_Max, from 1 to 200, sets the number of significant digits (default 30) • xstdev returns the standard deviation 1 )( 2 − − = ∑ n xx σ
  • 42. Xnumbers Tutorial 41 • xstdevp returns the population standard deviation n xx p ∑ − = 2 )( σ Variance xvar(range, [Digit_Max]) xvarp(range, [Digit_Max]) Return the variance of n numbers, extended or not. Range is a range of cells. The optional parameter Digit_Max, from 1 to 200, sets the number of significant digits (default 30) • xvar returns the standard variance 1 )( 2 − − = ∑ n xx v • xvarp returns the population variance n xx vp ∑ − = 2 )( Probability distributions Xnumbers contains several type of probability distribution functions DSBeta(x, a, b, [dtype]) Beta distribution 0 < x < 1 , a > 0, b > 0, DSBinomial(k, n, p, [dtype]) Binomial distribution k integer, n integer , 0 < p < 1 DSCauchy(x, m, s, n, [dtype]) Cauchy (generalized) distribution n integer , s > 0 DSChi(x, r, [dtype]) Chi distribution r integer, x > 0 DSErlang(x, k, l, [dtype])) Erlang distribution k integer, x > 0 DSGamma(x, k, l, [dtype])) Gamma distribution x > 0, k > 0, l > 0 DSLevy(x, l, [dtype])) Levy distribution x > 0, l > 0 DSLogNormal(x, m, s, [dtype])) Log-normal distribution x > 0, m ≥ 0, s > 0 DSLogistic(x, m, s, [dtype])) Logistic distribution x > 0, m ≥ 0, s > 0 DSMaxwell(x, a, [dtype])) Maxwell-Boltzman distribution x > 0, a > 0 DSMises(x, k, [dtype])) Von Mises distribution k > 0, -π < x < π DSNormal(x, m, s, [dtype])) Normal distribution s > 0 DSPoisson(k, z, [dtype])) Poisson distribution k integer, z > 0 DSRayleigh(x, s, [dtype])) Rayleigh distribution x > 0, s > 0 DSRice(x, v, s, [dtype])) Rice distribution (j=1 cumulative) x > 0, v ≥ 0 , s > 0 DSStudent(t, v, [dtype])) Student distribution (j=1 cumulative) v integer degree of freedom DSWeibull(x, k, l, [dtype])) Weibull distribution (j=1 cumulative) x > 0, k integer, l > 0 The optional parameter dtype = 0 (default) returns the density distribution f(x); dtype = 1 returns the cumulative distribution F(x). ( ) ∫=<= x a dttfxPxF )()( x The lower limit "a" depends by the definition domain of the density function f(x).
  • 43. Xnumbers Tutorial 42 Probability density definition Rayleigh Gamma Cauchy Beta Lévy Logistic
  • 45. Xnumbers Tutorial 44 Log-normal Binomial knk pp knk n pnkf − − − = )1( )!(! ! ),,( Student 2/)1(2 2 1 2 1 2 1 )1)((2 ))1(( ),( + +Γ⋅ +Γ = v tvv v vtf π Poisson ! ),( v ex vxf xv − = Chi squared )(2 ),( 2 12/ 2/12/ v ex vxf v xv Γ = −−
  • 47. Xnumbers Tutorial 46 Univariate Statistic xstatis(range, [digit_max]) This function returns the univariate statistic summary of a range of data. The statistics computed are: Total of numerical elements of the range N Arithmetic Mean n x∑ Standard Deviation 1 )( 2 − −∑ n xx Popul. Standard Deviation n xx∑ − 2 )( Autocorrelation lag1 ∑ ∑ − −− − + n i n ii xx xxxx 1 2 1 1 1 )( ))(( This function returns a vector of 5 elements. Use the ctrl+shift+enter key sequence for insert it.
  • 48. Xnumbers Tutorial 47 Linear Regression Coefficients xRegLinCoef( Y, X, [DgtMax], [Intcpt]) Computes the multivariate linear regression with the least squares method in multi-precision arithmetic. Y is a vector (n x 1) of the dependent variable. X is a list of independent variable. It may be a vector for monovariable regression (n x 1), or a matrix for multivariate regression (n x m). DgtMax sets the precision (default 30). Intcpt = true/false. If true (default ) the algorithm calculates the intercept; otherwise the intercept is set to 0 For monovariate regression, this function returns two coefficients [a0, a1]; the first one is the intercept of Y axis, the second one is the slope. Simple Linear Regression Example. Evaluate the linear regression for the following xy data table x y 0.1 1991 0.2 1991.001046 0.35 1991.001831 0.4 1991.002092 0.45 1991.002354 0.6 1991.003138 0.7 1991.003661 0.8 1991.004184 0.9 1991.004707 1 1991.00523 1.5 1991.007845 1.8 1991.009414 2 1991.01046 3 1991.01569 Let's see other examples The model for this data set is y = a0 + a1 x Where [a0 , a1] are the unknown coefficents that can be evaluate by the xRegLinCoef function We can also compute the factor r2 in order to measure the goodness of the regression This can be done by the xRegLinStat function
  • 49. Xnumbers Tutorial 48 Example of univariate regression Example of multivariate regression Example of linear regression with intercept = 0 Note that the x-regression functions always returns a vector of m+1 values [a0, a1, ...am] independently if the intercept is 0 or not. Multivariate Regression This function can also compute a multivariate regression. This is when y depends by several variables x1, x2, … xn. Look at this example x1 x2 x3 y 0 0 -4 4000.8 0.1 0 -2 4000.7 0.2 0.5 -1 4001.55 0.3 0.5 0 4001.65 0.4 1 1.5 4002.4 0.5 1 2 4002.59 The model for this data set is y = a0 + a1 x1+ a2 x2+ a3 x3 Where [a0 , a1 , a2 , a3] are the unknown coefficients
  • 50. Xnumbers Tutorial 49 Linear Regression - Standard Deviation of Estimates xRegLinErr( Y, X, Coeff, [DgtMax], [Intcpt]) Returns the standard deviation of the linear regression of estimate Y is a vector (n x 1) of dependent variable values. X is a list of independent variable values. It may be a vector for monovariable regression (n x 1) or a matrix for multivariate regression (n x m). Coeff is the coefficients vector of the linear regression function [a0, a1, a2...am]. DgtMax sets the digits precision of computing. Intcpt = true/false. If true (default ) the algorithm calculates the intercept deviation; otherwise intercep a0 is set to 0 See above examples Polynomial Regression The same function finding the linear regression can easily be adapted to the polynomial regression. In the example below we will find the best fitting polynomial of 3rd degree for the given data x y 10 1120 11 1473 12 1894 13 2389 14 2964 15 3625 16 4378 17 5229 18 6184 19 7249 20 8430 The model for this data set is y = a0 + a1 x+ a2 x2 + a3 x3 where [a0 , a1 , a2 , a3] are the unknown coefficients First of all, we add at the given table two extra columns containing the power x2 , x3 They can easily be computed in an Excel worksheet as shown above. The polynomial coefficients can be computed by xRegLinCoef. The exact result is y = 10 + x + x2 + x3
  • 51. Xnumbers Tutorial 50 Linear Regression Formulas Generally, the multivariate linear regression function is: mm xaxaxaay ...22110 +++= where: [ ]maaaa ...,, 210 The coefficients of regression can be found by the following algorithm Make the following variables substitution: 1..mifor =−= xxX ii yyY −= where the right values are the averages of samples y and x respectively: ∑= k ky n y 1 ∑= k kii x n x , 1 After that, the coefficients a= [a1, a2, ....an] are the solution of the following linear system baC =⋅][ where [C] is the cross-covariance matrix, and b is the XY covariance               === == = = ∑ ∑∑ ∑∑∑ ∑∑∑∑ j jm j jmjj j j jmjj jjj j j jmjj jjj jjj j X XXX XXXXX XXXXXXX 2 , ,,3 2 ,3 ,,2,3,2 2 ,2 ,,1,3,1,2,1 2 ,1 .. .. .. C                 = ∑ ∑ ∑ ∑ j jmj j jj j jj j jj XY XY XY XY , ,3 ,2 ,1 .... b and the constant coefficient is given by: ∑ = −= m i ii XaYa 1 0 For m=1 we obtain the popular formulas of the monovariate linear regression ∑ ∑ = j j j jj X XY a 21 XaYa 10 −= This is the linear solution known as the Ordinary Least Squares (OLS). The analysis of this kind of approach shows that, for large dimensions of n (many measurement values) the matrix C becomes nearly singular
  • 52. Xnumbers Tutorial 51 Linear Regression Covariance Matrix xRegLinCov ( Y, X, Coeff, [DgtMax], [Intcpt] ) Returns the (m+1 x m+1) covariance matrix of a linear regression of m independent variables mm xaxaxaay +++= ...ˆ 22110 For a given set of n points ),( 21 imiiii yxxxP = Parameter Y is an (n x 1) vector of dependent variable. Parameter X is a matrix of independent variables. It may be an (n x 1) vector for monovariable regression or an (n x m) matrix for multivariate regression. Parameter Coeff is the (m+1) vector of the linear regression coefficents Cross Covariance Matrix Given the matrix X of the independent variables points Intercept calculated Intercept = 0             = mnn m m xx xx xx X ...1 ............ ...1 ...1 1 212 111             = mnn m m xx xx xx X ... ......... ... ... 2 212 111 The covariance matrix C is ( )12 − ⋅⋅= T XXsC Where: ( ) 1 ˆ 2 2 −− − = ∑ mn yy s i ii The covariance matrix C is ( )12 − ⋅⋅= T XXsC Where: mn yy s ii − − = ∑ 2 2 )ˆ( Note that the square roots of the diagonal elements of the covariance matrix iii cs = are the standard deviations of the linear regression coefficients
  • 53. Xnumbers Tutorial 52 Linear Regression Statistics xRegLinStat( Y, X, Coeff, [DgtMax], [Intcpt]) Returns some statistics about the linear regression R2 Square of the linear correlation factor S y,x Standard deviation of the linear regression Parameter Y is a vector (n x 1) of dependent variable. Parameter X is a list of independent variable. It may be an (n x 1) vector for monovariable regression or a (n x m) matrix for multivariate regression. Coeff is the coefficients vector of the linear regression function [a0, a1, a2...am]. Formulas The regression factor (better: the square of regression factor) R2 lie between 0 and 1 and roughly indicates how closely the regression function fits the given values Y. Generally, it can be computed by the following formula: 2 2 * 2 2* 2 1 )( )( 1 y yy i i i ii yy yy R σ σ − −= − − −= ∑ ∑ Where * y is the value estimated by the regression function and y is the mean of y values. mm xaxaxaay ...* 22110 +++= ∑= k ky n y 1 For monovariate regression (m=1), the above formula returns the popular formula: ( ) ( ) ∑ ∑ ∑ ∑ − − = n y y n x x R 2 2 2 2 2 Standard error of the linear regression is: Intercept calculated Intercept constrained to 0 ( ) 1 2* , −− − = ∑ gln yy s i ii xy ( ) gln yy s i ii xy − − = ∑ 2* , Where gl = number of independent variables
  • 54. Xnumbers Tutorial 53 Linear Regression Evaluation = xRegLinEval(Coeff, X) Evaluates the multivariate linear regression in multi precision arithmetic. Parameter Coeff is the coefficients vector [a0. a1, a2, ....] of the linear regression Parameter X is the vector of independent variables. It is one value for a simple regression The functions return the linear combination. nn xaxaxaay ...22110 +++= Example: Plot the linear regression for the following data set x y -1 0.58 -0.8 0.65 -0.6 0.88 -0.4 1.25 -0.2 1.32 0 1.14 0.2 1.31 0.4 1.51 0.6 1.54 0.8 1.48 1 1.98 In this worksheet, each value of linear regression * y is computed by the function xRegLinEval. The regression coefficients are computed by xRegLinCoef. The results are converted in double by the function xcdbl. Selecting the range A1:C12 and plotting the data we get the following regression graphs 0 0.4 0.8 1.2 1.6 2 2.4 -1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1 y y*
  • 55. Xnumbers Tutorial 54 Polynomial Regression - Coefficient xRegPolyCoef( Y, X, Coeff, [Degree], [DgtMax], [Intcpt]) Computes the polynomial regression with the least squares method in multi precision arithmetic. m m xaxaxaay +++= ...2 210 with m > 1 Y is a vector (n x 1) of the dependent variable. X is a vector (n x 1) of the independent variable Degree is the degree m >1 of the polynomial DgtMax sets the precision (default 30). Intcpt = true/false. If true (default) the algorithm calculates the intercept; otherwise the intercept is set to 0 The function returns the coefficient vector [a0, a1, a2...am] Example. Find the 3rd degree polinomial fitting the given data set (xi, yi) Polynomial Regression - Standard Deviation of Estimates xRegPolyErr ( Y, X, Coeff, [Degree], [DgtMax], [Intcpt]) Returns the standard deviation of the polynomial regression estimate Y is a vector (n x 1) of dependent variable values. X is a vector (n x 1) of the independent variable Coeff is the coefficients vector [a0, a1, a2...am]. of the polynomial regression DgtMax sets the precision (default 30).. Intcpt = true/false. If true (default) the algorithm calculates the intercept; otherwise the intercept is set to 0 see above example Polynomial Regression Statistics xRegPolyStat ( Y, X, Coeff, [Degree], [DgtMax], [Intcpt]) Computes the R-squared factor of the polynomial regression and the standard deviation of the regression.
  • 56. Xnumbers Tutorial 55 2 R R-Squared coefficient of the polynomial regression xyS , Standard Deviation of the polynomial regression Y is a vector (n x 1) of dependent variable values. X is a vector (n x 1) of the independent variable Coeff is the coefficients vector [a0, a1, a2...am]. of the polynomial regression DgtMax sets the precision (default 30). Intcpt = true/false. If true (default ) the algorithm calculates the intercept deviation; otherwise this is set to 0 If you wanto to see the standard deviation select two cells and give the CTRL+SHIFT+ENTER sequence. Macro - Regression Xnumbers contains two macros performing the linear and polynomial regression in multiprecision arithmetic. From the Xnumbers menu, select • Macro Regression Linear • Macro Regression Polynomial Data XY is an array containing the column of the variable X at the left with the adjacent column of Y at the right. For a m-multivariate regression also the columns of X must be exactly m. Select this range before starting the macro. In this case the input box will be automatically filled Data XY for univariate and polynomial regression Data XY for multivariate regression Output cell indicates the starting upper left cell of the output area. Intercept. If checked the macro calculates the intercept otherwise it is set to 0 Convert to double. Converts the multiprecision in double before the output
  • 57. Xnumbers Tutorial 56 R-squared + residual std. dev. Calculate the correspondent statistics Estimate std. dev. Calculate the standard deviation of estimate Digits Max. from 1 to 200, sets the precision (default 30) Degree. The panel of the Polynomial Regression macro is similar to the Linear except for the input degree box that allows to chose the polynomial degree. An example of the regression output is. To obtain this result follow this steps:. • Select the range A2:C7 and start the macro. • Select all the option boxes and deselect the "double conversion" in order to see all the 30 digits. Note. The text has been added by hand only for clarity. The macro do not write them. We do it. The coefficient a0 (intercept) will be always output. If the intercept is switched off, the result in the cell F2 will be 0
  • 58. Xnumbers Tutorial 57 Sub-Tabulation One important application of linear regression is the sub-tabulation, which is the method to extract a table of values with smaller step from an original table with bigger steps. In other words, we can obtain a fine tabulation from a table with a few values of a function. Let’s see this example. Example: Extract from the following dataset, a table having 11 values with step 0.1, from 0 to 1 x y 0 5.1 0.2 4.7 0.5 4.5 0.6 4.3 0.7 4.2 1 3.6 3.00 3.50 4.00 4.50 5.00 5.50 0 0.2 0.4 0.6 0.8 1 The graph shows the extra points added by the sub tabulation. Note that this method is different from the interpolation because the regression line does not pass through any of the original points. The new values of the table B are different from the ones table A even in the same x-values. This feature came in handy when we want to regularize the row data. Data Conditioning The conditioning of the data consists of subtracting the mean from the values of the sample. It can improve the accuracy of the linear regression, but the regression coefficients obtained - conditioned coefficients - are different from the regression coefficients of the row data. They can be back-transformed by the following method: Given X and Y two data vectors, the linear regression polynomial of n degree well be: First of all, we find the linear regression coefficients [ a0 , a1 ] Than we re-calculate the values y i = a0 + a1 h , i = 1…10, h = 0.1
  • 59. Xnumbers Tutorial 58 ∑= ⋅= n i i i xaxp 0 )( We made the data conditioning, making the average of X and Y 11 ∑∑ == ii y n yx n x Substituting the old variables with the new variable u and v yyvxxu iiii −=−= Than, the new linear regression polynomial well be: ∑= ⋅= n i i i ubup 0 )( The original ai coefficients can be obtained from the new bi coefficients by the following formulas. i i n i i xbya ⋅⋅−+= ∑=0 0 )1( ki i n ki ki k xb k i a − = + ⋅⋅      −= ∑ )1( This method is often useful for increasing the gloabal accuracy of the linear regression
  • 60. Xnumbers Tutorial 59 Linear Regression with Robust Method RegLinRM(x, y, [Method]) This function1 performs the linear regression with three different robust methods: - SM: simple median - RM: repeated median - LMS: least median squared Robust methods are suitable for data containing wrong points. When data samples have noise (experimental data), the basic problem is that classic LMS (least minimum squared) is highly affected by noisy points. The main goal of robust methods is to minimize as much as possible the influence of the wrong points when fitting the function The parameter x and y are two vectors of the points to fit. The optional parameter "Method" sets the method you want to use (default = SM) The functions returns an array of two coefficients [a1, a0] where 01 axay +⋅≅ Use CTRL+SHIFT+ENTER to paste it. Example: Suppose you have sampled 5 experimental values (xi, yi), with a (suspected) large error in the last value 6.5. x y 1 1.1 2 2 3 3.1 4 3.8 5 6.5 In the graph are shown the regression lines obtained with all robust methods in comparison with the standard OLS regression. As we can see all the lines SM, RM, LMS (Robust Methods) minimize the influence of the value (5, 6.5) 1 The routines for robust linear regression were developed by Alfredo Álvarez Valdivia. They appear in this collection thanks to its courtesy Linear Regression 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 Ordenadas SM RM LMS MM.CC.Standard
  • 61. Xnumbers Tutorial 60 Linear Regression Min-Max RegLinMM(x, y) This function performs the linear regression with the Min-Max criterion (also called Chebychev approximation) of a discrete dataset (x, y) The parameter "x" is a (n x 1) vector of the independent variable, The parameter "y" is a (n x 1) vector of the dependent variable The function returns the coefficients [a0, a1] of the max-min linear regression xaay 10 ~ += As known, those coefficients minimize the max absolute error for the given dataset |)(~|max ii yxyE −= Example. Find the better fitting line that minimize the absolute error The liner regression is y ≅ 0.428 + 1.142 x with an error max Emax ≅ ± 0.7 The scatter plot shows the lineare regression approximation As we can see, all the points lie in the plane strips of ± Emax around the min-max line (pink line). (Emax ≅ 0.7)
  • 62. Xnumbers Tutorial 61 NIST Certification Test The multiprecision regression functions of Xnumbers have been tested against the NIST linear regression data sets. The table below gives the LRE results from xRegLinCoef and xRegPolyCoef functions NIST test for linear regression (row data sets) StRD Datasets Level of difficulty Model of class Coeff. Function LRE Norris low Linear 2 xRegLinCoef 15.0 Pontius low Quadratic 3 xRegPolyCoef 15.0 NoInt1 Average Linear 1 xRegLinCoef 15.0 Filip high Polynomial 11 xRegPolyCoef 15.0 Longley high Multilinear 7 xRegLinCoef 15.0 Wampler1 high Polynomial 6 xRegPolyCoef 15.0 Wampler2 high Polynomial 6 xRegPolyCoef 15.0 Wampler3 high Polynomial 6 xRegPolyCoef 15.0 Wampler4 high Polynomial 6 xRegPolyCoef 15.0 Wampler5 high Polynomial 6 xRegPolyCoef 15.0 The table below gives the LRE results on the NIST test univariate data sets obtained from xStats function NIST test for univariate data sets Dataset Category Difficulty Size Mean Stand. Dev. Autocor. Coef. PiDigits Univariate 1 5000 15 15 15 Lottery Univariate 1 218 15 15 15 Lew Univariate 1 200 15 15 15 Maveo Univariate 1 50 15 15 15 Michelso Univariate 1 100 15 15 15 NumAcc1 Univariate 1 3 15 15 15 NumAcc2 Univariate 2 1001 15 15 15 NumAcc3 Univariate 2 1001 15 15 15 NumAcc4 Univariate 3 1001 15 15 15
  • 63. Xnumbers Tutorial 62 Transcendental Functions Logarithm natural (Napier’s) xLn(x, [Digit_Max]) Returns the natural logarithm (or Napier’s) , in base "e" The argument may be either normal or extended number. Example: xLn(30) = 3.4011973816621553754132366916 Logarithm for any base xLog(x, [base], [Digit_Max]) Returns the logarithm for any base (default 10) )(log xy base= The argument may be either normal or extended number. Example xlog(30) = 1.47712125471966243729502790325 Exponential xexp(x, [Digit_Max]) Returns the exponential of x in base "e" xexp(x) = e x Example e10 = xexp(10) = 22026.4657948067165169579006452 e1000 = xexp(1000) = 1.97007111401704699388887935224E+434 Note the exponent 434 of the second result. Such kind of numbers can be managed only with extended precision functions because they are outside the standard limits of double precision. Exponential for any base xexpa(x, [a], [Digit_Max]) Returns the exponential of x in any in base xexpa(x, a) = a x The arguments “a” and “x” may be either normal or extended numbers, with a > 0. If the base "a" is omitted the function assumes a = 10. 21.234 = xexpa(1.234, 2) = 2.3521825005819296401155858555 10−0.54 = xexpa(-0.54) = 0.288403150312660594239196924659
  • 64. Xnumbers Tutorial 63 Constant e xe([Digit_Max]) Returns Euler's constant "e", the base of the natural logarithm. The optional parameter Digit_Max, from 1 to 415, sets the number of significant digits (default 30). xe() = 2.71828182845904523536028747135 xe(60) = 2.71828182845904523536028747135266249775724709369995957496696 Constant Ln(2) xLn2([Digit_Max]) Returns the constant Ln(2). The optional parameter Digit_Max, from 1 to 415, sets the number of significant digits (default 30). Constant Ln(10) xLn10([Digit_Max]) Returns the constant Ln(10). The optional parameter Digit_Max, from 1 to 415, sets the number of significant digits (default 30). Hyperbolic Sine xsinh(x, [Digit_Max]) Returns the hyperbolic sine of x in multiprecision arithmetic 2 sinh xx ee − − = Hyperbolic ArSine xasinh(x, [Digit_Max]) Returns the hyperbolic arsine of x in multiprecision arithmetic ( )1ln)(asinh 2 ++= xxx
  • 65. Xnumbers Tutorial 64 Hyperbolic Cosine xcosh(x, [Digit_Max]) Returns the hyperbolic cosine of x in multiprecision arithmetic 2 )cosh( xx ee x − + = Hyperbolic ArCosine xacosh(x, [Digit_Max]) Returns the hyperbolic Arcosine of x in multiprecision arithmetic The argument x, normal or extended, must be x >1 ( ) 1,1lnacosh 2 >−+= xxx Hyperbolic Tangent xtanh(x, [Digit_Max]) Returns the hyperbolic tangent of x in multiprecision arithmetic xx xx ee ee x − − + − =)tanh( Hyperbolic ArTangent xatanh(x, [Digit_Max]) Returns the hyperbolic artangent of x in multiprecision arithmetic The argument x, normal or extended, must be |x| < 1 1, 1 1 ln 2 1 )(atanh <      − + = x x x x Euler constant γ =xeu( [Digits_Max] ) Returns the Euler-Mascheroni constant γ (The same constan returned by xGm function) Example xeu() = 0.57721566490153286060651209008 xeu(60) = 0.57721566490153286060651209008240243104215933593992359880576
  • 66. Xnumbers Tutorial 65 Trigonometric Functions Sin xsin(a, [Digit_Max]) Returns the sine of the angle a xsin(a) = sin(a) The argument a, in radians, may be either a normal or an extended number. xsin(1.5) = 0.997494986604054430941723371141 Cos xcos(a, [Digit_Max]) Returns the cosine of the angle a xcos(a) = cos(a) The argument a, in radians, may be either a normal or an extended number. xcos(1.5) = 7.07372016677029100881898514342E-2 Computation of cos(π/2) Example: compute cos (89,99999995°) with the standard built-in function COS function COS(89.99999995) = COS(1.570796326) = 7.94896654250123E-10 The correct answer, accurate to 15 digits, is 7.94896619231321E-10 As we can see, only 7 digits are corrected. The remaining 8 digits are meaningless. On the contrary, with the multiprecision function xcos(x) we have the correct result with all its significant digits. xcos(1.570796326) = 7.94896619231321E-10 The table below shows the computation effect when a approaches π /2 angle α α (deg) xcos(α) COS(α) built-in Err % 1.57 89.95437383553930 7.96326710733325E-4 7.96326710733263E-04 7.75E-14 1.570 89.95437383553930 7.96326710733325E-4 7.96326710733263E-04 7.75E-14 1.5707 89.99448088119850 9.63267947476522E-5 9.63267947476672E-05 -1.55E-13 1.57079 89.99963750135470 6.32679489657702E-6 6.32679489666849E-06 -1.45E-11 1.570796 89.99998127603180 3.26794896619225E-7 3.26794896538163E-07 2.48E-10 1.5707963 89.99999846476560 2.67948966192313E-8 2.67948965850537E-08 1.28E-09 1.57079632 89.99999961068120 6.79489661923132E-9 6.79489670660314E-09 -1.29E-08 1.570796326 89.99999995445590 7.94896619231321E-10 7.94896654250123E-10 -4.41E-08 1.5707963267 89.99999999456290 9.48966192313216E-11 9.48965963318629E-11 2.41E-07 1.57079632679 89.99999999971950 4.89661923132169E-12 4.89658888522954E-12 6.20E-06 As we can see, the accuracy of the standard function COS decreases when the angle approaches the right angle. On the contrary, the xcos function keeps its accuracy.
  • 67. Xnumbers Tutorial 66 Tan xtan(a, [Digit_Max]) Returns the tangent of a xtan(a) = tan(a) The argument a, in radians, may be either a normal or an extended number. Arcsine xasin(a, [Digit_Max]) Returns the arcsine of a xasin(a) = arcsin(a) The arcsine is defined between -π/2 and π/2 The argument a, where 1|| ≤a , may be either a normal or an extended number. Arccosine xacos(a, [Digit_Max]) Returns the arccosine of a xacos(a) = arccos(a) The arccosine is defined between 0 and π The argument a, where 1|| ≤a , may be either a normal or an extended number. Arctan xatan(a, [Digit_Max]) Returns the arctan of a xatan(a) = arctan(a) The arctan(a) is defined between 2/arctan(a)2/ ππ <<− Constant π These functions return the following multiples of π xpi([Digit_Max]) xpi = π xpi2([Digit_Max]) xpi2 = π/2 xpi4([Digit_Max]) xpi4 = π/4 x2pi([Digit_Max]) x2pi = 2π The optional parameter Digit_Max, from 1 to 415, sets the number of significant digits (default 30). Example. Compute the Hermite-Ramanujan constant with 36 significant digits 163π e xexp(xmult(xpi(36),xsqr(163,36),36),36) = 262537412640768743.999999999999250005
  • 68. Xnumbers Tutorial 67 Complement of right angle xanglec(a, [Digit_Max]) Returns the complement of angle a to the right angle xanglec(α) = π/2−α where 0 ≤ α ≤ π/2 . Example: xanglec(1.4) = 0.17079632679489661923132169163 For angles not too near the right angle this function is like the ordinary subtraction. The use of this function is computing the difference without loss of significant digits when the angle is very close to the right angle. For example, computing in Excel the following difference: (PI()/2 − 1.570796) = 1.57079632679490 − 1.570796 = 0.00000032679490 we have a loss of 7 significant digits, even though the computation has been made with 15 significant digits. On the contrary, if we use: xanglec(1.570796 , 15) = 3.26794896619231E-7 we get the full precision with 15 significant digits. The "lost" digits are automatically replaced
  • 69. Xnumbers Tutorial 68 Polynomial Rootfinder The roots of polynomials are of interest to more than just mathematicians. They play a central role in applied sciences including mechanical and electrical engineering where they are used in solving a variety of design problems. Xnumbes provides several macros based on the following polynomial rootfinder algorithms. RootFinder JT Jenkins and Traub algorithm (translated in VB from the original rpoly FORTRAN 77 subroutine) RootFinder GN Generalized Newton-Raphson method RootFinder ADK Aberth, Durand, Kerner algorithm Rootfinder RF Ruffini's method for real integer roots. Rootfinder LB Lin-Bairstow algorithm Rootfinder SK Siljak algorithm Rootfinder LA Laguerre algorithm These macros are able to find, in a few seconds, all the roots - real or complex - of a dense polynomial up to 15th - 20th degree, in double or multi-precision. It is remarkable that sometimes the results has shown in an exact way, even if the computation is intrinsically approximate. The characteristics of each rootfinder are reassumed in the following table Macro Roots Coefficients Arithmetic RootfinderJT Complex Real Standard RootfinderGN Complex Real Multi-precision RootfinderDK Complex Complex Multi-precision RootfinderRF Real, Integer Real, Integer Multi-precision RootfinderLB Complex Real Standard RootfinderSK Complex Complex Multi-precision RootfinderLA Complex Real Standard
  • 70. Xnumbers Tutorial 69 Input parameters The polynomial rootfinder interface is simple and straight. Method: JT Jenkins-Traub Standard Real coefficients GN Gen. Newton-Raphson Multiprecision Real coefficients ADK Aberth-Durand-Kerner Multiprecision Complex coefficients RF Ruffini Standard Real Integer coefficients LB Lin-Bairstow Standard Real coefficients SK Siljak Multiprecision Complex coefficients LA Laguerre Standard Real coefficients Coefficients input: is an array containing the polynomial coefficients with increasing degree, from top to bottom. May be also a single cell containing the polynomial formula string, such as: -120+274x-225x^2+85x^3-15x^4+x^5 RootfinderDK and Siljak can also accept complex coefficients. In that case the input is an (n x 2) array. Examples of possible input are (thick black box): Remarks. The formula string is more adapt for sparse polynomials. Real coefficients can be put in horizontal or vertical vector. Complex coefficients, only in vertical vectors Results Output: It is the upper left corner of the output area. If blank, the routine assumes the cell nearest the given coefficients range. Error: Sets the relative roots accuracy. The algorithm terminates when the relative difference between two iterations is less then this value. Iter: The algorithm stops when the iterations counter reaches this value. Multi-Precision: Enable/disable the multi-precision arithmetic MP-out: If checked, the results are written in multi-precision, otherwise they are converted in standard double precision.
  • 71. Xnumbers Tutorial 70 Output The rootfinder macros output their results in the following simplified layout The roots and their estimated relative errors are written in a table starting from the left upper cell indicated in the input window. In the right-bottom cell is written the total elaboration time in seconds Note: we have formatted the table only for clarity. The macros do not perform this task. We do it. Integer Rootfinder output The macro RootfinderRF outputs all integer roots of the polynomial (if any) at the left and the coefficients of the remainder polynomial (deflated polynomial) at the right RootfinderRF Polynomial Integer roots Reduced Polynomial This result means that the given polynomial 8704119046280328151058214720 2345678 −+−−+−+− xxxxxxxx can be factorized as )3438234()8)(2)(2( 2342 +−+−+−+ xxxxxxx
  • 72. Xnumbers Tutorial 71 How to use rootfinder macros Using polynomial rootfinder macros is simple. Select the polynomial coefficients and start the rootfinder that you prefer. All input fields are filled and the only work that you have to do – in the most cases - is to press "Run". Example. Select the range C3:C11 and start the RootfinderJT . The coefficients input-box is filled with C3:C11 and the result-output box is filled with the cell E3. Press “run” and - after a while - the routine ends and the roots will be displayed at the right, like in the following fugure Sparse polynomials. We can pass to the rootfinder macros also symbolic polynomial string, (that it is the faster way for sparse high degree polynomials). Let’s see this example Find all roots of the following 16th degree polynomial x^16-6817x^8+1679616 Write this string in a cell, select it and start a rootfinder macro
  • 73. Xnumbers Tutorial 72 In this case we have used the Durand-Kerner algorithm obtaining a very high accuracy (practically the highest accuracy in standard double precision) Root Error Estimation The third column produced by the rootfinder macros is an estimation of the relative root error, defined as: ||/|~| iii xxxer −= for 0|| >ix where x~ is the true unknown root and ix is the approximate root given by the rootfinder We have to say that this number should be regarded as an estimation of “goodness” of the root found; small values (for example 1E-9 , 1E-12 ) indicate an high precision of the correspondent root. On the contrary, larger values (for examples 1E-3 , 1E-5) indicates a “difficult” roots that require an extra investigation. For example assume to find the root of the following 6th degree polynomial Clustering effect: In this case, the accuracy is enough good, but quite lower than the previous example. The reason is that the roots: – 1, 1.01, 1.02, 1.03, 1.04, 1.05 are very close each other (0.1% of difference)
  • 74. Xnumbers Tutorial 73 Complex polynomials. The macro RootfinderDK and RootfinderSK can solve also complex polynomials. Example: find the roots of the following polynomial with complex coefficients 5432 )3(5)515(4)412()( zzizzizizP ++−+−−+++−= Select both real and imaginary coefficients columns and start the macro RootfinderKD The roots are z = ±1 , z = ±2 , z = 3 − j . Observe that the results are shown in exact mode even if the computation is intrinsically approximated. A polynomial of n degree, having as roots the first 1, 2,..n integers, belongs to the Wilkinson class that, as known, is ill-conditioned. This dense polynomial is usually assumed as standard reference for polynomial rootfinder algorithms. We have tabulated the LRE (log relative error) obtained with all the rootfinder macros. As we can see, for a Wilkinson polynomial of 20th degree, we have good about four significant digits (0.1% accuracy) Wilkinson polynomial test 0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 0 5 10 15 20 25 LRE But all polynomials are so hard to solve? Fortunately not. Many polynomials with higher degree, can be solved with good accuracy For example, if we try to get all real roots of the 16th degree Legendre’s polynomial L16(x) = 6435 -875160 x2 +19399380 x4 -162954792 x6 + 669278610 x8 -1487285800 x10 +1825305300 x12 -1163381400 x14 +300540195 x16 We have a general accuracy of more than 13 digits Legendre polyn. Coeff. Real Imm Rel. Err. 6435 -0.989400934991646 0 2.9585E-17 0 -0.944575023073157 0 1.6352E-13 -875160 -0.865631202387904 0 3.673E-14 0 -0.755404408355024 0 1.1559E-14 19399380 -0.617876244402639 0 1.0779E-14 0 -0.458016777657228 0 7.4625E-16 -162954792 -0.281603550779259 0 1.9313E-16 0 -0.095012509837637 0 8.5038E-18 669278610 0.095012509837637 0 8.5038E-18 0 0.281603550779259 0 1.4485E-16 -1487285800 0.458016777657228 0 1.5356E-15 0 0.617876244402640 0 5.0196E-15 1825305300 0.755404408354981 0 2.5615E-14 0 0.865631202387767 0 4.6793E-14 -1163381400 0.944575023073325 0 8.4139E-14 0 0.989400934991655 0 5.5583E-15 300540195
  • 75. Xnumbers Tutorial 74 (remember that the higher degree coefficients are at bottom) In the last column there are the errors estimation given by the rootfinder DK. They are slight different from the true roots errors, but we have to remember that this column must be regard as an index of the root approximation: low values mean a good accuracy, larger values often (but not always) indicate a poor approximation Integer roots In applied science it's rarely to came across in polynomials having exact integer roots. Nevertheless, they are frequent in math, didactical examples and algorithm testing . Xnumbers has a dedicated special macro for finding the integer real roots of a polynomial. It uses the Ruffini's method with the QD algorithm for roots isolation. This method is generally less efficient then JT or DK but it can gain in accuracy. The roots found by this method have no round-off errors so the deflated polynomial is exact. Therefore, in that case, the root-finding-deflating process is without errors. For polynomial having a mix of integer real roots, complex roots and real roots the method returns the integer roots and the coefficients of the deflated polynomial that can be solved with the aid of the general purpose macros: DK, GN or JT. Because the deflated polynomial has a lower degree, the roots accuracy will be generally higher than by solving directly the given polynomials. Let's see how it works practically. Assume to have the following polynomial degree coeff a0 8678880 a1 -13381116 a2 8844928 a3 -3279447 a4 746825 a5 -107049 a6 9437 a7 -468 a8 10 The exact roots are: integer real complex 5, 6, 7, 8, 9 2.8 5.0i5.4 ± If we try to solve this 8th degree polynomial with a general rootfinder, probably the best accuracy that we can obtain is about 1e-10, that it is a good result but we can do better if we extract the integer roots before and then, solving for the remaining roots Select the range B2:B10 and start the macro for extracting the integer roots and deflated polynomial find real and complex roots (if exist) Extract integer roots (if exist) original P(x) Deflated Q(x)
  • 76. Xnumbers Tutorial 75 The original polynomial is now cracked into the following factors )57445711810)(8)(7)(6)(5( 23 −+−−−−− xxxxxxx Now let's find the roots of the following 3rd degree polynomial by, for example, the general JT rootfinder. We obtain: Re Im Rel. Err. 2.8 0 1.14E-17 4.5 0.5 1.44E-15 4.5 -0.5 1.44E-15 The general accuracy is better than 1e-14 , thousand times than the direct method. Clearly is a good thing to keep attention to the integer roots (when they are). 1.0E-16 1.0E-14 1.0E-12 1.0E-10 1.0E-08 1.0E-06 1.0E-04 1.0E-02 1.0E+00 Rootfinder RF + DK Rootfinder JT Rootfinder DK Rootfinder NG Global roots accuracy versus the solving methods: Rootfinder RF + DK Rootfinder JT Rootfinder DK Rootfinder NG The multiprecision should be used when the coefficients exceed 15 digits (remember that the coefficients must be exact in order to extract the exact integer roots) Let's see the following 18th degree polynomial having the roots Coefficients -612914519230813800000 91181999821816015800 -5186948337826516202 137665995531841931 -1622627967498318 6214402509219 -11208193158 10605849 -5122 1 Polynomial roots Note that same coefficients have 16 - 18 significant digits and they must be inserted as x-numbers, (that is as string) in order to preserve the original precision. We have also to set the multiprecision check-box in the macro RootfinderRF integer real complex 25, 27,29, 31, 1000, 1001, 1002, 1003, 1004 none none Note that this a so called "clustered polynomial" because some of its integer roots (1000, 1001, 1002, 1003, 1004) are very closed each other (difference less then 1%). This situation is quite difficult for many algorithms and the accuracy is generally quite poor. On the contrary, the Ruffini's method works very fine in that case.
  • 77. Xnumbers Tutorial 76 Multiple roots The macro "Factors" performs the decomposition of a polynomial with multiple roots into smaller factors having all single roots. It uses the GCD method with the Euclid' algorithm Example, the polynomial: 98765432 2017587827795744773765343132648 xxxxxxxxx +−+−+−+−+− Has the roots x = 1 (m = 2), x = 2 (m = 3) and x = 3 (m = 4) It can be decomposed into the product of the following factors 2322 )6116)(56)(3( xxxxxx +−+−+−+− Using this macros is simple. Select the polynomial coefficients and start the macro. All the input and output cells are automatically filled. Press Run Each factors contains only single roots and thus can be solved with high precision by any rootfinder macro1 . 1 For further details see "Non Linear Equations", Foxes Team, 2006
  • 78. Xnumbers Tutorial 77 Polynomial Functions Polynomial evaluation =POLYN(z, Coefficients, [DgtMax]) Computes the polynomial at the value z. n n zazazaazP ...)( 2 210 +++= The parameter Coefficients is the (n+1) column vector containing the polynomial coefficients from the lowest to the highest order. This function accept also complex coefficients. In that case the parameter Coefficients is an (n+1 x 2) array. The optional parameter DgtMax set the precision. If omitted, the function works in the faster double precision. This function works also for complex arguments. In that case, z must be a complex number (two adjacent cells) and the function returns two values. To see both real and imaginary part, select two cells and give the CTRL+SHIFT+ENTER key sequence. If you press only ENTER, the function returns only its real part. Example: compute the following real polynomial 4252)( 234 ++−+= zzzzzP for iz 24−= Otherwise, if you want to compute a real polynomial for a real argument, e.g. z = 10 - simply pass a single value
  • 79. Xnumbers Tutorial 78 Example: compute the following complex polynomial )54()2(5)1(2)( 234 izizzizzP −+−+−−+= for iz 24−= Polynomial derivatives =DPOLYN(z, Coefficients, Order, [DgtMax]) Computes the polynomial derivative at the value z. n n zazazaazP ...)( 2 210 +++= j j j dz zPd zD )( )( = The parameter "Coefficients" is the (n+1) vector containing the polynomial coefficients from the lowest to the highest order. This function accept also complex coefficients. In that case the parameter Coefficients is an (n+1 x 2) array. The parameter "Order" sets the order of the derivative. The optional parameter "DgtMax" set the precision. If omitted, the function works in the faster double precision. This function works also for complex arguments. In that case, z must be a complex number (two adjacent cells) and the function returns two values. To see both real and imaginary part, select two cells and give the CTRL+SHIFT+ENTER key sequence. If you press only ENTER, the function returns only its real part. Example. Compute the derivatives of the following polynomial 32 23)( zzzzP +++= For z= 3, we have: Example: calculate the 2nd derivative of the following complex polynomial at the point iz 24 −= )54()2(5)1(2)( 234 izizzizzP −+−+−−+=
  • 80. Xnumbers Tutorial 79 With DPOLYN and POLYN it is very easy to implement, for example, the Newton’s algorithm for finding the polynomial root with high precision Example: find the real root of the following polynomial with Newton’s algorithm 8000645 367 −+− xxx The popular iterative Newton’s formula is )(' )( 1 i i ii xp xp xx +=+ Starting from the point x = 10. Note that we cannot use the handly x = 0 , because the derivative is zero The exact digits caught by the algorithm, are shown in blue. Note the impressive acceleration. Try this example with 60 and more digits if you like. Polynomial coefficients =PolyTerms(Polynomial) Returns the vector of the polynomial coefficients The argument is a polynomial string like "1-3x+5x^2 +x^5" in any order. Example
  • 81. Xnumbers Tutorial 80 Note the braces { } in the formula. This indicates that the function return a vector. We must select the range before enter the function with "shift+ctrl+enter". Polynomial writing =PolyWrite(Coefficients, [variable]) It returns the polynomial string from its coefficients. The first argument may be a (1 x n) vector or an (2 x n) array. In the last case, the first row indicates the coefficient position and the second row contains the correspondent coefficient value. The second optional argument specifies the variable string (default is "x"). Note that the second argument "t" must be insert as string, that is between quotes "..." Polynomial addition =PolyAdd(Poly1, Poly2) Performs the addition of two polynomials. The arguments are monovariable polynomial strings. Example: PolyAdd("1-3x" , "-2-x+x^2") = "-1-4x+x^2" . Polynomial multiplication =PolyMult(Poly1, Poly2) Performs the multiplication of two polynomials The arguments are monovariable polynomial strings. Example: PolyMult("1-3x" , "-2+5x+x^2") = "-2+11x-14x^2-3x^3" . ( )( ) 322 3141125231 xxxxxx −−+−=++−−
  • 82. Xnumbers Tutorial 81 Polynomial subtraction =PolySub(Poly1, Poly2) Returns the difference of two polynomials. The arguments are monovariable polynomial strings. Example: PolySub("1-3x" , "-2+5x+x^2") = "3-8x-x^2" . Polynomial division quotient =PolyDiv(Poly1, Poly2) Returns the quotient of two polynomials. The arguments are monovariable polynomial strings. Example: PolyDiv("x^4-1" , "x^2-x-1") = "2+x+x^2" . In fact: ( )( ) xxxxxx 31211 224 ++++−−=− Polynomial division remainder =PolyRem(Poly1, Poly2) Returns the remainder of two polynomials The arguments are monovariable polynomial strings.
  • 83. Xnumbers Tutorial 82 Hermite’s and Cebychev’s polynomials By the basic operations we can build any other polynomial. Example: Calculate the first 9th degree Cebychev’s and Hermite’s polynomials Cebysev’s polynomials can be obtained by the iterative formula Hermite’s polynomials can be obtained by the iterative formula xTT == 10 ,1 11 2 −+ −⋅= nnn TTxT xHH == 10 ,1 11 22 −+ ⋅−⋅= nnn HnHxH The two iterative formulas can be arrange as: =polysub(PolyMult("2x",Tn),Tn-1) =polysub(PolyMult("2x",Hn),PolyMult(2*n,Hn-1)) These functions are inserted from the cell B4 to B9 and C5 to C9 Legendre’s Polynomials Legendre’s polynomials can be obtained by the following well known iterative formula xPPxP n n xPx n n xP nnn ==⋅ − −⋅⋅ − = −− 1021 ,1,)( 1 )( 12 )( The first five polynomials are: ( ) ( ) ( )33035 8 1 ,35 2 1 ,13 2 1 ,,1 24 4 3 3 2 210 +−=−=−=== xxPxxPxPxPP The above formula is very popular, but from the point of view of numeric calculus has one disadvantage: its coefficients are decimal and this causes round-off errors leading inaccuracy for higher polynomial degree. It is convenient to rearrange the iterative formula to avoid fractional coefficients. Let’s assume that a Legendre’s polynomial can be written as )( 1 )( xL k xP n n n = Where kn is an integer number and Ln(x) is a polynomial having integer coefficients The Legendre’s polynomial Pn(x) is completely defined by the couple of (kn , Ln(x)) Starting with (1a)
  • 84. Xnumbers Tutorial 83 1 1 1 0 = = k k xL L = = 1 0 1 We can show that the following iterative process, with n ≥ 2 , gives the couples (kn , Ln(x)) )()()()( )1( )12()( 21 1 2 xLaxLxUxV nka xnkxU nnnnn nn nn −− − − ⋅−⋅= −⋅= −⋅= ( ))(, 21 nnn nnn VcoefbGCDc kknb = ⋅⋅= −− Where the coeff operator returns the coefficients vector of the polynomial Vn(x), and the GCD is the greatest common divisor. Simplifying, we get, finally the couple (kn , Ln(x)) )( 1 )( xV c xL c b k n n n n n n = = This iterative algorithm, working only with integer values, is adapted to build Legendre’s polynomials with high degree. Let’s see how to arrange a worksheet for finding Legendre’s polynomial In the first column we insert the degree n, beginning from 0 to 2, for the moment In the last two columns “k” and “L(x)” we have added the starting values. The row 6 contains all the functions that the process needs. In particularly we note: The function polyterms(D6) gives the coefficients vectors [-1, 0, 3] of V(x) = -1+3x^2 The function xMCD returns the greatest common divisor of [-1, 0, 3, 2] ⇒ 1 Select the row 6 and drag it down. We generate the Legendre’s polynomial in the form (1a)
  • 85. Xnumbers Tutorial 84 Here is a table of Legendre’s polynomials obtained with the above method n k L(x) 0 1 1 1 1 x 2 2 -1+3x^2 3 2 -3x+5x^3 4 8 3-30x^2+35x^4 5 8 15x-70x^3+63x^5 6 16 -5+105x^2-315x^4+231x^6 7 16 -35x+315x^3-693x^5+429x^7 8 128 35-1260x^2+6930x^4-12012x^6+6435x^8 9 128 315x-4620x^3+18018x^5-25740x^7+12155x^9 10 256 -63+3465x^2-30030x^4+90090x^6-109395x^8+46189x^10 11 256 -693x+15015x^3-90090x^5+218790x^7-230945x^9+88179x^11 12 1024 231-18018x^2+225225x^4-1021020x^6+2078505x^8-1939938x^10+676039x^12 13 1024 3003x-90090x^3+765765x^5-2771340x^7+4849845x^9-4056234x^11+1300075x^13 We can also extract a table of Legendre’s coefficients by the Polyterms() function
  • 86. Xnumbers Tutorial 85 Polynomial shift =PolyShift(Poly, x0) Performs the polynomial translation to x0, The argument "Poly" can be the polynomial strings or the vector of polynomial coefficients. This function returns the coefficient vector of the translated polynomial. If you select one cell, the output will be a polynomial string Example. Given the polynomial: 65432 1448633275809495250447389623188784918 xxxxxx +−+−+− substituting x with z+24, we have 6432 916918 zzzzz +−−−+− This function is useful for transforming polynomial for reducing the coefficients amplitude and improving the precision of rootfinder methods. In this example we work with coefficients of two maximum digits, instead of 9 digits. We note also that the second polynomial, having the second coefficient = 0, is centered. His roots are the same of the given polynomial, translated of 24, but can be factorize much better. In fact, we have )9)(2)(1( 222 −+++− zzzzz Polynomial center =PolyCenter(Coefficients) Returns the center of the polynomial roots circle The argument specifies the vector of the polynomial coefficients in the following order: [ ]naaaa ...,, 210 It can also be a polynomial string if x1, x2, ...xn are roots of polynomial the center Bx is defined as: n a n xxxx nn x 1210 ... B −− = +++ =
  • 87. Xnumbers Tutorial 86 Polynomial roots radius =PolyRadius(Coefficients) Returns the approximated radius of the polynomial roots circle. The argument is the vector of the polynomial coefficients in the following order: [ ]naaaa ...,, 210 It can also be a polynomial string If zi are the roots of a polynomial, the radius is defined as: |)(|max ...1 i ni zR = = .The circle of root is very useful for locating all the roots of a polynomial. For example, given the following 9 degree polynomial. degree coefficents a0 -3098250 a1 4116825 a2 -2427570 a3 916272 a4 -244674 a5 46934 a6 -6430 a7 608 a8 -36 a9 1 The center = 4 and the radius ≅ 6.8 We can draw the circle containing, with high probability, all polynomial roots We know that the roots of this polynomial are: x real x imm 9 0 5 0 2 0 3 -6 3 6 1 -4 1 4 6 -3 6 3 We have to point out that this method is probabilistic. It means that, picking-up a random polynomial, the most part of the roots are found inside the circle but it is also possible to find same roots outside the circle with 1% of probability. Example: compute the root circle of the polynomial: x7 -5x6 +64x3 -8000
  • 88. Xnumbers Tutorial 87 radius ≅ 4.331 center ≅ 0.714 The roots are: x real x imm -2.7429701 1.6132552 -2.7429701 -1.6132552 -0.4369651 3.2182957 -0.4369651 -3.2182957 3.17993518 2.2060806 3.17993518 -2.2060806 5 0 Polynomial building from roots =PolyBuild(Roots, [Variable]) Builds a polynomial from its roots. Argument "Roots" is an (n x 2) array, contains the polynomial roots. It can be an (n x 1) vector for real roots. This function returns the coefficient vector of the polynomial. If you select one cell, the output will be a polynomial string Complex roots for real polynomial: Multiple roots: Complex roots for complex polynomial If the complex roots are not symmetrical, the polynomial has both real and imaginary part. Zero roots .If you want a polynomial with multiple zero roots, simply repeat many couple [0, 0] as it needs. This function returns the vector of polynomial coefficients if you select more than two vertical cells. It is useful for higher degree polynomial
  • 89. Xnumbers Tutorial 88 In this example we get the 10 coefficients of the 9th degree polynomial having the 9 roots in the range A2:B10. Select the range E2:E11 and insert the function PolyBuild with the CTRL+SHIFT+ENTER sequence. If complex roots are not conjugate, the polynomial has complex coefficients. This function returns also the imaginary part of the coefficients. Simply select the range D2:E6.
  • 90. Xnumbers Tutorial 89 Polynomial building with multi-precision PolyBuildCfx() This macro generates the polynomial coefficients from the polynomial roots. This macro works like the function PolyBuild except that it works in multi-precision. It is useful for high degree polynomial, when the coefficients become longer than 15 digits. For using this macro select the range containing the roots. Then, start the macro. Choose the digits precision (default=30) and the range you want to paste the coefficients (default is the range at the right side of the roots range selected). In the following table we have calculated the coefficient of the polynomial having as roots the first 19 integer numbers. That is: x1 = 1 , x2 = 2, x3 = 3 ,.... x19 = 19 Roots PolybuildCfx (30 digits) PolyBuild Diff. 1 -121645100408832000 -121645100408832000 0 2 431565146817638400 431565146817638000 400 3 -668609730341153280 -668609730341153000 -280 4 610116075740491776 610116075740492000 -224 5 -371384787345228000 -371384787345228000 0 6 161429736530118960 161429736530119000 -40 7 -52260903362512720 -52260903362512700 -20 8 12953636989943896 12953636989943900 -4 9 -2503858755467550 -2503858755467550 0 10 381922055502195 381922055502195 0 11 -46280647751910 -46280647751910 0 12 4465226757381 4465226757381 0 13 -342252511900 -342252511900 0 14 20692933630 20692933630 0 15 -973941900 -973941900 0 16 34916946 34916946 0 17 -920550 -920550 0 18 16815 16815 0 19 -190 -190 0 1 1 0 As we can see there are a little difference (digits in red) between the exact coefficients computed by this macro PolyBuildCfx (multiprecision arithmetic with 30 digits) and those returned by the function PolyBuild (standard double precision).
  • 91. Xnumbers Tutorial 90 Polynomial Solving =PolySolve (Polynomial) This function returns the roots of a given real polynomial using the Jenkins-Traub algorithm. n n xaxaxaa ...2 210 +++ The arguments can be a monovariable polynomial strings like "X^2+3x+2" or a vector of coefficients This function returns an (n x 2) array. It uses the same algorithm of the RootfinderJT macro. It works fine with low-moderate degree polynomials, typically up to 10th degree. For higher degree it is more convenient to use the macro. Example. Find all roots of the given 10 degree polynomial Integer polynomial =PolyInt(Polynomial) This function returns a polynomial with integer coefficients having the same roots of the given polynomial. This transformation is also know as "denormalization" and can be useful when the coefficients of the normalized polynomial are decimal. Example: Given thefollowing polynomial: -0.44+2.82x-3.3x^2+x^3 To eliminate decimal coefficients we denormalize the polynomial -22+141x-165x^2+50x^3 = PolyInt("-0.44+2.82x-3.3x^2+x^3") Take care with the denormalization because the coefficients became larger and the computation may lose accuracy. See the example below The following polynomials have the same root x = 11/10: Pb(x) = -2.4024+10.1524x-17.1x^2+14.35x^3-6x^4+x^5 Pa(x) = -6006+25381x-42750x^2+35875x^3-15000x^4+2500x^5
  • 92. Xnumbers Tutorial 91 If we compute both polynomials for x = 11/10, with standard double precision we get: Pa(1.1) = -2.664E-15 Pb(1.1) = 4.547E-12 As we can see, the first value, obtained by the decimal polynomial, is 1000 times more precise then the one obtained by the integer polynomial Polynomial System of 2nd degree =SYSPOLY2(Poly1, Poly2) Solves a system of two 2nd degree polynomials.     =+++++ =+++++ 0 0 202524 2 2322 2 21 101514 2 1312 2 11 ayaxayaxyaxa ayaxayaxyaxa It returns a (4 x 4) array containing the four solutions. The parameters "Poly1" and "Poly2" can be coefficients vectors or polynomials strings The coefficients must be passed in the same order of the above equation. Polynomial strings, on the contrary, can be written in any order. Examples of 2nd degree x-y polynomials strings are: 13+x+y^2-y+x^2+2x*y x^2 + y^2 - 10 4x^2+8x*y+y^2+2x-2 Note: the product symbol “*” can be omitted except for the x*y mixed term A 2nd degree system can have up to four solutions. It can also have no solution (impossible) or even infinite solutions (undetermined). The function returns #N/D if a solution is missing Example: solve the following system     =−+ =−+++ 010 02 22 22 yx yxyxyx Using SYSPOLY2 the solutions – real or complex – can be obtained in a very quick way Real solutions represent the intersection point of the curve poly1 and poly2. They are: P1 = (−3 , 1) , P2 = (−1 , 3) The system has also two complex solutions that have not a geometrical representation P3 = (2.5 +j 1.118034 , −2.5 +j 1.118034) , P4 = (2.5 −j 1.118034 , −2.5 −j 1.118034) The degree of the given system is 4 Example: solve the following system    =−+++ =− 012 01 2 yxyxy xy
  • 93. Xnumbers Tutorial 92 The apparent degree of the system is 2 x 2 = 4 As we can see, the function SYSPOLY2 returns only three solutions: one real and two complex. P1 = (−1, −1) , P2 = (−j , j ) , P3 = = ( j , −j ) Thus, the actual system degree is 3. Bivariate Polynomial =POLYN2(Polynomial, x, y, [DgtMax]) Returns the value - real or complex - of a bivariate polynomial P(x, y). The parameter "Polynomial" is an expression strings. Valid examples are: 13+x+y^2-y+x^2+2x*y , x^2+y^2-10 , 8x*y+y^2+2x-2 , 10+4x^6+x^2*y^2 Note: the product symbol “*” can be omitted except for the x*y mixed terms The third optional parameter DgtMax sets the multiprecision. If missing, the computation is performed in faster double precision. The variables x , y can be real or complex. The function can return real or complex numbers. Select two cells if you want to see the imaginary part and give the CTRL+SHIFT+ENTER sequence Example: Compute the polynomial yxyxyxP −+++= 22 2 at the point x = (2.5 + j 1.11803398874989) y = (−2.5 + j 1.11803398874989) Verify that it is a good approximation of the polynomial root
  • 94. Xnumbers Tutorial 93 Partial fraction decomposition Partial fraction decomposition is the process of rewriting a rational expression as the sum of a quotient polynomial plus partial fractions.. ∑+=+= iFxQ xD xR xQ xD xN )( )( )( )( )( )( where each Fi is a fractions of the form m m px A px A px A )( ... )( 2 21 + + + + + or m mm cbxx CxB cbxx CxB cbxx CxB )( ... )( 222 22 2 11 ++ + + ++ + + ++ + being m is the multiplicity of the correspondent root The denominators D(x) is determined from the poles of the fractions itself. In fact, p is a real root of D(x), while the quadratic factor can be obtained from the complex root using the following relation 22 ,2 βααβα +=−=⇒± cbi (1) Many calculators and computer algebra systems, are able to factor polynomials and split rational functions into partial fractions. A solution can also be arranged in Excel with the aid of Xnumbers functions. Let's see Real single poles. Find the fraction decomposition of the following rational fraction 541056515 179414332763 )( )( 234 23 +−+− +−+ = xxxx xxx xD xN First of all, we try to find the roots of the denominator using, for example, the function polysolve. We find that the roots are pi = [1, 2, 3, 9]. They are all real with unitary multiplicity, therefore the fraction expansion will be 4 4 3 3 2 2 1 1 )( )( px A px A px A px A xD xN + + + + + + + = where pi are the roots and Ai are unknown Several methods exist for finding the fraction coefficients Ai . One of the most straight and elegant is due to Heaviside that, for a real single root, simply states: )(' )( i i i pD pN A = where D'(x) is the derivative of D(x) A possible arrangement in Excel is the following
  • 95. Xnumbers Tutorial 94 Therefore, the searched decomposition is 9 40 3 5 2 8 1 40 541056515 179414332763 234 23 + + + − + + + −= +−+− +−+ xxxxxxxx xxx You can prove yourself that this expression is an identity, thus always true for every x, except the poles. Complex single poles. Find the fraction decomposition of the following rational fraction 65042292 1235221 )( )( 234 23 +−−− ++−− = xxxx xxx xD xN First of all, we try to find the roots of the denominator using, for example, the function polysolve. We find that the roots are }34,25{ iip ±−±= . They are complex with unitary multiplicity, therefore the fraction expansion will be 22 2 22 11 2 11 )( )( cxbx CxB cxbx CxB xD xN ++ + + ++ + = where bi and ci , calculated by the (1), are 25,8,26,10 2211 ===−= cbcb The coefficients Bi and Ci are unknown. For solving them we used here the so called undetermined coefficients method Renamed, for simplicity: 22 2 211 2 1 )(,)( cxbxxDcxbxxD ++=++= The fraction expansion may be rewritten as )()()()()( )( 2 2 2 2 1 1 1 1 xD C xD xB xD C x xD xB xD xN +++= Giving 4 different values to x, the above relation provides 4 linear equations with the unknown B1, C1, B2, C2, that can be easily solved. We can choose any value that we want; for example xi = { 0, 1, 2, 3 } and we get the following linear system 0 1/26 0 1/25 B1 123/650 1/17 1/17 1/34 1/34 C1 9/34 1/5 1/10 2/45 1/45 X B2 = 3/10 3/5 1/5 3/58 1/58 C2 63/290 Solving this linear system by any method that we like, for examply by SYSLIN , we get the solution [B1 , C1 , B2 , C2] = [-2 , 7 , 1 , -2] Substituting these values, we have finally the fraction decomposition
  • 96. Xnumbers Tutorial 95 258 2 2610 72 65042292 1235221 22234 23 ++ − + +− +− = +−−− ++−− xx x xx x xxxx xxx You can prove yourself that this expression is always valid for any compatible value of x A possible arrangement for solving this problem In Excel is a bit more complicated then the previous one. Let's see. First of all we compute the roots with the function Polysolve; then we compute the trinomials D1(x) and D2(x) by the formulas (1) Then we compute the polynomials N, D, D1, D2 for each values of x by the function polyn. We get the 4x5 table visible at the right From this table we get the complete system matrix in the following way. The 4 x 4 linear system can be solved by any method that you want. For example by matrix inversion as shown in the example.
  • 97. Xnumbers Tutorial 96 Orthogonal Polynomials Orthogonal polynomials are a class of polynomials following the rule: ∫ ⋅= b a nmnnm cdxxpxpxw δ)()()( Where m and n are the degrees of the polynomials, w(x) is the weighting function, and c(n) is the weight. nmδ is the Kronecker's delta function being 1 if n = m and 0 otherwise. The following table synthesizes the interval [a, b], the w(x) functions and the relative weigh c(n) for each polynomials family polynomial interval w(x) Chebyshev polynomial of the first kind [-1, 1] 2/12 )1( − − x    ≠ = 0for2/ 0for n n π π Chebyshev polynomial of the second kind [-1, 1] 2/12 )1( x− 2/π Gegenbauer polynomial [-1, 1] 2/12 )1( − − α x 0for/2 0for )()(! )2Γ(2 2 2 21 = ≠ Γ⋅+ +⋅ απ α αα n nn αnπα- Hermite polynomial (-∞,+∞) 2 x e− !2 nn π Jacobi polynomial (-1, 1) βα )1()1( xx +− nh Laguerre polynomial [0, +∞) x e− 1 generalized Laguerre polynomial [0, +∞) xk ex − ! )!( n kn + Legendre polynomial [-1, 1] 1 12 2 +n Where )1(! )1()1( 12 2 1 +++Γ⋅ ++Γ⋅++Γ +++ = ++ βα βα βα βα nn nn n hn
  • 98. Xnumbers Tutorial 97 Orthogonal Polynomials evaluation This set of functions1 calculate the orthogonal polynomials and their derivatives at the given point . They return two values: the first one is the polynomial value, the second is its 1st derivative. If you want to see both values select two adjacent cells and give the CTRL+SHIFT+ENTER sequence. If you give ENTER, you will get only the polynomial value Function Poly_ChebychevT(x, n) Chebychev polynomial of the first kind Function Poly_ChebychevU(x, n) Chebychev polynomial of the second kind Function Poly_Gegenbauer(a, x, n) Gegenbauer polynomial Function Poly_Hermite(x, n) Hermite polynomial Function Poly_Jacobi(a, b, x, n) Jacobi polynomial Function Poly_Laguerre(x, n, m) Laguerre generalized polynomial Function Poly_Legendre(x, n) Legendre polynomial Example: Tabulate the Legendre polynomial of 6th degree, for 0 ≤ x ≤ 1, with step h = 0.1 As we can see we have insert Poly_Legendre as a standard function, because in this exercise we do not need the derivative information Example. Find the greatest zero of the 5th degree Legendre polynomial We can use the Newton-Raphson method, starting from x = 1, as shown in the worksheet arrangement. Both polynomial and derivative are obtained from the Poly_Legendre simply selecting the range B5:C5 and pasting the function as array with CTRL+SHIFT+ENTER sequence. The other cells are filled simply by dragging down the range B5:C5 1 Many thanks to Luis Isaac Ramos Garcia for his great contribution in developing this software
  • 99. Xnumbers Tutorial 98 Function Poly_ChebychevT(x, [n]) Function Poly_ChebychevU(x, [n]) Evaluate the Chebychev orthogonal polynomial of 1st and 2nd kind Parameters: x (real) is the abscissa, n (integers) is the degree. Default n = 1 Function Poly_Gegenbauer(L, x, [n]) Evaluate the Gegenbauer orthogonal polynomial of 1st and 2nd kind Parameters: x (real) is the abscissa, n (integers) is the degree. Default n = 1 L (real) is the Gegenbauer factor and must be L < 1/2 Function Poly_Hermite(x, [n]) Evaluate the Hermite orthogonal polynomial of 1st and 2nd kind Parameters: x (real) is the abscissa, n (integers) is the degree. Default n = 1 Function Poly_Jacobi(a, b, x, [n]) Evaluate the Jacobi orthogonal polynomial of 1st and 2nd kind Parameters: x (real) is the abscissa, n (integers) is the degree. Default n = 1 a (real) is the power of (1-x) factor of the weighting function b (real) is the power of (1+x) factor of the weighting function Function Poly_Laguerre(x, [n], [m]) Evaluate the Laguerre orthogonal polynomial of 1st and 2nd kind Parameters: x (real) is the abscissa, n (integers) is the degree. Default n = 1 m (integer) is the number of generalized polynomial. Default m = 0 Function Poly_Legendre(x, [n]) Evaluate the Legendre orthogonal polynomial of 1st and 2nd kind Parameters: x (real) is the abscissa, n (integers) is the degree. Default n = 1
  • 100. Xnumbers Tutorial 99 Weight of Orhogonal Polynomials This set of functions calculate the weight c(n) of each orthogonal polynomial p(x, n) [ ]∫= b a nn dxxpxwc )()( 2 Function Poly_Weight_ChebychevT(n) Chebychev polynomial of the first kind Function Poly_Weight_ChebychevU(n) Chebychev polynomial of the second kind Function Poly_Weight_Gegenbauer(n, l) Gegenbauer polynomial Function Poly_Weight_Hermite(n) Hermite polynomial Function Poly_Weight_Jacobi(n, a, b) Jacobi polynomial Function Poly_Weight_Laguerre(n, m) Laguerre generalized polynomial Function Poly_Weight_Legendre(n) Legendre polynomial If we divide each orthogonal polynomial family for the relative weight we have an orthonormal polynomial family Zeros of Orthogonal Polynomials This macro finds all roots of the most popular orthogonal polynomials Its use is very easy. Simply start the Zero macro from the menu "tools > Ortho-polynomials..." Choose the family and the degree that you want and fill the optional parameters Then press OK This is an example of output for a Laguerre polynomial of 6th degree ( m = 0) Note: the format is added for clarity. The macro does not do this
  • 101. Xnumbers Tutorial 100 Coefficients of Orthogonal Polynomials This macro calculate the coefficients of the most common orthogonal polynomials Its use is very easy. Simply start the Coeff macro from the menu "tools/Ortho-polynomials..." Choose the family and the degree that you want and fill the optional parameters. Then, press OK This macro return also the polynomial weight This is an example of output for a Laguerre polynomial of 4th degree (m = 0) The ortho-polynomial can be written as ( )24967216 24 1 )( 234 6 +−+−= xxxxxL
  • 102. Xnumbers Tutorial 101 Complex Arithmetic and Functions Xnumbers provides a large collection of complex functions Complex Addition Complex Subtraction Complex Multiplication Complex Division Polar Conversion Rectangular Conversion Complex absolute Complex power Complex Root Complex Log Complex Exp Complex inv Complex negative Complex conjugate Complex Sin Complex Cos Complex Tangent Complex Inverse Cos Complex Inverse Sin Complex Inverse Tan Complex Hyperbolic Sin Complex Hyperbolic Cos Complex Hyperbolic Tan Complex Inverse Hyperbolic Cos Complex Inverse Hyperbolic Sin Complex Inverse Hyperbolic Tan Complex digamma Complex Exponential Integral Complex Error Function Complex Complem. Error Function Complex Gamma Function Complex Logarith. Gamma Function Complex Zeta Function Complex Quadratic Equation Complex Expression Evaluation How to insert a complex number For definition a complex number is an ordered couple of numbers: (a,b) In Excel a couple of numbers is represented by two vertical or horizontal adjacent cells and the complex number (a, b) is a range of two cells. The figure below shows both vertical and horizontal representations: (234 , 105) in range "B7:C7" and in range "B2:B3" ( -100 , 23) in the range "E7:F7" and in range "D2:D3" For entering complex functions you must select two cells, insert the complex function and give the CTRL+SHIFT+ENTER keys sequence. If you press the ENTER key, the function returns only the real part of the complex number. Symbolic rectangular format Xnumbers support the format "x+jy " only in expression strings passed to the function cplxeval. Except this case, you must always provides a complex number as a couple of real numbers (one or two cells). The reason for this choice is that the rectangular format is more adapt for symbolic calculation while the array format is more convenient for numerical computation characterized by long non- integer numbers. But, of course, you can convert a complex number (a,b) into its symbolic format "a+jb" by the Excel function COMPLEX, as shown in the following example
  • 103. Xnumbers Tutorial 102 XNUMBERS has two sets of complex functions: for standard double precision (prefixed by "cplx") and for multiprecision (prefixed by "xcplx"). Complex Addition xcplxadd(a, b, [Digit_Max]) cplxadd(a, b) Performs the complex addition: ( ) ( ) ( )21212121 bb,aab,ba,a ++=+ Complex Subtraction xcplxsub(a, b, [Digit_Max]) cplxsub(a, b) Performs the complex subtraction. ( ) ( ) ( )21212121 bb,aab,ba,a −−=− Complex Multiplication xcplxmult(a, b, [Digit_Max]) cplxmult(a, b) Performs the complex multiplication: ( ) ( ) ( )122122112121 baba,ba-bab,b*a,a += Complex Division xcplxdiv(a, b, [Digit_Max]) cplxdiv(a, b) Performs the complex division ( ) ( )       + − + + = 2 2 2 1 2112 2 2 2 1 2211 21 21 bb baba , bb baba b,b a,a
  • 104. Xnumbers Tutorial 103 Polar Conversion xcplxpolar(z, [angle], [Digit_Max]) cplxpolar(z, [angle]) Converts a complex number from its rectangular form to the equivalent polar form. The optional parameter angle sets the angle unit (RAD, DEG) (default RAD). (x, y) ⇒ ( ρ , θ ) Where 22 yx +=ρ 0,atan >      = x x y θ 0, 2 )sgn( =⋅= xy π θ      <≠⋅+      <= = 0,0,)sgn(atan 0,0, xyy x y xy π π θ Rectangular Conversion xcplxrect(z, [angle], [Digit_Max]) cplxrect(z, [angle]) Converts a complex number from its polar form to the equivalent rectangular form. The optional parameter angle sets the angle unit (RAD, DEG) (default RAD). ( ρ , θ ) ⇒ (x, y) Where ( ) ( )θρ θρ sin cos = = y x Complex absolute xcplxabs(z, [Digit_Max]) cplxabs(z) Returns the absolute value of a complex number 2 2 2 1|| zzz += x y ρ θ (deg) 1 0 1 0 0.866025 0.5 1 30 0.707107 0.707107 1 45 0.5 0.866025 1 60 0 1 1 90 -0.5 0.866025 1 120 -0.70711 0.707107 1 135 -0.86603 0.5 1 150 -1 0 1 180 -0.86603 -0.5 1 -150 -0.70711 -0.70711 1 -135 -0.5 -0.86603 1 -120 0 -1 1 -90 0.5 -0.86603 1 -60 0.707107 -0.70711 1 -45 0.866025 -0.5 1 -30
  • 105. Xnumbers Tutorial 104 Complex power xcplxpow(z, [n], [Digit_Max]) cplxpow(z, [n]) Returns the nth integer power of a complex number zn (default n = 2) ( ) θ ρ nnnn eiyxz ⋅=+= Where       =+= y x yx atan,22 θρ Complex Roots xcplxroot(z, [n], [Digit_Max]) cplxroot(z, [n]) Returns all the nth roots of a complex extended number z1/n (default n = 2) The function returns a matrix of (n x 2) values. Remember to press the sequence CTRL+SHIFT+ENTER for insert properly this function. The root of a complex number is computed by the De Moivre-Laplace formula. 1...1,0, 2 sin 2 cos −=            + ⋅+      + ⋅=+= nk n k i n k iyxz nnn πθπθ ρ where       =+= y x yx atan,22 θρ Note: If you select only one row, the function return only the first complex root (given for k = 0). Example: compute all the 3 complex cubic roots of the number z = 8
  • 106. Xnumbers Tutorial 105 Complex Log xcplxLn(z, [Digit_Max]) cplxLn(z) Returns the natural logarithm of a complex number ( ) θρ +=+= log)log()log( iyxz Where:       =+= x y yx atan,22 θρ Complex Exp xcplxExp(z, [Digit_Max]) cplxExp(z) Returns the exponential of a complex number )sin()cos( yieyeee xxiyxz +== + Complex inverse xcplxinv(z, [Digit_Max]) cplxinv(z) Returns the inverse of a complex number 2222 11 yx y i yx x iyxz + − + = + = Complex negative xcplxneg(z) cplxneg(z) Returns the complex negative ( ) iyxiyxz −−=+−=− Complex conjugate xcplxconj(z) cplxconj(z)
  • 107. Xnumbers Tutorial 106 Returns the conjugate of a complex number iyxiyxz −=+= Complex Sin =cplxsin(z) Returns the sine of a complex number Complex Cos cplxcos(z) Returns the cosine of a complex number Complex Tangent cplxtan(z) Returns the tangent of a complex number Complex ArcCos cplxacos(z) Returns the arccosine of a complex number Complex ArcSin cplxasin(z) Returns the arcsine of a complex number Complex ArcTan cplxatan(z) Returns the arctangent of a complex number Complex Hyperbolic Sine cplxsinh(z) Returns the hyperbolic sine of a complex number Parameter “z” can be a real or complex number (two adjacent cells)
  • 108. Xnumbers Tutorial 107 Complex Hyperbolic Cosine cplxcosh(z) Returns the hyperbolic cosine of a complex number Parameter “z” can be a real or complex number (two adjacent cells) Complex Hyperbolic Tan cplxtanh(z) Returns the hyperbolic tangent of a complex number Complex Inverse Hyperbolic Cos cplxacosh(z) Returns the inverse of the hyperbolic cosine of a complex number Complex Inverse Hyperbolic Sin cplxasinh(z) Returns the inverse of the hyperbolic sine of a complex number Complex Inverse Hyperbolic Tan cplxatanh(z) Returns the inverse of the hyperbolic tangent of a complex number Complex digamma cplxdigamma(z) Returns the logarithmic derivative of the gamma function for complex argument. ( ) )( )(' )(ln)( x x x dx d x Γ Γ =Γ=Ψ Complex Exponential Integral cplxei(z) Returns the exponential integral of a complex number ∫ ∞ − − −= x t dt t e xEi )(
  • 109. Xnumbers Tutorial 108 Complex Error Function cplxerf(z) Returns the "error function" or "Integral of Gauss's function" of a complex number ∫ − = z t dtezerf 0 22 )( π Complex Complementary Error Function cplxerfc(z) Returns the complementary error function for a complex number )(1)( zerfzerfc −= Complex Gamma Function cplxgamma(z) Returns the gamma function for a complex number ∫ ∞ −− =Γ 0 1 )( dtetx tx Complex Logarithm Gamma Function cplxgammaln(z) Returns the natural logarithm of the Gamma function for a complex number Complex Zeta Function cplxzeta(z) Returns the Riemann zeta function ζ(s) for a complex number. It is an important special function of mathematics and physics which is intimately related with very deep results surrounding the prime number, series, integrals, etc. For |s|>1 the function is defined as:
  • 110. Xnumbers Tutorial 109 Complex Quadratic Equation cplxEquation2(a, b, c, [DgtMax]) Returns the multiprecision solution of the quadratic equation with complex coefficients 02 =+⋅+⋅ czbza where a, b, c are complex The solutions are found by the resolution formula a acb a b z 2 4 2 2 − ±−= This function returns an (2 x 2) array The optional parameter DgtMax, from 1 to 200, sets the number of the significant digits. If missing, the computation is in standard double precision. Example: Find the solution of the following complex equation with 20 digits precision 04)29(2 =++−+ iziz Of course the function can also works with real coefficents equations 0322 =++ zz
  • 111. Xnumbers Tutorial 110 Number Theory Maximum Common Divisor xMCD(a1, [a2]) MCD(a1, [a2], [a3]...) Returns the Maximum Common Divisor (also called Greatest Common Divisor, GCD) of two or more extended numbers The arguments "a1" and "a2" may be single numbers or arrays (range). At least, two values must be input. If "a1" is a range, "a2" may be omitted Minimum Common Multiple xMCM(a1, [a2]) MCM(a1, [a2], [a3]...) Returns the Minimum Common Multiple (also Least Common Multiple, LCM) of two or more extended numbers The arguments "a1" and "a2" may be single numbers or arrays (range). At least, two values must be input. If "a1" is a range, "a2" may be omitted Example Note that LCM may easily overcome the standard precision limit even if its arguments are all in standard precision.
  • 112. Xnumbers Tutorial 111 Rational Fraction approximation xFract(x, [Digit_Max]) Fract(x, [ErrMax]) Returns the fractional approximation of a non-integer number x, the functions returns a vector of two integer numbers, numerator N and denominator D : x ≈ N / D The optional parameter ErrMax sets the accuracy of the fraction conversion (default=1E-14). The function tries to calculate the fraction with the maximum accuracy possible. The algorithm uses the continued fraction expansion1 0,1 1,0 10 10 == == DD NN 11 11 −+ −+ +⋅= +⋅= iiii iiii DDaD NNaN Where ai are found by the following algorithm: ( ) 11 1 1 /int ++ + + ⋅−= = = iiii ii iii ayxy yx yxa In the example below we want to find the fraction form of the number 0.126. The function returns the solution: N = 63 , D = 500 0.126 ≅ 63 / 500 Often the rational form is not so easy to find, and depends strongly on the precision we want to reach. See, for example, the fractions that approximate 2 with increasing precision Digit N D N/D Error 2 3 2 1.500000000000000 0.08579 3 7 5 1.400000000000000 0.01421 4 41 29 1.413793103448280 0.00042 5 99 70 1.414285714285710 7.2E-05 6 239 169 1.414201183431950 1.2E-05 7 1393 985 1.414213197969540 3.6E-07 8 3363 2378 1.414213624894870 6.3E-08 9 8119 5741 1.414213551646050 1.1E-08 10 47321 33461 1.414213562057320 3.2E-10 11 114243 80782 1.414213562427270 5.4E-11 12 275807 195025 1.414213562363800 9.3E-12 13 1607521 1136689 1.414213562372820 2.8E-13 14 3880899 2744210 1.414213562373140 4.2E-14 15 9369319 6625109 1.414213562373090 1.3E-14 You can regulate the desiderate approximation with the parameter ErrMax 1 from The art of Computer Programming, D.E.Knuth, Vol.2, Addison-Wesley, 1969
  • 113. Xnumbers Tutorial 112 Continued Fraction xFractCont(x, [Digit_Max]) FractCont(x) These functions perform the continued fraction expansion of a decimal number x Usually the continued fraction is written in the compact form x = [a0, a1, a2, a3...] Each step gives the rational fraction fi = si / ti , where the fractions fi converge to the number x .... 1 1 1 3 2 1 0 + + + += a a a ax Both the above functions return an array containing the integer values [ai, si, ti]. The algorithm stops when the fraction approximates x with the maximum precision allowed, that is 10^-15 and 10^-(DgtMax) respectively for the standard and the multiprecision function. Example. Compute the continued fraction expansion of the number e2 = 7.38905... with standard precision (error < 1E-15) As we can see, the continued fraction of the transcendental real number e2 can be written as: e2 = [7, 2, 1, 1, 3, 18, 5, 1, 1, 6, 30, 8...] while the correspondent approximating fractions are e2 ≅ 15/2 (1.5%), e2 ≅ 22/3 (0.75%), e2 ≅ 37/5 (0.15%), e2 ≅ 133/18 (0.002%), .... Sometime the continued fraction expansion requires the multiprecision. Expand, for example, the Ramanujian number 58π e ≅ 24591257751.9999998222132414696... with 30 significant digits As we can see, the last fraction: 101111070168039846853 / 4111667292 approximates 58π e with a relative error of about 1E-30
  • 114. Xnumbers Tutorial 113 Continued Fraction of Square Root FractContSqr(n) This function returns the continued fraction expansion of the square root of an integer number [ ]03210 2...,,, aaaaan = .... 1 1 1 3 2 1 0 + + + += a a a an As known, the expansion is infinite and periodic. The algorithm stops when it come across into a term double of the first term a0 This function returns the (m+1) vector [a0, a1, a2,...am ] where am = 2*a0 Use the ctrl+shift+enter sequence. Example. Calculate the continued fraction of 77 Therefore the infinite, periodic continued fraction is [ ]....16,1,3,2,3,1,16,1,3,2,3,1,16,1,3,2,3,1,877 = Note that the function returns only the first terms + the period. Check Prime Prime(n) Prime(n) = "P" if n is prime, or the lowest factor if n is not prime Returns "?" if the function is not able to check it. Example prime(134560093) ="P" prime(134560079)= 89 Next Prime NextPrime(n) This function1 returns the prime number greater than n or "?" if the function is not able to calculate it nextprime(9343560093) = 9343560103
  • 115. Xnumbers Tutorial 114 Modular Addition xaddmod(a, b, m) Performs the modular addition1 )(mod)( mba + where a, b integer and m positive integer Modular Subtraction xsubmod(a, b, m) Performs the modular subtraction )(mod)( mba − where a, b integer and m positive integer Modular Multiplication xmultmod(a, b, m) Performs the modular multiplication )(mod)( mba⋅ where a, b integer and m positive integer Modular Division xdivmod(a, b, m) Performs the modular division )(mod)/( mba where a, b integer and m positive integer Remember that the modular division is always possible if, and only, the module m is prime. Otherwise the division could be impossible. In that case the function returns "?" Modular Power xpowmod(a, p, m) Performs the modular integer power of ap That is defined as the remainder of the integer division of ap by m       ⋅−= m a mar p p 1 Modular function xaddmod, xsubmod, xmultmod, xdivmod appear thanks to the courtesy of John Jones
  • 116. Xnumbers Tutorial 115 Example: compute )0059(mod324 xpowmod(3,24,9005) = 3306 It's easy to prove that 3306)9005mod(812824295364)9005mod(324 == When the number a or p become larger it is impossible to compute the integer power directly. But the function xpowmod can return the correct result. Examples: compute )3001mod(123939040 It would be impossible to compute all the digits of this power. Using multiprecision we have xpow(12,3939040) = 1.24575154970238125896669174496E+4250938 This result shows that 123939040 has more then 4 million of digits! Nevertheless the remainder of this impossible division is xpowmod(12, 3939040,3001) = 947 Examples: Miscellanea of modular function Observe that m = 1000453 is prime.
  • 117. Xnumbers Tutorial 116 Perfect Square xIsSquare(n) Checks if a number n is a perfect square xisSquare(1000018092081830116) = TRUE Because: 1000018092081830116 = 10000090462 xisSquare(2000018092081830116) = FALSE Check odd/even xIsOdd (n) Checks if a number n is odd (TRUE) or even (FALSE) Check Integer xIsInteger(x) Checks if a number x is integer
  • 118. Xnumbers Tutorial 117 Macro - Factorize Factorize() This macro factorize a number of active cell returning the list of prime number and their exponents ke k eee ppppn ...3 3 2 2 1 1= Select the cell contains the number you want to factorize and the run the macro Factorize from the menu or Prime... > Factorize Example. Assume to have in the cell A2 the following extended number 13320493696756339619246691430 Click “copy” if you want to copy the list in the worksheet, starting from the cell just below cell A2. This macro uses the trial division method with the prime table generated by the Eratostene's sieve algorithm This method is adapt for numbers having factors no more that 6 digts max. For higher factor the elaboration time becames extremely long and it cames usefull a second factorization method, the so called Pollard rho algorithm, for craking a number into two lower factors (not necessary prime). Each factors, if not prime, can be factorized separately with the trial division method. Example. The number 18446744073709551617 = 274177 * 67280421310721 can be factorized with both methods: it requires about 33 sec with trial division; but less then 3 sec with Pollard method The following number instead can be factorize only with Pollard method (about 40 sec). 10023859281455311421 = 7660450463 * 1308520867 Note that in this case both factors have 10 digits. The factos are prime so the factorization stops itself. For testing the primality see the probabilistic Prime test
  • 119. Xnumbers Tutorial 118 Large numbers having factors more than 20 digits can be only manipulate with Xnumbers but no more factorized. This task requires sophisticated algorithms joined with extreme fast routines, usually written in C++ or Assembler. When the numbers are very large, no efficient integer factorization algorithm is published; a recent effort which factored a 200 digit number (RSA-200) took eighteen months and used over half a century of computer time. The supposed difficulty of this problem is at the heart of certain algorithms in cryptography such as RSA. Not all numbers of a given length are equally hard to factor. The hardest instances of these problems are those where the factors are two randomly-chosen prime numbers of about the same size, but not too close Factoring software is available either in commercial math packages or in standalone freeware programs. One of the most interesting program released in the public domain, supporting Quadratic Sieve (QS) and Number Field Sieve (GNFS), that we have used for many years is Msieve. Batch Factorization with Msieve Msieve is a very power freeware program, created by Jason Papadopoulos, for factoring large integers. You can download the latest version of Msieve.exe from www.boo.net/~jasonp No particular installation is required. Simply copy it in any folder that you like, for example, /msieve. What Msieve Does Factoring is the study (half math, half engineering, half art form, and half... genial tricks) of taking big numbers and expressing them as the product of smaller numbers. As the number to be factored becomes larger, the difficulty involved in completing its factorization explodes and the elaboration time increase sharply. The multiprecision library contained in Xnumber, written in VBA, is no more sufficient for performing the factorization of number larger of 18-20 digits. Msieve can with high probability find the complete factorization of any input number up to about 125 digits in size. The actual number of digits supported is much higher (up to 164 digits), but problems larger than 125 digits are likely to fail. Trial division is used on all inputs; if the result is less than 25 digits in size, tiny custom routines do the factoring. For larger numbers, the code switches to more powerful methods. Prior to version 1.04, those methods were limited to the quadratic sieve (QS). From that point on, however, an implementation of Pollard-Brent algorithm and the number field sieve NFS are also available. A description of QS and NFS can be found in the Msieve Library itself with also a good amount of Quadratic Sieve references. The oldest users will be pleasant to know that Msieve.exe is a consolle program. It can be started at the prompt command (the old DOS enviroment) using the following general syntax >> msieve [options] [number] Msieve supports many useful options. For a complete list, run the command >> msieve -h For factoring one number give the following command >> msieve -q 8004000546054003543176004301 8004000546054003543176004301 p10: 1000400017 prp10: 2000000011 prp10: 4000400023 In this case the program only outputs all the factors found. The code "p10" indicates a prime factor of 10 digits; "prp10" indicates a probable prime factor of 10 digits.
  • 120. Xnumbers Tutorial 119 With the option -v (verbose), the program will also output the factoring trace with many useful information (for factoring experts, of course). >> msieve -v 8004000546054003543176004301 One useful option is also the time limit setting -d >> msieve -v -d 10 number This elaboration will stop after 10 minute, ended or not, and the intermediate results will be saved in a working file, ready for a successive restart. Onother way to stop a running elaboration is sending the key sequence CTRL+C to the active command window. The younger users will be happy to know that this elaboration can be performed directly from Excel by the macro Numbers/Factors... Note. The first time that you run Msieve from this macro, you have to provide the folder where Msieve.exe is located. Follow the simple configuration instructions. After that, the macro can start Msieve in batch input mode, returning output in Excel at the end of the elaboration. Using is very simple. Select the cell containing the number to factorize Start the macro Numbers/Factors..., choose the Msieve and click on "Factorize". If we like we can modify the job timeout (default 15 min). The "Factorization Job Manager" lists and visualizes all the jobs submitted to Msieve. Lists Refresh Imports the results in Excel Kills a running job Deletes a job Verbose mode on/off This Help The jobs list is automatically refreshed every second. If the refreshing activity stops for some reason, you can manually refresh it.
  • 121. Xnumbers Tutorial 120 The factors obtained can be imported into Excel by the apposite buttom. The macro outputs each factor in the format [exponent, factor] An active factorization job runs in a minimize window. If you like, you can open it at normal size and observe what Msieve is doing The precedent submited jobs remain memorized until they are delete. They can be recalled and viewed by the buttom Batch... of the Number Factorization panel The factorization time is usually very fast for numbers up to 50 digits and remains reasonably fast for numbers in the range 50 - 75 digits, as shows the following statistical graph 0.1 1 10 100 1000 0 10 20 30 40 50 60 70 80 Time (sec) 1.2 GHz, 256 Digits Factorization function Factor(n) This function performs the decomposition in prime factors of a given integer number Returns an array of two columns: the first column contains the prime factors and the second column contains the exponents This function is useful for factorizing on-line integer numbers N < 1E14 directly in the worksheet. It uses the so called "brute force attack" and the Fermat-Lehman method. In this example, the given number is decomposed in 5 factors 2277785128000 = 26 53 232 732 101 The #ND symbol indicates the end of factors list. To make sure to get all factors you have to extend the selection until you see this symbol Using this function is very simple. Select a range of 2 columns and several rows (for example 7 rows). The insert the function Factor using the ctrl+shift+enter sequence.
  • 122. Xnumbers Tutorial 121 Macro - Prime Numbers Generator PrimeGenerator This macro is useful to generate your own table of prime numbers. The table begins from the cell A1 of the active worksheet. Prime Test This macro perform the probabilistic prime test with the Fermat or Miller-Rabin method. These tests are especially adapt for long number. Using is very simple. Select the number that you want to test and start the macro from the menu Macros / Numbers / Prime test Select the method that you want and press "Test". After few seconds you get the results Note that this test is exact for detecting composite numbers, but it can detect a prime number with a finite probability (usually very high). The numbers satisfying the Fermat or Miller-Rabin are called "pseudo-prime" The probability is correlated to the trials number T with the following approximate formula Fermat test T p − −= 21 For T = 100 the probability is about 31 108.71 − ⋅− Miller-Rabin test T p − −= 41 For T = 50 the probability is about 31 108.71 − ⋅− You can also select a list of cells containing several numbers to test. This is useful for finding prime in a set of consecutive integers. The macro computes 10.000 (default) prime numbers for each time. The macro can be stop and restart as you like It always restarts from the last prime number written in the worksheet.
  • 123. Xnumbers Tutorial 122 Example: find the next prime number after 200000000000532100000000 A prime number must be odd, so let's begin to prepare a sequence of 20 or more odd numbers starting from 200000000000532100000001 The frequency of prime numbers, in this range, is about 5%, so we hope to find a prime number in our list. If this does not happen we try with a successive set of numbers, and so on, until a prime comes out. In this case we have found a probable prime 200000000000532100000003 You may prove by yourself that it is a true prime
  • 124. Xnumbers Tutorial 123 Diophantine Equation DiophEqu(a, b, c) This function solves the Diophantine linear equation Zyxcybxa ∈=+ , where a, b, c , x, y are all integer numbers The integer solutions can be expressed as ...2,1,0for 0 0 ±±=    ⋅+= ⋅+= k Dkyy Dkxx yk xk This function return an (2 x 2) array of four integer values. The first row contains a particular solution, while the second row contains the integer increments for generating all the solutions. If you only want a particular solution [x0, y0] simply select an array of 2 adjacent cells. If the equation has no solution the function return "?"       yx DD yx 00 Example. Find all the integer solutions of the equation 2x+3y = 6 As we can see, the function returns one solution (-6, 6) and the increments (3, -2). So all the integer solutions of the above equation can be obtained from the following formulas for any integer value of k    −= +−= ky kx k k 26 36 Often is not so easy to find the solution of a diophantine equation. Let's see the following. Long numbers. This function works also with extended numbers. Example. Find a solution of the equation ax+by = c having the following coefficients a b c 18760000596690052 13650847757772 64 Note that the first coefficients has 17 digits and the second one has 14 digits. Without multiprecision it would be difficult to solve this problem. You can enjoy yourself to prove that the result returned from the function DiophEqu is correct
  • 125. Xnumbers Tutorial 124 Brouncker-Pell Equation = PellEqu(d, [n]) This function solves the so called Brouncker-Pell quadratic equation on the integer domain Ndyxydx ∈=⋅− ,,,122 The function returns the n-th integer solution (default n = 1) using the continued fraction method. As know, this equation has non trivial solutions if d is not a perfect square. The function works for low-moderate integer d values but, even with this constraints, sometimes this smallest solution is quite small, and sometimes it is huge. Let's see. Compute the first 3 integer solutions of the equation 131 22 =⋅− yx As we can see the solutions grow sharply also for very small d. Sometime the first solution is huge also with moderate d. In that case is better to arrange the solution in vertical cells, as shown in the following examples Note that the "smallest" solution of the equation 1991 22 =⋅− yx has 30 digits ! When the solution exceed 250 digits or the time of 200 iterations the functions returns "?".
  • 126. Xnumbers Tutorial 125 Euler's Totient function = Totient(n) Returns the number of integer not exceeding and relatively prime to n This function is intended only for low-moderate numbers. Remember that two numbers n , m are relative prime if, an only if, GCD(n, m) = 1 With this function is easy to set a table of the totient φ(n) values for 1 ≤ n ≤ 100 φ(n) 1 2 3 4 5 6 7 8 9 10 0 1 1 2 2 4 2 6 4 6 4 10 10 4 12 6 8 8 16 6 18 8 20 12 10 22 8 20 12 18 12 28 8 30 30 16 20 16 24 12 36 18 24 16 40 40 12 42 20 24 22 46 16 42 20 50 32 24 52 18 40 24 36 28 58 16 60 60 30 36 32 48 20 66 32 44 24 70 70 24 72 36 40 36 60 24 78 32 80 54 40 82 24 64 42 56 40 88 24 90 72 44 60 46 72 32 96 42 60 40
  • 127. Xnumbers Tutorial 126 Integer relation A set of real numbers [x1, x2, ... xn ] , is said to possess an integer relation if there exist a set of integers [a1, a2, ...an ] , not all zero, such that a1 x1 + a2 x2 ... + an xn = 0 The integer relation algorithm can be used to solve subset sum problems, as well as to determine if a given numerical constant is equal to a root of a univariate polynomial with integer coefficients; in this case we say the constant to be "rational" suche as "12/51" or "algebraic", such as 2 . On the contrary, if such polynomial does not exist, we say the constant "transcendental", such as π. Therefore this algorithm can be used for algebraic-transcendental screening of real numbers. Because we always have an approximated value of the constant, the found integer relation will not exactly zero but very close to it only if we use a sufficient number of digits. For example, we find the following 5th degree relation [a1, a2, a3, a4, a5 ]⋅[1, x, x2 , x3 , x4 ]T = a1+ a2 x + a3 x2 + a4 x3 + a5 x4 ≅ 0 where [a1, a2, a3, a4, a5 ] = [-1, 0, -2, 0, 1] , and x = 1.553773974... Of course, a numerical discovery of a relation by this algorithm does not in general constitute a proof of this relation; one of the reasons being that the computer operates in finite precision. In many cases, however, the relations we first discovered numerically subsequently received rigorous mathematical proofs. Moreover, many complicated relations probably would never, have been dreamed of without the assistance of the computer. Algorithms for finding integer relations include HJLS algorithm, LLL algorithm, PSLQ algorithm. All these algorithms requires high-precision arithmetic, but the PSLQ, developed by Ferguson and Forcade (1979) and improved by Ferguson and Bailey (1992), is proved the most efficient and stable integer relation algorithm. The Xnumbers VB macro has been developed from the PSLQ descripted by Bailey, D. and Plouffe at http://www.cecm.sfu.ca/organics/papers/bailey Norm and Bounding. The norm of the relation is defined the Euclidean norm of the vector |x| = [x1, x2, ... xn ] Among the relations satisfinyg the constraint | ∑ xi ai | < ε , we are clearly interested in those relations having small norm, and, if possible, with the smallest nth degree. The constraint of small norm has been introduced for filtering between relations. In fact, if we can easily observe that, if the vector x becames exagerately large, then an integer relation will be surely find for any real number. | 314159265358979323 −100000000000000000 π | < 10-16 , Of course, the norm of this relation is very high (about 3.2 1017 ) and thus we have to reject it PSLQ computes the norm of any examined relation and returns the bound of the coefficients. Even if a relation is not found, the resulting bound means that cannot possibly be the root of a polynomial of degree n, with coefficients of size less than the established bound. Even negative results of this sort are often of interest. Examples given a real value x we build the vector [1, x, x2 , x3 , ... xn ] with n in increasing order n = 3, 4, 5...., we are searching for possible integer relations having small error ε < 1E-14 and small norm |x| Epprox. value: 3.189207115002720, Exact value: 224 + Relation: [14, -32, 24, -8, 1], Polynomial: 432 8243214 xxxx +−+− Epprox. value: 2.67413461226797, Exact value: 223 + Relation: [4, 24, -12, 4, 6, 0, -1] , Polynomial: 6432 6412244 xxxxx −+−−+
  • 128. Xnumbers Tutorial 127 Epprox. value: 2.991971857463750, Exact value: 323 + Relation: [-23, -36, 27, -4, -9, 0, 1], Polynomial: 6432 94273623 xxxxx +−−+−− Epprox. value: 0.809016994374947, Exact value: 2/)15( + Relation: [1, 2, -4] , Polynomial: 2 421 xx −+ Epprox. value: 0.923879532511287, Exact value: 4/)22( + Relation: [-1, 0, 8, 0, -8], Polynomial: 42 881 xx −+− Epprox. value: 0.900968867902419, Exact value: )7/cos(π Relation: [1, -4, -4, 8], Polynomial: 32 8441 xxx +−− Epprox. value: 1.839286755214160, Exact value: Relation: [1, 1, 1, -1], Polynomial: 32 1 xxx −++ Epprox. value: 0.012345678901235, Exact value: 1/81 Relation: [1, -162, 6561], Polynomial: 2 65611621 xx +− = 2 )811( x− A very simple trick for testing a relation consists in increasing the degree to n+1; if the algorithm still returns the same relation then, with high probabability, the relation is correct. But pay attention, because, sometime a numeric value can have two different relations with different degree. This means that the given value is the root of two different polynomials. Let's see the following example Epprox. value: 7.7099759466767, Exact value: 653 + Relation: [221, -108, 18, -1], Polynomial: 32 18108221 xxx −+− But the same value also returns the following relation of degree 5 Relation: [221, 113, -90, 17, -1], Polynomial: 432 1790113221 xxxx −+−+ We can verify that both relations are correct, in this case. Higher degree relations All the previous examples can be found using PSLQ in standard 64 bit precision. However, when we search for higher the Polynomial degree ( n > 6), the multiprecision is required Epprox. value: 3.00161461434129452355339436348, Exact value: 243 + Relation: [ -8, 48, -20, 56, -6, 8, 5, 0, -1], Polynomial: -1+104x2 -18x4 +8x6 -x8 Time: 0.4 sec We have seen that for 8th degree polynomial we have needed about 30 digits, in this case. Generally speaking, the required precision increases with the degree and the norm of the coefficients of the polynomial, as reported in the following table 3 1 9 33 27 19 9 33 27 19 33 +++−
  • 129. Xnumbers Tutorial 128 Degree Digits Time (sec) 10 40 0.5 15 80 4.5 16 85 5.4 20 120 23.0 25 170 81.7 28 220 126.1 30 250 217.7 Absolute limits. The table can be accessed in two ways: given a polynomial degree we get the minimum precision of the number (number of significant digits); on the contrary, given a precision of a number (digits) we get the maximum obtainable polynomial degree. For example, if we are searching for a polynomial of 15 degree, we must have a number with 80 digits, at least. If we have have calculate a number with 100 digits we can search for polynomials having degree n < 18. In the above table and graph we have also reported the time1 in second of the PSLQ (two-level) algorithm. As we can see, the time increases sharply with the degree. The time stays less the few seconds for n <16, but requires about 3-4 minutes for n = 30 and 250 digits (the maximum possible for Xnumbers) Some other high results are shownin the following list. In these trials, the problem is to recover the polynomial of degree n satisfied by α, where α = a1/r ± b1/s . In other words, the input vector x = (1, α, α2 , α2 .... αn-1 ), where n = (r s + 1). Here the working precision was set to the level required by PSLQ and the input number α was computed with the needed precision, as determined by the above table. Degree: 12. Exact value: α = 31/ 6 + 21/ 2 ≅ 2.61515051... (50 digits) Relation: [ -25, 0, 552, 0, -60, 0, 166, 0, -60, 0, 12, 0, -1] , Time: 2.8 sec Degree: 12. Exact value: α = 31/ 4 + 21/ 3 ≅ 2.57599506... (50 digits) Relation: [ -11, -216, -360, -32, 27, -288, 24, 0, -9, -8, 0, 0, 1], Time: 3 sec Degree: 15. Exact value: α = 31/ 5 + 21/ 3 ≅ 2.50565198951... (80 digits) Relation:, [ -59, -360, -540, 80, -1620, 27, -80, -540, 0, 40, -9, 0, -10, 0, 0, 1] Time: 5 sec Degree: 16. Exact value: α = 31/ 4 − 21/ 4 ≅ 0.126866897949771... (85 digits) Relation: [ 1, 0, 0, 0, -3860, 0, 0, 0, -666, 0, 0, 0, -20, 0, 0, 0, 1], Time: 5.6 sec Degree: 20. Exact value: α = 31/ 5 − 21/ 4 ≅ 0.0565238246127... (120 digits) Relation: [ 49, -1080, 3960, -3360, 80, -108, -6120, -7440, -80, 0, 54, -1560, 40, 0, 0, -12, -10, 0, 0, 0, 1], Time: 16.8 sec Degree: 20. Exact value: α = 31/ 5 + 21/ 4 ≅ 2.434938054618... (120 digits) Relation: [ 49, -1080, 3960, -3360, 80, -108, -6120, -7440, -80, 0, 54, -1560, 40, 0, 0, -12, -10, 0, 0, 0, 1], Time: 23 sec 1 Computation on a PC with AMD Athlon, 2GHz, 512 MB Ram
  • 130. Xnumbers Tutorial 129 It is interesting to note that the two numbers 31/ 5 ± 21/ 4 gives the same relations. In fact they are both roots of the same 20th degree polynomial. Degree: 25. Exact value: α = 31/ 5 + 21/ 5 ≅ 2.39442929461255233... (170 digits) Relation: [-3125, 0, 0, 0, 0, 21875, 0, 0, 0, 0, -57500, 0, 0, 0, 0, -3500, 0, 0, 0, 0, -25, 0, 0, 0, 0, 1], Time: 81.7 sec Degree: 25. Exact value: α = 31/ 5 − 21/ 5 ≅ 0.0970325846184823... (170 digits) Relation: [ 1, 0, 0, 0, 0, -116255, 0, 0, 0, 0, -11240, 0, 0, 0, 0, -3760, 0, 0, 0, 0, 5, 0, 0, 0, 0, -1], Time: 68.7 sec Degree: 28. Exact value: α = 31/ 7 − 21/ 4 ≅ -0.01927630224403418... (220 digits) Relation: [ -47, -2688, -13104, -7560, 448, -79296, 136584, -108, -672, -184128, -25956, 0, 560, -74592, 54, 0, -280, -4872, 0, 0, 84, -12, 0, 0, -14, 0, 0, 0, 1], Time: 126 sec Degree: 30. Exact value: α = 31/ 6 − 21/ 5 ≅ 0.0522386001789677198... (250 digits) Relation: [ 179, -4860, 30780, -65520, 30240, -192, -405, -98820, -538380, -149520, -240, 0, 270, -84240, 90360, -160, 0, 0, -90, -7380, -60, 0, 0, 0, 15, -12, 0, 0, 0, 0, -1], Time: 218 sec
  • 131. Xnumbers Tutorial 130 Macro Integer Relation Finder The macro Integer Relation Finder, from the menu "Numbers", searches for two type of integer relations: Polynomial relation Input: a real number x, and the n-th degree n n xaxaxaxaa ...3 3 2 210 ++++ General relation Input: a vector x of n-th dimension nn xaxaxaxa ...332211 +++ Using this macro is very simple: select the cell containing the number x or the range of the column-vector x and start the macro. The macro assumes a simple layout in order to speed-up the input operation. The input layout is different for the two type of relation Polynomial relation input layout The cell B2 contains the multiprecision number x of 30 digits and just below, the cell B3 contains the degree n. Select the cells B2 and start the macro Multiple searching for one x and several degree The cell B2 contains the multiprecision number x of 30 digits and just the row below, the range B3:D3 contains the degree n = 5, 6, 7. Select the cells B2 and start the macro. For the same number x, the macro will search for integer polynomial with degree 5, 6 and 7 respectively Multiple searching for several x and several degree The range B2:B4 contains the multiprecision numbers x1, x2 and x3 and just the row below, the range B5:E5, contains the degree n = 5, 6, 7 an 8. Select the range B2:B4 and start the macro. For the each number x, the macro will search for 4 integer polynomial with degree 5, 6, 7 and 8, respectively General relation input layout The range B2:B6 contains the vector x . Select the range B2:B6 and start the macro. The dimension of he relation will be 5, just the same of the vector dimension Multiple searching for several vectors is possible. Simple select as many adjacent vectors as we need. Note that each vector can have different dimension. In this case, select the rectangular range containing all the vectors.
  • 132. Xnumbers Tutorial 131 Output results The macro writes the result in the worksheet are beside the input data using a simple standard layout almost similar for polynomial and general relation. Let' s see with an example Example 1. We want to search for a smallest integer polynomial having the approximate root: x ≅ 2.99197185746375045829465694878... (30 digits) We restrict our searching to the polynomials from 3 to 8 degree Insert the input data x and the degree 3, 4, 5, 6 and 7 as in the sheet at the left. Select the cell B1 and start the macro The input fields are correctly filled. Press "start". In few seconds the macro finds the polynomials adding same useful information. Fortunately it have found 2 "good" polynomials, in this case The coefficents of the polynomials are listed in the last section of the report, but just above, the macro write the information returned by the PSLQ routine Digits: the precision of the calculus Degree: degree of the polynomial; degree +1 is the dimension of the relation RC: return code: 0 =OK, 1 =precision exhausted, 2 = iteration overflow, 3= overflow error This code states the "goodness" of the relation found Residue: relative residue of the relation, computed as ε / a , where ∑= ii xaε , |)max(| ii xaa = Bound: this number M states that there can exist no relation vector whose Euclidean norm is less than M. This is a very important result provided by PSLQ algorithm. Together with RC helps us to analyze the goodness of the relation found. Iter: number of iterations needed for reaching the result Time: elaboration time in seconds Example 2. We have 3 real numbers x1, x2, x3 computed with 15 precision digits. x1 = 35.812551166675 , x2 = 8.554440155849 , x3 = 4.186428394404 We want to investigate if they could be express by the following simbolic formula       +++= e aa eaa a x 54 32 1 1 π π ⇔ 054 321 =++++− e aa eaaxa π π where [a1, a2, a3, a4, a5] all integer. For finding this integer relation of dimension n = 5 (that it is compatible with the precision of the numbers that we have) we compute the following vectors [ ]11 ,,,, −− eex ππ changing the x value with x1, x2, x3 respectively
  • 133. Xnumbers Tutorial 132 Arrange the 3 vectors as in the sheet at the left. Select the range B2:D6 and start the macro. The input fields are correctly filled and the option button "General" is selected. Press "start". In very few seconds the macro succesfully finds integer relations for the first two numbers, while, on the contrary, the third numbers has not a small simple relation. Note how large are the coefficents of the third column. Note also that its bound M = 3.4E+9 its very high; it assures that no integer relation exists with norm ||a|| < 3.4E+9
  • 134. Xnumbers Tutorial 133 Linear Algebra Functions Matrix Addition xMatAdd(mat1, mat2, [DgtMax]) Performs the addition of two matrices. mat1 and mat2 are (n x m) arrays       +      =      mnn n mnn n mnn n bb bb aa aa cc cc 1 111 1 111 1 111 .. .. .. .. .. .. Matrix Subtraction xMatSub(mat1, mat2, [DgtMax]) Performs the subtraction of two matrices. mat1 and mat2 are (n x m) arrays       −      =      mnn n mnn n mnn n bb bb aa aa cc cc 1 111 1 111 1 111 .. .. .. .. .. .. Matrix Multiplication xMatMult(mat1, mat2, [DgtMax]) Performs the multiplication of two matrices. mat1 (n x p) and mat2 (p x m) are arrays           ⋅      =      mpp m m pnnn p mnn n aa aa aa aaa aaa cc cc 1 212 111 21 11211 1 111 .. .. .. .. .. .. .. Matrix Inverse xMatInv(A, [DgtMax]) Returns the inverse of a (n x n) square matrix It returns "?" for singular matrix. This function uses the Gauss-Jordan diagonalization algorithm with partial pivoting method. Matrix Determinant xMatDet(A, [DgtMax]) Returns the determinant of a square matrix. It returns "?" for singular matrix.
  • 135. Xnumbers Tutorial 134 Matrix Modulus xMatAbs(A, [DgtMax]) Returns the absolute value of a matrix or vector. It is also known as "modulus" or "norm" Parameters A may be an (n x m) array or a vector 2 1 1 , )(∑∑ = = = n i m j jiaA Scalar Product xProdScal( v1, v2, [DgtMax]) Returns the scalar product of two vectors i2 1 i121 VVVVc n i ⋅=•= ∑ = Note: The scalar product is zero if, and only if, the vectors are perpendicular 0 2121 VVVV ⊥⇔=• Similarity Transform = xMatBAB(A, B, [DgtMax]) Returns the matrix product: BABC 1− = This operation is also called the "Similarity Transform" of the matrix A by the matrix B. This operation plays a crucial role in the computation of eigenvalues, because it leaves the eigenvalues of the matrix A unchanged. For real, symmetrical matrices, B is orthogonal. The Similarity Transform is also called the "orthogonal transform". A and B must be square matrices. Matrix Power = xMatPow(A, n, [DgtMax]) Returns the integer power of a square matrix. 48476 time ... n n AAAAAB ⋅⋅== Example
  • 136. Xnumbers Tutorial 135 Matrix LU decomposition = xMatLU(A, [Pivot], [DgtMax]) Returns the LU decomposition of a square matrix A It uses Crout's algorithm           ⋅           =⋅= 33 2322 131211 2121 21 00 0 1 01 001 u uu uuu ll lULA Where L is a lower triangular matrix, and U is an upper triangular matrix The parameter Pivot (default=TRUE) activates the partial pivoting. Note: if partial pivot is activated, the LU decomposition refers to a permutation of A If the square matrix has dimensions (n x n), this function returns an (n x 3n) array where the first n columns are the matrix L, the next n columns are the matrix U, and the last n columns are the matrix P. Globally, the output of the Mat_LU function will be: - Columns (1, n) = Matrix L - Columns (n+1, 2n) = Matrix U - Columns (2n+1, 3n) = Matrix P When pivoting is activated the right decomposition formula is: A = P L U , where P is a permutation matrix Note: LU decomposition does not work if the first element of the diagonal of A is zero Example: find the factorization of the following 3x3 matrix A Note: if you want to get only the L and U matrices select a range (3 x 6) before entering this function Matrix LLT decomposition = xMatLL(A, [DgtMax]) This function returns the LLT decomposition of a square matrix A It uses Cholesky's algorithm T T lll ll l lll ll l LLA           ⋅           =⋅= 333231 2221 11 333231 2221 11 0 00 0 00 Where L is a lower triangular matrix The function returns an (n x n) array Note: Cholesky decomposition works only for positive definite matrix
  • 137. Xnumbers Tutorial 136 Example. The diagonal elements of the L matrix are all positive. So the matrix A is positive definite and the decomposition is correct. This function simply stops when detects a negative diagonal element, returning the incomplete decomposition. See this example <Attention !> A diagonal element of the L matrix is negative. So the matrix is not positive definite and the decomposition cannot be completed Vector Product = xProdVect(v1, v2, [DgtMax]) Returns the vector product of two vectors           − − − =           ×           =× 12212211 32113112 31223221 32 22 12 31 21 11 21 vvvv vvvv vvvv v v v v v v VV Note that if V1 and V2 are parallels, the vector product is the null vector. Solve Linear Equation System xSYSLIN( A, B, [DgtMax]) Solves a linear system in multiprecision. The input parameter A is an (n x n) array, B may be a vector (n x 1) or an (n x m) array. It returns a vector (n x 1) or an (n x m) array depending by the argument B A set of m linear systems in n unknowns looks like this: ][...,][,][ 12111 mbxAbxAbxA =⋅=⋅=⋅ It can be rewritten as:         =           ⋅         ⇒=⋅ mnn m mnn m nnn n bb bb xx xx aa aa 1 111 1 111 1 111 .. .... .. ... .. ....... .. .. .... .. ...][][][ BxA This function uses the Gauss-Jordan diagonalization algorithm with partial pivoting method.
  • 138. Xnumbers Tutorial 137 Example. Find the solution of the following 7x7 linear system A b 462 792 1287 2002 3003 4368 12376 24290 924 1716 3003 5005 8008 12376 31824 62856 1716 3432 6435 11440 19448 31824 75582 149877 3003 6435 12870 24310 43758 75582 167960 333918 5005 11440 24310 48620 92378 167960 352716 702429 8008 19448 43758 92378 184756 352716 705432 1406496 12376 31824 75582 167960 352716 705432 1352078 2697968 The solution is the vector [1, 1, 1, 1, 1, 1, 1]. Solving with standard arithmetic, we have an average accuracy of about 1E-8, while in multiprecision we have an accuracy better than 1E-28 Square Delta Extrapolation ExtDelta2(x) xExtDelta2(x, [DgtMax]) This function returns the Aitken's extrapolation, also known as "Square Delta Extrapolation". The parameter x is a vector of n value (n ≥ 3), in vertical consecutive cells. (n = 3 for the multi- precision function xExtDelta2). This formula can be applied to any generic sequence of values (vector with n>2 ) for accelerating the convergence. ),...,,(),...,,( 2321 2 321 − ∆ → nn vvvvxxxx Note that this algorithm produces a vector with n-2 values. If n = 3, the result is a single value. Taking the difference iii xx −=∆ +1 , the Aitken's extrapolation formula is: ( ) ( )21 2 1 21 2 1 2 −− − −− − +− − −= ∆−∆ ∆ −= iii ii i ii i ii xxx xx xxv This formula can be applied to the second sequence to obtain a new sequence with n-4 values, and so on. The process stops when the last sequence has less than 3 values. Example. we want to find the numeric solution of the equation x = cos(x) We choose the central point method. Starting from x0 = 0 we build the iterations xn+1 = cos(xn)
  • 139. Xnumbers Tutorial 138 As we can see in the following table, the convergence is evident but very slow (after 12 iterations the precision is about 3E-5) . The functions of this worksheet are: The cell B2 contains the starting value x0 The cell B3 , C2, D3 contain the formulas [B3]=C2 [C2]=COS(B2) [D3]=ASS(B2-C2) Selecting the range B3:D3 and dragging down we can easily perform the iterative process as we like. We observe that the convergence is evident but quite slow. As we can see the last 12th value has an error of about 3.5E-3. But if we perform the delta extrapolation of the three last values we get a new value having an accuracy better than 1E-5. Now let's repeat the iterative process using systematically the square delta extrapolation In this process, we have systematically repeated the ∆2 extrapolation every 3 iterations. We have inserted in the cell B5 =ExtDelta2(B2:B4) In the cell B8 =ExtDelta2(B3:B7) In the cell B14 =ExtDelta2(B9:B11) The acceleration is superb!. After only 12 steps, the precision is better than 1E-15. The graph below shows better than many words the acceleration effect 1E-16 1E-14 1E-12 1E-10 1E-08 1E-06 0.0001 0.01 1 0 2 4 6 8 10 12 Error (extrap) Error The Aitken's extrapolation formula works very well with the Gauss-Seidel iterative method, and for accelerating the convergence of many iterative processes.
  • 140. Xnumbers Tutorial 139 Macro for Multiprecision Matrix Operations This application collects a set of useful macros performing multiprecision matrix operations Determinant )det(A Gauss-Jordan algorithm Addition BA + Subtraction BA − Multiplication BA⋅ Scalar multiplication Ak ⋅ Inverse 1− A Gauss-Jordan algorithm Similarity transform BAB 1− Linear System BAX = Gauss-Jordan algorithm Linear System overdetermined. bAx = rows > columns LU decomposition LUA = Crout’s algorithm Cholesky decomposition T LLA = Cholesky’s algorithm Norm A Scalar product BAT ⋅ SVD T VU ⋅Σ⋅ Golub-Reinsch algorithm The use of this macro is quite simple. If the operation requires only one matrix (determinant, inversion, etc.) select the matrix, start the macro and choose the appropriate operation. Other operations (addition, multiplication, etc.) require two matrices. In that case you have also to select the second matrix in the second input-box. The internal calculus is performed in multiprecision. The result is converted in standard precision (15 significant digits max) for more readability, but, if you like, you may also leave it in full multiprecision format. Example: Solve the following linear system Ax = b. Select the matrix A and start the macro Choose the operation “Linear System” and then move into the right field to select the vector b.
  • 141. Xnumbers Tutorial 140 Indicate, if necessary, the upper-left cell of the range where you want to write the result. Then, press OK. The result will be output starting from the output cell I2. Smart Selector The special button near the input field is useful for selecting large matrices .Select the first cell, or an internal cell of the matrix and then press this button. The entire matrix will be selected Elaboration time Multiprecision computation does slow down the computation considerably. It takes much more time then the standard double precision. The time depends on the matrix dimension and on the precision digits. The following graphs show the average time for the inversion and for the multiplication of two dense matrices. 0 20 40 60 80 100 120 140 160 0 25 50 75 100 invers. multip. Time (sec) N x N 30 digits Multiprecision 0 1 2 3 4 5 6 0 50 100 150 200 digits time (sec) 20 x 20 standard precision As we can see, the inversion of a (100 x 100) dense matrix, with 30 precision digits, takes about 150 seconds. Clearly, for this kind of tasks, macros are more suitable than functions.
  • 142. Xnumbers Tutorial 141 Integrals & Series Discrete Fourier Transform =DFT(samples, [central]) =FFT(samples, [central]) Returns the complex matrix of the DFT transform of N samples. This function returns an (N x 2) array. The first column contains the real part; the second column the complex part . The optional parameter "central" (default False) outputs a central transform. If N is an integer power of 2, that is N = 2P , use the fastest FFT. FFT uses the Cooley and Tukey decimation-in-time algorithm. Formulas Given N samples ( f(0), f(1), f(2),....f(N-1) ) of a periodic function f(t) with a normalized sampling rate (T=1), the DFT is: ( ) ( )[ ]∑ − = ⋅−⋅=⋅+= 1 0 /2/2cos)()()()( N n ir NnksiniNnknfkFikFkF ππ The components (Fr , Fi) are called the harmonic spectrum of f(t) By the Fourier series, we can approximate a periodic function f(t): ( ) ( )tksinbtkaatf k K k k ⋅+⋅+≅ ∑ − = ωωcos)( 1 1 0 where the coefficients (ak , bk ) are the components 2Fr and 2Fi of DFT Example: Find the 16-FFT of the following periodic function (T = 1 sec) )3cos(5.0)cos(3)( tttf ωω ++= where T π ω 2 = First of all we have to sample the given function. Setting N = 16 , we have a sampling period of 1,...1,0, 16 1 −=∆⋅=⇒==∆ Nitit N T t i )3cos(5.0)cos(3 iii ttf ωω ++= Appling the FFT function at the samples set ( f0, f1, f2,....f15 ), we get the complex discrete Fourier’s transform
  • 143. Xnumbers Tutorial 142 Note that the FFT returns a (16 x2) matrix. The first column contains the real part of FFT while the second column the imaginary one. The magnitude and phase can be easily obtained with the following formulas ( ) ( )22 imrei FFTFFTA +=       = re im i FFT FFT arctanθ Observe that the first row of the FFT contains the mean of f(t). Note also that the rows from 10 to 16 are the mirror-copy of the previous rows. Rearranging the mirror part we can obtain the central Fourier's spectrum. For example, below there are two representations of the same DFT, obtained setting the "central" parameter "false" and "true", respectively. Bilateral DFT Central DFT Amp 0 0.05 0.1 0.15 0.2 1 4 7 10 13 16 19 22 25 28 31 Amp 0 0.05 0.1 0.15 0.2 1 4 7 10 13 16 19 22 25 28 31
  • 144. Xnumbers Tutorial 143 Discrete Fourier Inverse Transform =DFT_INV(samples, [central]) =FFT_INV(samples, [central]) Returns the inverse of the DFT transform of N complex samples. This function returns an (N x 2) array containing the samples of the function f(t). The optional parameter "central" (default False) outputs a central transform. If N is an integer power of 2, thus N=2P , use the fastest FFT_INV function FFT_INV uses the Cooley and Tukey decimation-in-time algorithm. Formulas [ ] ( ) ( )[ ]∑ − = ⋅+⋅⋅+= 1 0 /2/2cos)()()( N k ir NnksiniNnkkFikFnf ππ Where the components (Fr , Fi) are the harmonic spectrum of f(t) Example: Find the inverse transform of the FFT computed in the previous example As we can see, the first column of FFT_INV returns the samples of f(t) that have originated the FFT itself
  • 145. Xnumbers Tutorial 144 Discrete Fourier Spectrum =DFSP(samples, [dB], [Angle]) This function returns the harmonic spectrum of a samples set The parameter "samples" is a vector of N equidistant samples The optional parameter "dB" (default FALSE) sets the amplitude conversion in decibel The optional parameter "Angle" (default “RAD”) sets the angle unit (RAD, GRAD, DEG) The function returns an (N x 2) array, containing the amplitude and phase. The spectrum (An, θn) is computed for real positive frequencys, being ( )∑ ++≅ nn tnAftf θωsin)0()( Example: Find the harmonic spectrum of the following 32 samples Amplitude 0 0.2 0.4 0.6 0.8 1 1.2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Phase -100 -50 0 50 100 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Inverse Discrete Fourier Spectrum =DFSP_INV(spectrum, [dB], [Angle]) This function builds the temporal sequence from its real spectrum (amplitude, phase) ( ) )(, inn tfA ⇒θ The parameter "spectrum" is an (M x 2) array. Each row contains a harmonic. The first column contains the amplitude and the second column the phase The optional parameter "dB" (default FALSE) sets the output in decibel The optional parameter "Angle" (default “RAD”) sets the angle unit (RAD, GRAD, DEG) The function returns the vector (N x 1) where N = 2M
  • 146. Xnumbers Tutorial 145 2D Discrete Fourier Transform =FFT2D (samples) This function performs the 2D-FFT of a bidimensional data set (xi, yi). The parameter "samples" is an (N x M) array where N and M are integer powers of 2 (4, 8, 16, 32, 64…). The function returns an (2N x M) array. The first N rows contain the real part, the last N rows contain the imaginary part. Note: This function requires a large amount of space and effort. Usually it can works with matrices up to (64 x 64). Example: Analyze the harmonic component of the following (8 x 8) table The 2D-FFT can be computed in a very straight way. Simply select a 16 x 8 array and insert the FFT2D where the input parameter is the given matrix (range B10:I17). We can easily extract the harmonic components: 1)0,0( =H 05.0)0,1( =H 1.0)1,0( =H jH 354.0354.0)1,1( += jH 25.0)2,2( = If we compute the inverse transform DFT2D_INV(“L10:S25”) we will obtain again the given starting matrix. 2D Inverse Discrete Fourier Transform =FFT2D_INV (samples) This function FFT2D_INV performs the inverse of the FFT2D. It accepts in input an (2N x M) array having the real part in the first N rows and the imaginary part in the last N rows. It returns an (N x N) array
  • 147. Xnumbers Tutorial 146 Macro DFT (Discrete Fourier Transform) This macro performs: • the DFT of a data set of N samples • the DFT-Inverse of a data set of N complex samples • the 2D-DFT of a matrix of N x M samples • the 2D-DFT-Inverse of a two matrices of N x M samples DFT It works for any number N. If N is a powers of 2 (8, 16, 32, 64, etc.) the macro uses the faster FFT algorithm and the elaboration is more efficient. Its use is quite simple. Select the vector of samples and then start the macro The column “x” is not strictly necessary. If present, the macro uses it to calculate the sampling parameters (see the top-right box). For long input vector, you can select only the first cell C9 and start the macro. The entire input data set (32 x 1) will be automatically selected. The macro writes the results in the following way f = frequency sample Hre = Real part of DFT transform Him = Imaginary part of DFT transform Amp = Amplitude (if “polar” is checked) Phase = Phase (if “polar” is checked) The amplituted can be converted in dB. That is: Amp dB = 20 Log(Amp)), or also in dBm, the power ratio relative to 1 mW (a sine wave of 1 Vpeak , with 50 ohms load, is equivalent 10 dBm). The option check box "central" specifies if the DFT output is bi-lateral or central. See the following examples
  • 148. Xnumbers Tutorial 147 Example of a Fourier transform of pulse. Period = 1 sec Duty cicle = 5.5% Amplitude = 1 Sampling time = 1/256 f(t) 0 0.2 0.4 0.6 0.8 1 1.2 -0.5 -0.3 -0.1 0.1 0.3 0.5 Amplitude of the central DFT (-100 < f < 100) Amp(f) 0 0.01 0.02 0.03 0.04 0.05 0.06 -100 -50 0 50 100 Amplitude of the bi-lateral DFT (0 < f < 255) Amp 0 0.01 0.02 0.03 0.04 0.05 0.06 0 50 100 150 200 250 Amplitude in dB of the central DFT (-100 < f < 100) Amp (dB) -60 -55 -50 -45 -40 -35 -30 -25 -100 -50 0 50 100
  • 149. Xnumbers Tutorial 148 Operation DFT-inverse In this case you have to select two columns: the real and imaginary part of the DFT (Hre, Him). Then start the macro as usually. If the DFT is in polar form (Amplitude, Phase), you have to check the “polar” option box and choose consequently the appropriate units: dB and angle Operation 2D-DFT Dimension N and M must be integer power of 2 In this case you have to select a matrix of N x M values (do not select the axes-scales) and start the macro as usually. If you want the DFT in polar form (Amplitude, Phase), you have to check the “polar” option and choose consequently the appropriate units: dB and angle The macro generates two matrices containing the real and imaginary parts of the 2D-DFT Operation 2D-DFT inverse In this case you have to select a matrix of 2N x M values (do not select the axes values) containing both real and imaginary part and start the macro as usually. If the DFT is in polar form (Amplitude, Phase), you have to check the “polar” option and choose consequently the appropriate units: dB and angle
  • 150. Xnumbers Tutorial 149 Macro Sampler This is a simple but very useful macro for function sampling It can generate samples of multivariate functions such as: f (x) , f (x1, x2) .... f (x1, ... xm) The samples can be arranged in a list or in a table (only for 2 variables). Examples of lists and tables generated by this macro are shown in the following sheet The tables at the top are the skeletons to generate the samples-list or the samples-table just below. The skeleton contains the following parameters for the sampler. Start starting point of the variable X0 Samples number of samples to generate: N Period length of the sampling: P Step length between two consecutive point H = X1 – X0 Cyclic True or False (default), specifies if the function is periodic with period P. The difference between a cyclic or no-cyclic function is in the step formula S = P / N for cyclic function S = P / (N-1) for no-cyclic function For example, the sampling with N = 5, X0 = 0 and P = 2, needs a step H = 0.5 The first point and the last point, in this case, are always taken. But, for a periodic function, the same sampling needs a step of H = 0.4 Practically, the last point X = 2, in this case, is discharged, because is f(0) = f(2). Usually periodic functions require to set Cyclic = “True” for the FT analysis The skeleton can be drawn by hand or automatically. In this case you have only to give the number of variables that you want. The check-box “Function seed” tells the macro to created also the cell in which you can insert the function to sample
  • 151. Xnumbers Tutorial 150 A simple skeleton for one variable is: In the cell C2 you must insert the function f(x) to sample. The reference for the independent variable x is the cell B2. For example, if the function is y = x +2x2 You have to insert the formula = B2+2*B2^2 in the cell C2 Parameters N (Samples), P (Period), H (Step) are not all independent. Only two parameters can be freely chosen. The macro chooses the first two parameters found from top to bottom The remain parameter is obtained by the step-formula Synthetically you can have one of the following three cases Given parameters Calculated parameter Samples, Period (N, P) Step (H) Samples, Step (N, H) Period (P) Period, Step (P, H) Samples (N) Look at the following three examples below for better explanation. The given parameters are in blue while the calculated parameter is in red. After you have set and filled the skeleton, select it and start the sampler macro again (remember that range must always have 6 rows, including the header) The macro show the following window The check-box “Add formula” tells to the macro to leave the formula in the sample set, otherwise it contains only the values. Formula can be added only for a monovariable list or for a table.
  • 152. Xnumbers Tutorial 151 Integral function =IntegrData(xi, yi, [IntType]) Computes numerically the integral functions F(xi) of a given dataset (xi, yi) ∫= ix ii dxyxF 0 )( The sampling interval must be constant The IntType parameter (default = 2) sets the integration formulas 1, 2, 3 Case 1 - 2 points integration formula of 1st degree symmetric 2/)( 2112 ffhI += 12/)2(3 fhE ≈ Case 2 - 4 points integration formulas of 3rd degree symmetric 24/)1313( 432123 ffffhI −++−= )4(5 720/11 fhE ⋅≈ Left side 24/)5199( 432112 ffffhI +−+= Right side 24/)9195( 432134 ffffhI ++−= Case 3 - 6 points integration formulas of 5th degree symmetric 1440/)1193)(8029311( 65432134 ffffffhI +−++−= )6(7 60480/191 fhE ⋅≈ Left side 1440/)271734827981427475( 65432112 ffffffhI +−+−+= 1440/)1177258102263727( 65432123 ffffffhI −+−++−= Right side 1440/)2763710222587711( 65432145 ffffffhI −++−+−= 1440/)475142779848217327( 65432156 ffffffhI ++−+−= Depending on the case, the algorithm uses these formulas at the beginning, at the center and the end of the integration interval. 2 points formula f1 f2 x h 4 points formulas f1 f2 f3 f4 x h f1 f2 f3 f4 x h f1 f2 f3 f4 h
  • 153. Xnumbers Tutorial 152 6 points formulas f1 f2 f3 f4 x h f5 f6 f1 f2 f3 f4 x h f5 f6 x f1 f2 f3 f4 h f5 f6 f1 f2 f3 f4 x h f5 f6 f1 f2 f3 f4 x h f5 f6 Example. Compute the integral function of the following tabulated function y(xi) Using the integration formulas 1, 2 and 3, we have, of course, different precision. The following graph shows the error behaviour of the three schemas. The average errors are about 0.05 for the 1st degree formula, 5E-5 for the 3rd degree and 4E-6 for the 5th degree. Clearly, for smooth, regular functions, the highest degree formulas reach the highest precision but we have to consider that they are also more expensive 1.E-06 1.E-05 1.E-04 1.E-03 1.E-02 1.E-01 1.E+00 0 1 2 3 4 5 6 err1 err2 err3 Usually the 3rd degree formulas are the best compromise between cost and precision Avoid discontinues. But, generally, do highest integration degree formulas always give the best precision? Not always. When the function to integrate shows jumps or direction discontinuities, the better choice is the linear formula. See the following example. Given the following functions |2|2/)|4(|)( −−+−= xxxxy its exact integral function is [ ] 4/8|2|)2(2|4|)4()()( 2 0 ++−−−−−== ∫ xxxxxdxxyxF x
  • 154. Xnumbers Tutorial 153 Sampling the function y(x) and F(x) with h = 0.25 for 0 ≤ x ≤ 6, we have the following data sets (xi, y(xi)) and (xi, F(xi)) 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0 1 2 3 4 5 6 y ∫ y dx As we can see, the function y(x), always linear, has no derivative at the points x = 2, and x = 4. If we integrate the data set (xi, yi) with the 1st degree formula (IntType = 1) the result coincides with the exact solution (error = 0). But if we try with the other higher formulas the average error is, surprisingly, about 0.005 Integral function of a symbolic formula When we have the function f(x) written in a symbolic formula we can obtain the plot of its integral function in two ways. • Sampling the given function f(xi) for xi = x0 + i⋅h with a suitable step h • Solving the associated ODE equation • Integrating f(x) from x0 to xi for xi = x0 + i⋅h with a suitable step h The first method is simple and it is already shown in the previous chapter; the second way need some more explanation. The function y(x) that we want to plot is defined as ∫= x x dxxfxy 0 )()( Taking the derivatives of both sides and remebering Leibniz's rule, we have )()(' xfxy = , 0)( 0 =xy As we can see, the computing of any integral function is equivalent of solving an ODE. Therefore we can use any method that we have used for solving ODE: Runge-Kutta, Predictor- Corrector, Taylor, etc. (see chap. ODE for further details) The third method, less efficient then the others, may be successfully used when the function f(x) or its derivatives f'(x) has some singularities in the integration range. The following example explain better the concept. Assume to have to plot the integral function of the following function ∫= x dxxxy 0 2 )ln()( We note that,. −∞= → )ln(lim 2 0 x x Therefore, x = 0 is a singular point. From theory we know that the integral exists, thus we can project to tabulate the given integral for several points xi = i⋅h with a suitable step h, for example h = 0.1 For this scope we may use the function integr_de or Integr that are more suitable for such singularities
  • 155. Xnumbers Tutorial 154 Insert the function definition in cell B1 Ln(x^2) Build a sequence x = 0, 0.1, 0.guatda.com/cmx.p2...4 in the range A4:A44 Insert the function integr_de in cell B5 and the function = LN(A5^2) in cell C5 and drag down the range B5:C5. Note that we cannot complete the first cell C4 because of #NUM! error. Now, plotting the the range A5:C44, we have the following graph -4 -3 -2 -1 0 1 2 3 4 0 0.5 1 1.5 2 2.5 3 3.5 4 ∫f(x) f(x) Comparing with the exact solution xxxxy 2)ln()( 2 −= we observe a precision better then 1E-15. From the graph we see that the integral function y(x) has a zero between 2.5 and 3 From the table we may take a more precise bracketing of the zero 2.7 < xz < 2.8 Zeros of integral function We may obtain a more close solution of the integral equation using the Newton-Raphson iterative algorithm 0)()( 0 == ∫ x x dttfxy )( )( 1 n n nn xf xy xx −=+ In the cell B1 insert the definition of the function f(x) Ln(x^2) Starting from x0 = 2.7 we calculate the integral from 0 to 2.7 in the cell C5, The derivative is the function f(x) itself, calculated in the cell B5. The new value x1 is calculated in the cell A6 Iterating the process, the solution happears in the last cells of the column A As we can see, after few iteration the zero converges to the exact solution x = e with a precision better then 1E-15
  • 156. Xnumbers Tutorial 155 Function Integration (Romberg method) =Integr_ro(Funct, a, b, [Param], [rank], [ErrMax]) This function computes the numeric integral of a function f(x) by the Romberg method. ∫= b a xfI )( The parameter "Funct" is a math expression string in the variable x, such as: "x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. . Remember the quote " " for passing a string to an Excel function. "Funct" may be also a cell containing a string formula "Param" contains label and values for parameters substitution (if there are) "Rank", from 1 to 16 (default), sets the maximum integration rank. "ErrMax" (default 1E-15) , sets the maximum relative error. For further details about writing a math string see Math formula string The algorithm starts with rank = 1 and incrementing the rank until it detects a stop condition. |R(p, p) - R(p, p-1)| < 10^-15 absolute error detect or (|R(p, p) - R(p, p-1)|) / |R(p, p)| < 10^-15 if |R(p, p)| >> 1 relative error detect or rank = 16 Example. Compute the integral of "x*cos(x)" for 0 ≤ x ≤ 0.4 Integr_ro("x*cos(x)", 0, 0.4) = 0.0768283309263453 This function can also display the number of sub-intervals and the estimate error. To see these values simply select three adjacent cells and give the CTRL+SHIFT+ENTER key sequence. This result is reached with rank = 4 , s = 16 sub-intervals, and an estimate error of about E = 3.75E-16 The function accepts also external parameters. Remember only to include the label in the parameter selection.
  • 157. Xnumbers Tutorial 156 Function Integration (Double Exponential method) = Integr_de(funct, a, b, [Param]) This function1 computes the numeric integral of a function f(x) by the Double Exponential method. This is specially suitable for improper integrals and infinite, not oscillating integrals. ∫= b a dxxfI )( ∫ +∞ = a dxxfI )( ∫ +∞ ∞− = dxxfI )( The parameter "funct" is a math expression string in the variable x, such as: "x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. . Remember the quote " " for passing a string to an Excel function. "funct" may be also a cell containing a string formula. The limits "a" and "b" can also be infinite. In this case insert the string "inf" "Param" contains labels and values for parameters substitution (if there are) For further details about writing a math string see Math formula string The Double Exponential method is a fairly good numerical integration technique of high efficiency, suitable for integrating improper integrals, infinite integrals and "stiff" integrals having discontinue derivative functions. This ingenious scheme, was introduced first by Takahasi and Mori [1974] For finite integral, the formula, also called "tanh-sinh transformation", is the following ∫∫ +∞ ∞− ⋅= dtthtxfdxxf b a )())(()( where: ( ))sinh(tanh 22 )( t abab tx − + + = ( ))sinh(cosh )cosh( 2 )( 2 t tab th − = Example 4guatda.com/cmx.p996...0.47442115)1( 1 0 3.05.0 =−∫ dxxx The above integral is very difficult to compute because the derivative is discontinue at 0 and 1 The Romberg method would require more than 32.000 points to reach an accuracy of 1E-7. On the contrary, this function requires less then 100 points for reaching the high accuracy of 1E-14 This function can also evaluate infinite and/or semi-infinite integral. 1 This function uses the double exponential quadrature derived from the original FORTRAN subroutine INTDE of the DE-Quadrature (Numerical Automatic Integrator) Package , by Takuya OOURA, Copyright(C) 1996
  • 158. Xnumbers Tutorial 157 Example ∫ ∞ − 0 dxx n As known, the integral exist if n > 1 and its value is I = 1/(n-1). The parameter "n" is called "order of convergence". For n = 1.1 we get I = 10 Note that we need to pass the parameter with its label "n". (Param = "D1:D2") This function cannot give reliable results if n is too close to 1. The minimum value is about n = 1.03. For lower values the function returns "?". The DE integration works very well for finite improper integral Example ( ) ( ) 2lnlimln 1 2 0 1 0 2 −== ∫∫ + → a a dxxdxx Note that the function f(x) is not defined for x = 0 Example. Another difficult improper integral 9/4)ln( 1 0 −=⋅∫ dxxx
  • 159. Xnumbers Tutorial 158 Function Integration (mixed method) = Integr(Funct, a, b, [Param]) This function computes the numeric integral of a function f(x) over a finite or infinite interval ∫ b a dxxf )( ∫ ∞+ )( a dxxf ∫∞− b dxxf )( ∫ ∞+ ∞− )( dxxf This function can compute definite integrals, improper integrals and piece-wise functions integrals. The parameter "funct" is a math expression string in the variable x, such as: "x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. . Remember the quote " " for passing a string to an Excel function. "funct" may be also a cell containing a string formula The limits "a" and "b" can also be infinite. In this case, insert the string "inf" "Param" contains labels and values for parameters substitution (if there are) This function uses two quadrature algorithms 1) The double exponential method1 (see function integr_de ) 2) The adaptive Newton-Cotes schema (Bode's formula) (see macro Integral_Inf ) If the first method fails, the function switches on the second method Oscillating functions, need specific algorithms. See Integration of oscillating functions (Filon formulas) and Fourier's sine-cosine transform Example. Compute the integral of x⋅cos(x) for 0 ≤ x ≤ 0.4 In the given interval the function is continuous, so its definite integral exists. This result is reached with rank = 4, s = 16 sub-intervals, and an estimate error of about 3.7E-16. This function returns the integral and can also displays the number of sub-intervals and the estimate error. To see these values simply select three adjacent cells and give the CTRL+SHIFT+ENTER keys sequence. Note that the function Integr is surrounded by { } . This means that it returns an array The function Integr can accept also parameters in the math expression string. See the example below. 1 This function uses the double exponential quadrature derived from the original FORTRAN subroutines INTDE and INTDEI of the DE-Quadrature (Numerical Automatic Integrator) Package , by Takuya OOURA, Copyright(C) 1996
  • 160. Xnumbers Tutorial 159 Note that we must include the parameter labels in order to distinguish the parameters "k", "w", and "q". The integration variable is always "x" Beware of the poles Before attempting to evaluate a definite integral, we must always check if the integral exists. The function does not perform this check and the result may be wrong. In other words, we have to make a short investigation about the function that we want to integrate. Let's see the following example. Assume the following integrals to have to compute dx x dx x 12 2 , 12 2 2 1 0 2 2/1 0 −− ∫∫ We show that the first integral exists while, on the contrary, the second does not exist For ...707.02/2 ≅=px the function has a pole; that is: +∞=      − −∞=      − +− →→ 12 2 lim, 12 2 lim 22 xx pp xxxx The first integral exists because its interval [0, 0.5] does not contain the pole and the function is continuous in this interval. We can compute its exact value:         + − = −∫ |12| |12| log 2 2 12 2 2 x x dx x ⇒ ( )12log2 12 2 2/1 0 2 −= −∫ dx x In this situation the function Integr returns the correct numeric result with an excellent accuracy, better than 1E-14. For this result the integration algorithm needs 128 sub-intervals The interval of the second integral contains the pole, so we have to perform some more investigation. Let's begin to examine how the integral function approaches the pole xp taking separately the limit from the right and from the left −∞=         + − −∞=         + − +− →→ 12 12 loglim, 12 12 loglim x x x x pp xxxx As we can see the both limits are infinite, so the second integral does not exists Note that if we apply directly the fundamental integral theorem we would a wrong result: ( )12ln2 |12| |12| log 2 2 12 2 1 0 !1 0 2 −=                 + − = −∫ x x dx x wrong Let's see how the function integr works in this case.
  • 161. Xnumbers Tutorial 160 The numeric result is, of course, completely wrong because the given integral goes to the infinity. But, even in this situation, this function gives us an alert: the sub- intervals have reached the maximum limit of 65536 (216 ) So the result accuracy must be regarded with a reasonable doubt. Complex Function Integration (Romberg method) =cplxintegr(Funct, a, b) This function returns the numeric integral of a complex function f(z) by the Romberg method. dzzfaFbF b a )()()( ∫=− The integration function "Funct" must be a string in the variable z and can be defined mixing all arithmetic operators, common elementary functions and complex numbers like: "z*cos(z)", "1+(1+i)*z+z^2", "exp(-z^2)", ecc.. . Remember the quote " " for passing a string to an Excel function. Parameters "a" and b” can be real or complex. Complex values are inserted as arrays of two cells. Example: Evaluate the following integral dz z i i i 1 1 1 2∫ + − + Because the integration function is analytic, then the given integral is independent form the integration path. Therefore it can be calculated by the function cplxintegr The exact result is the complex number (−1+i) Note that, thanks to the excellent accuracy, the result is shown exactly even if it is intrinsically approximated
  • 162. Xnumbers Tutorial 161 Data Integration (Newton-Cotes) =IntegrDataC(x,y, [Degree]) This function returns the integral of a discrete set of points (xi, yi) using the Newton-Cotes formulas. The points may be equidistant or random. The parameter "Degree", from 1(default) to 10, set the degree of the Newton-Cotes formula written as: ∑∫ = + ⋅⋅= n j jj hnx x bf k h dxxf 0 0 0 )( where fi = yi , "h" is the integration step, "n" is the degree. The coefficients (bj , K) can be extracted from the following table: Degree 1 2 3 4 5 6 7 8 9 10 K 2 3 8 45 288 140 17280 14175 89600 299376 b0 1 1 3 14 95 41 5257 3956 25713 80335 b1 1 4 9 64 375 216 25039 23552 141669 531500 b2 1 9 24 250 27 9261 -3712 9720 -242625 b3 3 64 250 272 20923 41984 174096 1362000 b4 14 375 27 20923 -18160 52002 -1302750 b5 95 216 9261 41984 52002 2136840 b6 41 25039 -3712 174096 -1302750 b7 5257 23552 9720 1362000 b8 3956 141669 -242625 b9 25713 531500 b10 80335 As we can see, for degree=1, the Newton-Cotes formula coincides with the trapezoidal rule and, for degree = 2, with the popular Cavalieri-Simpson formula. Trapezoid rule Cavalieri-Simpson rule ( )10 1 0 01 2 )( ff h xf xxh x x +≅ −= ∫ ( )210 2 0 02 4 3 )( 2 fff h xf xx h x x ++≅ − = ∫ For degree = 4, the table gives the Bode's rule 4 04 xx h − = ( )00000 1464246414 45 )( 4 0 fffff h dxxf x x ++++≅∫ Using the function IntegrDataC is very easy. Example. Calculate the integral with the Newton-Cotes formulas of degree = 1, 2, 4, 6 for the dataset obtained by sampling the function sin(x)/x with step 0.2 . In a previous example, using the Romberg method we have approximated this integral obtaining Si(1.6) ≅ 1.38918048587044 with an accuracy better than 1E-9 Let's see now how the Newton-Cotes formulas work.
  • 163. Xnumbers Tutorial 162 As we can see, the convergence to the exact result is evident. The most accurate result is reached with the 6th degree Newton-Cotes formula. The IntegrDataC can work with random samples. Example. Given the data table (xi yi) , approximate the integral with the Cavalieri-Simpson formula. x y 1 0 1.1 0.09531018 1.2 0.182321557 1.4 0.336472237 1.8 0.587786665 2.4 0.875468737 3.2 1.16315081 4 1.386294361 5 1.609437912 0 0.4 0.8 1.2 1.6 2 0 1 2 3 4 5 6 Note that the data points are not equidistant. The points have been extracted from the function y = ln(x) . Thus the exact integral is 5*ln(5)-4 ≅ 4.0471896 Data integration for random points. Having a set of not equidistant points (xi, yi), we cannot use directly the Newton-Cotes formulas for fixed step. In that case, IntegrDataC reorganizes the random data samples in equidistant data samples and after that, computes the integral using the standard formulas for fixed step Random Samples Converted to Equidistant Samples { (xi , yi) ; i = 0, 1, ... n } ⇒ { (xi = x0+i h , yi (xi ) ; i = 0, 1, ... m } For computing the values f(x0+i h ) at the equidistant grid points, IntegrDataC uses the Aitken Interpolation algorithm. Aitken interpolation algorithm. Given a set of points: { (xi , yi) i = 0, 1, ... n }
  • 164. Xnumbers Tutorial 163 This method is used to find the interpolation point yp at the value xp . It is efficient as the Newton formula, and it is also very simple to code. For j = 1 To n - 1 For i = j + 1 To n y(i) = y(j) * (x(i) - xp) - y(i) * (x(j) - xp)/ (x(i) - x(j)) Next i Next j yp = yi(n) Function Integration (Newton-Cotes) =Integr_nc(funct, a, b, Intervals, [Degree]) This function returns the numeric integral of a function f(x) using the Newton-Cotes formulas. dxxfaFbF b a )()()( ∫=− The parameter "Funct" is a math expression string in the variable x, such as: "x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. . Remember the quote " " for passing a string to an Excel function. "Funct" may be also a cell containing a string formula The parameters "a" and "b" are the limits of integration interval The parameter "Intervals" sets the number of sub-intervals of the integration interval. The parameter "degree", from 1(default) to 10, set the degree of the Newton-Cotes formula. degree = 1 coincides with the Trapezoidal rule; degree = 2 coincides with the Cavalieri- Simpson formula; degree = 4 with the Bode's rule. Remember that the total knots of the function computation is: knots = Intervals × Degree + 1 Example: Approximate the following integral using 10 sub-intervals and three different methods: trapezoidal, Cavalieri-Simpson, and Bode's rule. ∫ − ⋅ 1 0 dxex x The indefinite integral is known as the closed form: ( ) xx exdxex −− +−=⋅∫ 1 So we can compare the exact result, that is 1 21 − − e ≅ 0.264241117657115356
  • 165. Xnumbers Tutorial 164 Integr_nc("x*exp(-x)",0,1,10,1) = 0.263408098685072 (8.3E-04) 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0 0.2 0.4 0.6 0.8 1 1.2 x f(x) The trapezoidal rule, with 10 sub-intervals, requires 2 knots for each sub-interval for a total of 11 function evaluations (11 knots) The accuracy is better then 1E-3 Integr_nc("x*exp(-x)",0,1,10,2) = 0.264241039074082 (7.8E-08) 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0 0.2 0.4 0.6 0.8 1 1.2 x f(x) The Cavalieri-Simpson rule, with 10 sub- intervals, requires 3 knots for each sub- interval for a total 21 function evaluation (21 knots) The accuracy is better then 1E-7 Integr_nc("x*exp(-x)",0,1,10,4) = 0.264241117655293 (1.8E-12) 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0 0.2 0.4 0.6 0.8 1 1.2 x f(x) The Bode's rule, with 10 sub-intervals, requires 5 knots for each sub- interval for a total of 41 function evaluation (41 knots). The accuracy is better then 2E-12
  • 166. Xnumbers Tutorial 165 Integration: symbolic and numeric approaches The usual approach to the calculation of the definite integral involves two steps: the first is the construction of the symbolic anti-derivative F(x) of f(x) ∫= dxxfxF )()( and the second step is the evaluation of the definite integral applying the fundamental integration theorem. ∫ −= b a aFbFdxxf )()()( This approach can only be adopt for the set of the functions of which we know the anti- derivative in a closed form. For the most f(x), the integral must be approximated either by numerical quadrature or by same kind of series expansion. It is usually accepted that symbolic approaches, when possible, gives more accurate result then the numeric one. This is not always true. Even if the symbolic anti-derivative is known in a closed form, it may often be unsuitable for further numerical evaluation. In particular, we have cases in which such "exact" answers, when numerically evaluated, give less accurate results than numerical quadrature methods1 Let's see. Assume to have the following integral functions ( ) cxdx x x xF += + = ∫ 3 6 2 arctan 1 3 )( We want to calculated the definite integral between a = 2000 and b = 2004 The analytic approach gives: F(b) -F(a) = arctan(b3 ) -arctan(a3 ) In the following worksheet we have compared the evaluations with the exact anti-derivative and the numerical quadrature with the Bode's rule. In the cell C2 we have inserted the anti- derivative function. =ARCTAN(B2^3)-ARCTAN(A2^3) In the cell C2 we have inserted the Bode formula with 20 intervals =Integr_nc(D1, A2, B2, 20, 4) In the cell C5 we have also inserted the reference integral value As we can see, the more accurate result is those obtained with the numerical quadrature; surprisingly, it is more than 200 millions times more accurate than the one of the exact method! It is evident from this example that only the symbolic integration could not resolve efficiently the problem. For numerical integration the quadrature methods are often more efficient and accurate. 1 "Improving Exact Integral from Symbolic Algebra System", R.J. Fateman and W. Kaham, University of California, Berkeley, July 18,2000
  • 167. Xnumbers Tutorial 166 Integration of oscillating functions (Filon formulas) =Integr_fsin(Funct, a, b, k, Intervals) =Integr_fcos(Funct, a, b, k, Intervals) Oscillating functions can reserve several problems for the common polynomial integration formulas. The Filon's formulas are suitable to compute efficiently the following integrals. ∫ b a dxkxxf )cos()( ∫ b a dxkxxf )sin()( for k = 1, 2, 3...N The parameter "Funct" is a math expression string in the variable x, such as: "x*cos(x)", "1+x+x^2", "exp(-x^2)", ecc.. . Remember the quote " " for passing a string to an Excel function. "Funct" may be also a cell containing a string formula The parameters "a" and "b" are the limits of integration interval The parameter "k" is a positive integer The parameter "Intervals" sets the number of sub-intervals of the integration interval. Remember that the total number of the evaluation nodes is: Nodes = Intervals × 2 + 1 To understand the effort in this kind of numerical integration let's see this simple test. Assume we have to evaluate the following integral for several integer values k, with 0 < k < 25 ∫ π 0 4 )cos( dxkxx If we perform the computation with the Cavalieri-Simpson formula (80 nodes) and with the Filon formula (80 nodes), we get the following result 1.E-08 1.E-07 1.E-06 1.E-05 1.E-04 1.E-03 1.E-02 1.E-01 1.E+00 0 5 10 15 20 25 30 k Cavalieri-Simpson Fillon Relative error versus k As we can see, the relative error increases with the number k much more for the Cavalieri-Simpson rule than the Filon formula. For k = 24 the first formula should have 400 nodes at least, for reaching the same accuracy of the Filon formula. Example: evaluate the integral of the following oscillating function ∫ ⋅ + π2 0 2 )8sin( 1 1 x x that can be rearranged as ∫ ⋅ π2 0 )8sin()( xxg where 1 1 )( 2 + = x xg The plot of the integration function and its envelope function g(x) are shown in the following graph
  • 168. Xnumbers Tutorial 167 -1.2 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 1.2 0 1 2 3 4 5 6 7 1/(x^2+1) -1/(x^2+1) 1/(x^2+1)*sin(8*x) Below, a simple arrangement to compute the given integral The approximate error is less then 1E-8, with 300 intervals (default)
  • 169. Xnumbers Tutorial 168 Integration of oscillating functions (Fourier transform) = Fourier_sin(funct, k, [a], [param]) = Fourier_cos(funct, k, [a], [param]) These functions1 perform the numerical integration of oscillating functions over infinite intervals ∫ +∞ ⋅⋅ a dxxkxf )sin()( ∫ +∞ ⋅⋅ a dxxkxf )cos()( If a = 0 (default) , these integrals are called "Fourier's sine-cosine transforms" The parameter "funct" is a math expression defining the function f(x), not oscillating and converging to 0 for x approaching to infinity: "1/x", " 1/(8*x^2)", " exp(-b*x)", ecc.. . Remember the quote " " for passing a string to an Excel function. "Funct" may be also a cell containing a string formula The parameter "k" is a positive number The "Param" contains labels and values for parameters substitution (if there are) These functions return "?" if the integral is not converging or if they cannot compute the integral with sufficient accuracy For finite integration see also Integration of oscillating functions (Filon formulas) Example. Prove that is 2 sin 0 π =∫ ∞+ dx x x The graph of the integration functions is at the right. Numerically specking, this integral is very difficult to calculate for many algorithms. For example, the Bode adaptive quadrature needs more than 10.000 points for getting accuracy of about 1E-4. The Fourier_sin function on the contrary is very efficient for this kind of integral. The integral can be arranged in the following form ∫∫ ∞+∞+ = 00 sin 1sin dxx x dx x x That is the Fourier's sine transform of 1/x 1 These functions use the double exponential quadrature derived from the original FORTRAN subroutine INTDEO of the DE-Quadrature (Numerical Automatic Integrator) Package , by Takuya OOURA, Copyright(C) 1996
  • 170. Xnumbers Tutorial 169 We see that the accuracy is better than 1E-15. Note that the function Fourier_sin automatically multiply the integration function f(x) for the factor sin(k*x), so we have only to pass the f(x) expression. Example. Verify that is 8 4cos 0 π =∫ ∞+ dx x x The graph of the integration functions is Observe that the integration function goes to infinity for x approaching to 0. Numerically specking this function is "terrible". The integral can be arranged in the following form ∫∫ ∞+∞+ = 00 4cos 14cos dxx x dx x x That is the Fourier's cosine transform of 1/x^0.5 The accuracy is better than 1E-15 Infinite Integration of oscillating functions Generally, the infinite integration of real functions having a certain type of infinite oscillating tails may give some problem even to the most efficient quadrature algorithms. These problems can be avoided adopting specific integration tricks Let's see some of them. Example. Assume to calculate the following integral ( ) ( ) dx x xx 2coscos 0 ∫ +∞ − The integration function coverges to zero but it contains two oscillating terms. So we cannot use directly the integr or integr_de function because they returns "?" For solving we can use the Fourier's cosine trasform, separating each oscillating term. The given integral can be re-arranged in the following way ( ) ( ) ( ) ( ) ( ) ( ) dx x x dx x x dx x xx dx x xx 2coscos2coscos2coscos 11 1 00 ∫∫∫∫ ∞+∞+∞+ −++ − = − Note that the last two integrals cannot have the lower limit 0 because they do not converge for x approaching to 0.
  • 171. Xnumbers Tutorial 170 The first integral can be evaluted with the function integr and the two last integrals are evaluated with the function Fourier_cos with a = 1. Let's see the following spreadsheet arrangement Compare the accuracy with the exact result I = Ln(2) Example. Calculate the following integral dx x x)(sin 0 2 4 ∫ ∞+ Remembering that is 8 )4cos( 2 )2cos( 8 3 )(sin4 xx x +−= The given integral can be arranged as ( ) ( )dx x x dx x x dx x dx x x dx x x ∫∫∫∫∫ +∞+∞+∞∞+ +−++= 1 2 1 2 1 2 1 0 2 4 0 2 4 8 4cos 2 2cos 8 3sinsin The first and second integral can be evaluted with the function integr and the two last integrals are evaluated with the function Fourier_cos with a = 1. Compare the accuracy with the exact result I = π /2
  • 172. Xnumbers Tutorial 171 Example. Calculate the following integral ( )dxxsin 0 2 ∫ ∞+ This function oscillates very badly. Note that the function does not converge to zero, oscillating continuously from 1 and −1, but we can show that its integral is finite. Let's perform the substitution dt t dxtxtx 2 12 =⇒=⇒= So, the given integral becomes ( ) ( ) dt t t dxx 2 sin sin 00 2 ∫∫ ∞+∞+ = That can be easily computed by the Fourier's cosine transform Compare the accuracy with the exact result I = (π /8)1/2
  • 173. Xnumbers Tutorial 172 Double Integral 2D Integration for Normal Domains Xnumbers contains routines for integrating bivariate functions f(x, y) over a normal domain (normal to the x-axis and/or to the y-axis) or a circular domain. c d h 2(y)h 1(y) x y Domain normal to y-axis a b g 1(x) g 2(x) x y Domain normal to x-axis x2 +y2 ≤ R2 x y Circular Domain For those kinds of 2D-domains the integration formulas can be re-written as the following ∫ ∫∫∫ = b a xg xgD dydxyxfdsyxf X )( )( 2 1 ),(),( ∫ ∫∫∫ = d c yh yhD dxdyyxfdsyxf Y )( )( 2 1 ),(),( ∫ ∫∫∫ = π θρρθρθρ 2 0 0 ))sin(),cos((),( R C ddfdsyxf Note that a normal domain implies that - at least - one axis must have constant limits. Rectangular domains are a sub-case of normal domains in which both axes have constant limits. The macro Integr2D - suitable for integrating smooth functions f(x, y) – and its function version Integr_2D use the same bidimensional Romberg algorithm, but the function is limited to about 65.000 points. Macro for Double Integration Integr2D() This macro performs the numerical integration of a smooth, regular function f(x, y) over a plane normal domain D(x, y) . ∫ ∫ max min max min ),( X X Y Y dydxyxf The integration functions f(x, y) and – eventually – also the bounding limits – Xmin, Xmax, Ymin, Ymax -can be written in symbolic expression. For further details about the math string see Math formula string The integration function can be: • bivariate functions like x^2+y^2-x*y, log(1+x+y), 1/(1+x^2+2*y^2), etc. • constant numbers like 0 , 2, 1.5, 1E-6 , etc. • constant expressions like 1/2, √2+1, sin(0.1), etc.
  • 174. Xnumbers Tutorial 173 Boundary limits can be: • constant numbers like 0 , 2 , 10 , 3.141 , etc. • constant expressions like 1/2 , √2+1, pi, sin(1/2*pi) , exp(1) , etc. • mono-variable functions like x/2 , 3y-10 , x^2+x-1 , etc. A normal domain has, at least, two constant boundary limits. Function and limits can be passed to the macro directly or by reference. That is: you can write directly the symbolic expressions or constants into the input-box of the macro panel or you can pass the cells containing the expressions. This second mode is more easy and straight Let’ see how it works. Approximate the following double integral of the function f(x,y) = ln(1+x+y) in the closed region delimited by the given constrains Integration function )1ln( yx ++ Integration domain D 1 10 2 +≤≤ ≤≤ xyx x The domain D is shown in the above plot. As we can see, it is an x-nomal-domain domain. Verify that the given integral approximates the symbolic expression at the right ∫ ∫∫∫ + ++= 1 0 1 ),( 2 )1ln(),( x xyxD dydxyxdsyxf 8 17 12 3 )2ln(7 4 )3ln(9 −−+ − π The macro assumes as default the following simple arrangement (but, of course, it is not obligatory at all) Select the A2 cell and start the Integr2D macro. As we can see, the entire input boxes are filled with the right references. The output result will start from the A4 cell Optionally we can adjust the Error limit or the Rank. But usually the only thing to do is click on the “run” button Warning: The Rank increase exponentially the computation effort, because points = (2K)^2 "Parameter" input box may be used to pass values of one or more parameters eventually present in the integration function Activate "polar" for switching to the polar coordinate system
  • 175. Xnumbers Tutorial 174 The macro outputs 5 results: 1) Integral 2) Relative error estimation 3) Total points evaluated 4) Elaboration time 5) Error message (if any) Example 2. Approximate the double integral of the function [ ] x exy 852.0148.3 )(218.01 −− ⋅−+ in the closed region delimited by xyx ≤≤≤≤ 0,10 The above expression can be written as "(1+0.218*(y-x))^(-3.148)*exp(-0.852*x)" or alternatively "(1+218/1000*(y-x))^(-3148/1000)*exp(-852/1000*x)" In both cases the result is obtained with high precision Another elegant way to insert constant in the integration function is using parameters Example. Evaluate numerically the following double integral ∫∫ π π 0 0 )cos()sin( dxdymynx for n = 0.75 and m = 0.125 Set two cells, for example, F2 and G2, containing the values of n and m; just above, in the cells F1 and G1, add their name "n" and "m" and pass the entire range F1:G2 as parameters. The result will be
  • 176. Xnumbers Tutorial 175 Compare with the exact result 2562128 3 1 + Macro for Triple Integration Integr3D() 3D Integration in normal domains This macro calculates the triple integral of a function f (x, y, z) in a normal domain or in a spherical domain or in a cylindrical domain. For this kinds of domains the triple integral can be decomposed by the following formulas ∫∫∫ ∫ ∫ ∫= D x x y y z z dxdydzzyxfdxdydzzyxf max min max min max min ),,(),,( Where the functions: • Xmin and Xmax may depend on variables y and z • Ymin and Ymax may depend on variables x and z • Zmin and Zmax may depend on variables x and y The normal domain condition implies that, at least, one boundary must be constant The macro can integrate in spherical coordinates system, using the formulas ϕρ θϕρ θϕρ cos sinsin cossin = = = z y x where θ, from 0 to 2π, is the rotation angle on the xy-plane and φ , from 0 to π, is the colatitude angle The triple integral changes in ρθϕϕρ ρ ρ θ θ ϕ ϕ dddzyxfdxdydzzyxf D sin),,(),,( max min max min max min 2 ∫∫∫ ∫ ∫ ∫= The macro can also integrate in cylindrical coordinates system, using the formulas zz y x = = = θρ θρ sin cos
  • 177. Xnumbers Tutorial 176 where θ, from 0 to 2π, is the rotation angle on the xy-plane The triple integral changes in ∫∫∫ ∫ ∫ ∫= D z z dzddzyxfdxdydzzyxf max min max min max min ),,(),,( ρ ρ θ θ θρρ Functions f(x, y, z) and – eventually – also the boundary functions may be written in symbolic expression.. For further details about the math string see Math formula string Integration function can be: • three-variate functions like x^2+y^2-x*z , log(1+x+y+z) , (x+z)/(1+x^2+y^2) , etc. • Constant numbers like 0 , 2 , 1.5 , 1E-6, etc. • Constant expressions like 1/2 , √2+1 , sin(pi/4) , etc. Boundary limits can be: • Constant numbers like 0 , 2 , 10 , 3.141 , etc. • Constant expressions like 1/2 , √2+1, pi, sin(1/2*pi) , exp(1) , etc. • univariate or bivariate functions like x/2 , 3y-10 , x^2+x-1 , sin(x+z), Ln(x+y) , etc. A normal domain has, at least, two constant boundary limits. The Integration function and the boundary limits can be passed to the macro directly or by reference. That is: we can write directly the symbolic expressions into the input fields, or you can pass the cells that containing the expressions (simpler), or even a mixed mode. Let’s see how it works Example 1. Approximate numerically the following triple integral dxdydzxyz54 1 0 2 0 3 0 ∫∫∫ The integration domain is the parallelepiped of lengths 1, 2, 3 The macro assumes as default the following simple layout (but, of course, it is not obligatory) Now select the cell A2 containing the integration function and start the macro from the Xnumbers toobar Macros > Integral > Triple.
  • 178. Xnumbers Tutorial 177 As we can see, all the input boxes are filled with the right references. The output result will start from the A4 cell Optionally we can adjust the Rank, increasing it, if we want to increase the final accuracy. But usually the only thing to do is click on the “run” button Warning: The Rank increase exponentially the computation effort, because the number of max points is (2*Rank)^3 Parameter input box may be used to pass values of one or more parameters eventually present in the integration function The macro outputs 5 results: 1) Integral 2) Relative error estimation 3) Total points evaluated (function + boundaries) 4) Elaboration time 5) Error message (if any) The results will appear as the following Example 1. Approximate numerically the following triple integral ∫∫∫ + D dxdydzyx )( 1 22 π where D is the domain defined by the following implict relation 1222 ≤++ zyx This domain is a sphere of unitary radius. Therefore is better to pass to the spherical coordinates because the integration domain becomes much simpler, being 10 ≤≤ ρ , πθ 20 ≤≤ , πϕ ≤≤0 The macro automatically performs the spherical transformation; so we have only to set the right boundaries Select the cell A2 and start the macro. Select "Polar" and set the Rank = 25. Press "Run"
  • 179. Xnumbers Tutorial 178 Compare the result with the exact solution 8/15 = 0.53333... Double integration function =Integr_2D (Fxy, Xmin, Xmax, Ymin, Ymax, [Polar],[ErrMax]) This function returns the numeric integral of a smooth regular function f(x, y) over a plane normal domain D(x, y) . ∫ ∫ max min max min ),( X X Y Y dydxyxf The integration functions f(x, y) and – eventually – also the bounding limits – Xmin, Xmax, Ymin, Ymax -can be written in symbolic expression. For further details about the math string see Math formula string The integration function can be: • bivariate functions like x^2+y^2-x*y, log(1+x+y), 1/(1+x^2+2*y^2), etc. • constant numbers like 0 , 2, 1.5, 1E-6 , etc. • constant expressions like 1/2, √2+1, sin(0.1), etc. The boundary limits can be: • constant numbers like 0 , 2 , 10 , 3.141 , etc. • constant expressions like 1/2 , √2+1, pi, sin(1/2*pi) , exp(1) , etc. • monovariable functions like x/2 , 3y-10 , x^2+x-1 , etc. A normal domain has, at least, two constant boundary limits. Example. Approximate the following double integral ∫ ∫ + ++ 1 0 1 2 1)yln(x x x dxdy 8 17 12 3 )2ln(7 4 )3ln(9 −−+ − π The integration domain is shown in the previous example. In order to avoid long elaboration time, the function limits the total evaluation points to about 65.000 (rank = 8). For heavy computations use the macro Integr2D
  • 180. Xnumbers Tutorial 179 Note: this function can also return the relative error, the total of evaluation points and the error message (if any). To see these values simply select a range of two, three or four, adjacent cells (vertical or horizontal) and give the CTRL+SHIFT+ENTER key sequence. Example: Approximate the following integral dxdy yx∫ ∫ ++ 1 0 y/2 22 x e 1 1 The integration domain is represented in the following plot Integration domain D y ex y y ≤≤ ≤≤ 2 10 As we can see the domain is normal to the y-axes The calculus of this double integral can be arranged as the following Sometime, the integral my contain one or more parameters Example ∫∫ ⋅+⋅ 1 0 1 0 )( 22 dxdye ybxa Note that the parameters "a" and "b" must be passed with their labels.
  • 181. Xnumbers Tutorial 180 Example. Compute numerically the following integral dxdyyx D ∫∫ + 22 Where the domain D is the half-circular region showed at the right Set the parameter "Polar" to True. Compare the precision with the exact result
  • 182. Xnumbers Tutorial 181 Infinite integral Integral_Inf() This macro performs the numeric integration of a smooth, regular, not oscillating function f(x) over an unlimited (or very long) interval ∫∫∫ +∞ ∞−∞− +∞ dxxfdxxfdxxf a a )(,)(,)( This macro can use two different methods: • The Bode formula with adaptive step • The double exponential algorithm The Bode formula with 8 steps to calculate the integral and the truncation error. ( )432101 73212327 45 2 fffff h Ih ++++= ( )876542 73212327 45 2 fffff h Ih ++++= 21 hhh III += 63 2hh T II E − ≈ After each step the routine detects the truncation error and recalculates the step in order to keep a constant error (variable step integration method). The double exponential algorithm, also called "tanh-sinh quadrature". first introduced by Takahasi and Mori, is based on the hyperbolic variable transformations. It is more complicated then the polynomial Newton-Cotes schema but, on the other hand, it is much more efficient. Using this macro is very easy. Example: Approximate the given integral ∫ ∞+ − ⋅⋅ 0 2 100 dxex x The integration function is regular over the entire x-axes; the exponential assures the convergence. Therefore the infinite integral exists. Put the symbolic expression “100*x^2*exp(-x)” in any cell that you like (A3 for example), and arrange the worksheet in the following way (but it is not obligatory at all) The word “inf” means – of course –infinity. It is not necessary to specify the sign, because the macro always assumes “b” as +inf , “a” as -inf Now select the cell A3 and run the macro Integral_Inf . The input fields will be automatically filled
  • 183. Xnumbers Tutorial 182 Choose “run” to start the integration routine. The result will be similar to the worksheet below (without formatting) where we have compared the results of both methods As we can see the integral is 100 with an excellent approximation for both methods but the double exponential is more efficient. It required only 199 function evaluations. Sometime we have to calculate the integral over the entire x- axes. Let’ see ∫ ∞+ ∞− ++ ++ dx xx xx 1 32 4 2 Note that, in this case, we have needed more than 10.000 evaluation points for the variable step method but only 344 for the DE method. The superiority is ever so evident? Not always. There are cases in which the adaptive quadrature schema works better. For example when the integration function has a finite discontinuity (jump) inside the integration interval; this usually happens for the piecewise functions. Example, Assume to have to compute the following didactical integral ( )∫ ∞+ − 1 2 1 5.1sgn dx x x The integration function is shown in the following graph In this case is easy to calculate the integral simply separating the given interval [1, +∞] in two sub-intervals: [1, 1.5]∪ [1.5, +∞]. Calculating each integral and summing we get the exact result I = −1/3. But we want here to investigate how the two methods works in this situation
  • 184. Xnumbers Tutorial 183 As we can see the variable step method has find the result with high precision using about 7200 steps. The double exponential algorithm fails the convergence We have to put in evidence that using quadrature algorithms in a “blind” way, may lead to wrong result. We should always study the integration function to discover singularities, discontinuities, convergence rate, etc. If the integration function is “sufficiently” smooth, then the numeric integration can give good approximate results. This routine can also be used over a closed long interval, when other algorithms would take too long computational time. Double Data integration =IntegrData2D(Dataxy, hx, hy) Given a bidimensional set of points (xi, yj) defined by a rectangular grid { }mjnijhyyihxxyxD yjxiji ...0,...0,,|),( 00 ==+=+=≡ this function computes the numerical double integral over the domain D ∫∫D dxdyyxf ),( "Dataxy" is a (n x m ) rectangular array containing the function values f(xi, yj) "hx" and "hy" are the grid intervals respectively of the x-axis and the y axis Using this function is very simple. Example assuming to have the following rectangular dataset (x, y) in wich each cell represent a point of the grid and thus a value of the function f(xi, yj) where i = 0, 1, 2...20, j = 0, 1, 2...20
  • 185. Xnumbers Tutorial 184 the function = IntegrData2D(B54:V74, C53, A55) returns the value 16.2661406 The scales x and y at the border of the range are not necessary for the function IntegrData2D. They are designed only for clarity. This function can also return the estimate error as second, optional value. To see also the error select two adjacent cells and insert the function with the CTRL+SHIFT+ENTER keys The given data set was generated by the function yx eeyxf −− += 3/ ),( with xi = 0.3 i and yi = 0.2 j Therefore the above result is an approximation of the following integral ∫∫ −− + 3 0 4 0 3/ dydxee yx that is about 16.266082. One interesting feature of this function is that it accepts piecewise-rectangular domains For example. Assume you have to approximate the following integral ∫∫ ++ D yx )12Ln( where D is the domain shown at the right This integral could be split, of course, into three rectangular integrals ∫∫ ∫∫∫∫ ++ 3 0 3 1 2 1 1 0 2 0 3 3 ),(),(),( dydxyxfdydxyxfdydxyxf which the result is about 18.18803389
  • 186. Xnumbers Tutorial 185 But it could be approximated in a worksheet sampling the domain for 0 <= x <= 3 and 0 <= y <= 4 with a suitable grid, for example hx = 0.1 , hy = 0.1 The cells out of the domain must be left empty. The entire range is now B2:AF42 but the function will compute the integral only over the not empty cells. Note that in that case the scales x and y at the border of the range are used for calculating the function f(xi,yi) The integral and its estimate error will be Note the good accuracy of this result. IntegrData2D use the bidimensional trapezoidal formula with one-step of the Richardson extrapolation. This algorithm works very fine with smooth regular functions and its precision is similar the Cavalieri-Simpson formula
  • 187. Xnumbers Tutorial 186 Series Evaluation =xSerie(Funct, Id, a, b, [Param], [DgtMax]) Returns the numeric series of a function f(n) . ∑= = b an nfS )( The parameter "Funct" is a math expression string such as: "2^n/n*(-1)^(n+1)", "x^n/n!", "(-1)^(n)*(3+a)*x/(n-1)", ... Remember the quote " " for passing a string to an Excel function. For further details about the math string see Math formula string "Id" indicates the integer index of the sum (usually "n", "k" , "i", etc.) "a" and "b" are the limits of the sum. The function may also have other parameters ("x", "y", "a", etc.) that can assume real values. "Param" contains labels and values for parameters substitution (if there are). If we pass the variable range without “labels”, the function will assign the values to the variables in the same order that they appear in the formula string, from left to right. The parameter "DgtMax" sets the multiprecision arithmetic. if omitted or zero the function uses the fastest standard arithmetic Example 1. Compute ∑= + ⋅− 10 1 1 )1( n n n n x for x = 2, with standard precision (15 digits) and with 25 digits. The function substitutes x = 2 and then, computes the series f(n) for n =1, 2 ...10 10 2 ... 4 2 3 2 2 2 2 2 )1( 10432 1 10 1 1 −−+−=⋅−∑= + n n n n xSerie("(-1)^(n+1)*x^n/n","n",1,10,2)= -64.8253968253968 Example 2. Compute ∑= = 10 0n n n! x s for x = −1.5, with standard precision (15 digits) and with 25 digits. As known, this series approximates the exponential e^(-1.5) The function xSerie accepts one or more parameters.
  • 188. Xnumbers Tutorial 187 Example 3. Compute the following series where a and b are parameters ∑= +⋅ = 10 1n n n ab s for a = 0.7 and b = 1.5, in standard precision Note that we have enclosed the labels "a" and "b" in the range B4:C5 passed to the function as the argument "Param". The labels indicate to the function the correct assignment between the variables and their values Labels are optional. If we pass only the range B5:C5, without labels, the function assign the values to the variables in the order from left to right. Note how compact and straight is this calculation using the xSerie function. Series acceleration with ∆^2 Many series are very slow to converge requiring therefore methods to accelerate their convergence. The Aitken's extrapolation formula ( ∆2 extrapolation) can be used for this scope. Practically we build a new series S(1) , whose partial sums Sn (1) are given by the Aitken's formula. It is possible to repeat the process starting from the series S(1) to obtain S(2) ., and so on. Example. We want to approximate the following series: ∑ ∞ = + − = 0k k k1 )1( S We know the exact result that is Σ = Log(2) = 0.693147180559945... In the cell B4 we insert the formula =Series($B$1;"k";0;A4) In the cell C4 we insert =(B4-LN(2)) we fill the rows from 5 to 16 simply selecting the range B4:C4 and dragging it down. In the last cell B17 we have inserted the function =ExtDelta2(B10:B16) performing the ∆2 extrapolation using the last 7 values of the sum As we can see, the cell B16 shows the sum with 12 terms; its approximation is very poor having an error of more than 0.01. But if we apply the ∆2 extrapolation at the last seven partial sums S(12) , S(11) , S(10) .... S(6) we have a good approximation with an error less then 1E-8 Note that for reaching this accuracy the given series would need more than 100 million terms!
  • 189. Xnumbers Tutorial 188 Complex Series Evaluation =cplxserie(Formula, a, b, [z0]) This function returns the numeric series of a complex function f(z, n) . ∑= = b an nzfS ),( "Formula" is a math expression string defined by arithmetic operators and common elementary functions such as: "2^n/n*(-1)^(n+1)", "x^n/n!", "(-1)^(n)*(3+j)*x/(n-1)", ... Remember the quote """ to pass a string to an Excel function. The integer variable must be “n”. The parameters "a" and "b" set the minimum and the maximum limits of the integer variable "n". The function may have also a complex variable "z". In that case specify its value in the parameter z0. Example: evaluate the given series for z = z0 = 2-i 20 ... 32 20 1 zzz z n z S n +++== ∑=
  • 190. Xnumbers Tutorial 189 Double Series = xSerie2D(Funct, Id1, a, b, Id2, c, d, [Param], [DgtMax]) Returns the numeric double series of a function f(n, m). ∑∑= = = b am d cn nmfS ),( The parameter "Funct" is a math expression string such as: " x^(n+2*m)/(n!*m!)", "(n+1)/(m+1)!", "comb(n,k)*a^k*b^(n-k)" ... Remember the quote " " for passing a string to an Excel function. For further details about the math string see the par. Math formula string "Id1" , "Id2" indicate the integer indexes of the sum (usually "n", "m", "k" , "i", etc.) "a" , "b", "c", "d" set the minimum and the maximum limits of the integer variables n and m. The function may have other parameters ("x", "y", "a", etc.) that can assume real values. "Param" contains labels and values for parameters substitution (if there are). If we pass the variable range without “labels”, the function will assign the values to the variables in the same order that they appear in the formula string, from left to right. The parameter "DgtMax" sets the multiprecision. if omitted or zero the function uses the fastest standard arithmetic Example. Compute the following double series, in standard (15 digits) and multiprecision (25 digits) ( ) ∑ ∑= = + = 4 0m 10 1n 2mn m!n! x s for x = 0.8 Take care to the index limits because, for large interval, this function can slow down your Excel application
  • 191. Xnumbers Tutorial 190 Trigonometric series = Serie_trig(t, period, spectrum, [offset], [angle], [smooth]) It returns the trigonometric series ( )∑= ++= N 1n sin)0()( nn tnaftf θω T π ω 2 = The set ( ) Nna nn ...1,, =θ is called “spectrum” of the function f(t) Each couple is called harmonic. The parameter “t” can be a single value or a vector values The parameter “period” is the period T. The parameter "spectrum" is an array of (n x 2) elements: the first column contains the amplitude, the second column the phase. The optional parameter "offset" is the average level (default 0) The optional parameter "Angle" sets the angle unit: (RAD (default), DEG, GRAD) Optional parameter smooth (default False) applies the Lanzos factor to the trigonometric series )sin()0()( 1 n N n nn tnaftf θωσ ++= ∑= Nn Nn n / )/sin( π π σ = Here is a worksheet arrangement to tabulate a trigonometric serie having a spectrum of max 8 harmonics (the formulas inserted are in blue) The independent parameters are N (samples) and T (periodo) From those, we get the sampling interval ∆T = T/(N-1) The table at the left contains the parameters for each harmonic: the integer multiple of the harmonic, its amplitude and its phase 0 0.5 1 1.5 2 2.5 3 3.5 4 0 0.5 1 1.5 2 Period T Period T = 1 Note that we can always transform the cosine terms into sine with the following formula )2/sin()cos( παα += n° Arm. Amp Phase 1 1 45 2 0 0 3 0.4 -45
  • 192. Xnumbers Tutorial 191 The “ringing” phenomenon is an overshoot of Fourier series occurring at simple discontinuities (jumps). The series does not converge uniformly to a discontinuous function in a small interval of the discontinuity point. It can be proven that, at each discontinuity point, the series either overshoots or undershoots by about 9% of the magnitude of the jump. The ringing can be removed with the Lanczos sigma factors. Example. The phenomenon is illustrated for a square wave. Given the spectrum of the first 20 harmonics An = { 0, 1, 0, 1/3, 0, 1/5, 0, 1/7…1/19, 0 } and θn = 0, of the square wave with period T = 1, let’s plot its Fourier series with and without Lanczos sigma factors (parameter smooth = true / false respectively) -1.2 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 1.2 0 0.2 0.4 0.6 0.8 1 1.2 -1.2 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 1.2 0 0.05 0.1 0.15 0.2 0.25 As we can see, the series with Lanczos factors (pink line) shows a more stable and smooth behavior. Note, however, that ringing has interesting physical consequences. Consider, for example, a linear electric circuit in which, by means of a switch, a fast voltage transition is created: then the response of the circuit will really exhibit an overshoot
  • 193. Xnumbers Tutorial 192 Trigonometric double serie = Serie2D_trig(x, y, Lx, Ly, Spectrum, [offset], [Angle]) It returns the trigonometric double serie ∑∑= = +++= N n M m mnyxmn ymxnafyxf 1 1 ,,0 )cos(),( θωω where y y x x LL π ω π ω 2 , 2 == The set [ ] MmNna mnmn ...0,...0, ,, ==θ is called “spectrum” of the function f(t). Each couple is called "harmonic". The parameters “x” and “y” are vectors The parameters “Lx” and “Ly” are the base lengths of the x-axis and y-axis. The parameter "Spectrum" is an array of (n x 4) elements: containing the following information: index n, index m, amplitude and phase. That is, for example: n m Amplitude Phase 0 1 1 45 2 1 0.5 -45 3 1 0.25 15.5 1 4 0.125 30 The optional parameter "offset" is the average level (default 0) The optional parameter "Angle" sets the angle unit (RAD (default), DEG, GRAD) The function f(x, y) is returned as an (N x M) array. Use the CTRL+SHIFT+ENTER key to insert this function. Example: Here it is a worksheet arrangement to tabulate a trigonometric serie f(x, y) having a spectrum of max 4 harmonics
  • 194. Xnumbers Tutorial 193 Discrete Convolution Convol(f, g, h, [algo]) This function approximates the convolution of two sampled functions f(ti), g(ti) ∫ ∞+ ∞− −= )()(* dvvtgvfgf The parameters "f" and "g" are column-vectors The parameter "h" is the sampling step. The optional parameter "algo" sets the algorithm: 0 = discrete, 1 = linear (default), 2 = parabolic. The functions returns a vector with the same dimension of the two vectors f and g. The convolutions is also called "Faltung" Here are other examples of convolution plots In the signals analysis the function f is called "input signal" x(t) and g is called "system impulse response" h(t). The convolution f∗g is called "system responce" y(t) The system behavior is reassumed in the following schema
  • 195. Xnumbers Tutorial 194 If the system is described by the following differential equation )()()(' txtykty =⋅+ which has an impulse response given by k t )( − = eth We will use convolution to find the zero input response of this system to the square signal of period T = 1 and amplitude xmax = 1.5 For obtaining this gaph we have used a sampling step of ∆t = 0.02, but this value is not critical. You can choose the size that you like in order to obtain the needed accuracy. h(t)x(t) y(t) In a linear system, the outputs signal y(t) depends by the input signal x(t) and by the inpulse responce of the system h(t). That is: y(t) = ∫ x(v) h(t-v) dv
  • 196. Xnumbers Tutorial 195 Interpolation Polynomial interpolation =PolyInterp(x, xi, yi, [degree], [DgtMax]) =PolyInterpCoef(xi, yi, [degree], [DgtMax]) The first function performs the interpolation of a given set of points (x,y), and returns the value at the point x. The Input parameters Xi and Yi are vectors. The parameter "degree" sets the interpolation polynomial degree (default = 1) The optional parameter DgtMax sets the maximum digits of the multiprecision arithmetic. If omitted or 0, the functions works in faster double precision. The second function returns an array containing the coefficients of the polynomial interpolation. Use CTRL+SHIFT+ENTER for inserting this function. This function use the following popular Newton's formula: ∑ ∏ = − =       −+= n m m j jm xxxxDyxp 1 1 1 11 )(),...()( where D(x1,...xn) is the "divided difference", given by the following recursive formulas: ,....),(,),( 32 32 32 21 21 21 xx yy xxD xx yy xxD − − = − − = ....., ),(),( ),,( 31 3221 321 xx xxDxxD xxxD − − = m mm m xx xxDxxD xxD − − = − 1 211 1 ),...(),...( ),...( Example : Compute the sub-tabulation between a set of 6 given points (x, y) using a linear polynomial and a 3rd degree polynomial and a step of h = 0.2. Note that the given points are not equidistant.
  • 197. Xnumbers Tutorial 196 Sub-Tabulation with Degree = 1 Sub-Tabulation with Degree = 3 0 0.2 0.4 0.6 0.8 1 1.2 0 2 4 6 8 10 y Interp y 0 0.2 0.4 0.6 0.8 1 1.2 0 2 4 6 8 10 y Interp y If we wonder which is the 3rd degree polynomial used for interpolating the point x = 3.2 we can use the function PolyInterpCoef. The interpolation polynomial will be 3 3 2 210)( xaxaxaaxp +++= with the coefficients [a0, a1, a2, a3] returned from the function PolyInterpCoef We can plot this polynomial by the function Polyn. The interpolation polynomial is shown in the above graph. Note that the function has selected the 4 points [x2, x3, x4, x5], discharging the first point x1 and the last point x6, in order to get the most possible accurate interpolation. Note. when the number of the points (x, y) is exactly degree+1, then the polynomial is univocally determined no matter which the interpolation point is: we can pass any number x that we like, for example x = 0. In the following example we determine the 2nd degree polynomial interpolating the given 3 points with 30 significant digits
  • 198. Xnumbers Tutorial 197 Interpolation schemas If the number N of the points (x, y) is exactly equal to degree+1, then the polynomial is univocally determined; but if N > degree+1 there are several different polynomials that can be used for interpolating a given point x. A popular method, adopted by the function PolyInterp is called "sliding centered polynomial" Linear interpolation The linear interpolation is always performed between the nodes xi xi+1 where x ∈[xi xi+1] For degree > 1 the interpolation strategy is more complicated. Odd degree polynomial (example degree = 3) The 3rd degree interpolation is performed between the nodes {xi-1 xi, xi+1, xi+2 } where x ∈[xi , xi+1]. At the first of the segment, the interpolation is performed between the nodes {x1, x2, x3, x4 } where x ∈[x1 , x3]. At the end, the interpolation is performed between the nodes { xN-3, xN-2, xN-1, xN } where x ∈[xN-2 , xN ]. Even degree polynomial (example degree = 2) The 2rd degree interpolation is performed between the nodes {xi-1 xi, xi+1 } and { xi, xi+1, xi+2 } where x ∈[xi , xi+1]. The first set gives the left interpolation polynomial while the second set gives the right interpolation polynomial. In the range x ∈[xi , xi+1] the final interpolation is obtained taking the average of the two polynomials. At the first of the segment, the interpolation is performed between the nodes {x1, x2, x3 } where x ∈[x1 , x2]. At the end, the interpolation is performed between the nodes { xN-2, xN-1, xN } where x ∈[xN-1, xN ]. The average interpolation schemas adopted for even degree polynomials can often increase the gloabal accuracy but, on the other hand, it reduces the efficence. This example helps to explain this trick. Assume to have the data set composed by 4 points; we want to perform the parabolic interpolation of a point y(x) where 0.6 ≤ x ≤ 1 and y(0.6) = y2 and y(1) = y3 i x y 1 0.2 0.163746 2 0.6 0.329287 3 1 0.367879 4 1.4 0.345236 Note that the given points belong to the function x exxy − =)( xi, xi+1 xi-1 xi, xi+1, Left polynomial xN-2, xN-1, xNx1, x2, x3 xi, xi+1, xi+2 Right polynomial average interpolation xi-1 xi, xi+1, xi+2 xN-3, xN-2, xN-1, xNx1, x2, x3, x4
  • 199. Xnumbers Tutorial 198 Being the degree = 2, we can build two interpolation polynomials using the first 3 points and the last 3 points. We have thus the left interpolation polynomial PL(x), covering the range [0.2, 1], and the right interpolation polynomial PR(x) covering the range [0.6, 1.4] The intersection range is [0.6, 1] that is just the range to interpolate y = -0.1914x2 + 0.4027x + 0.1566 y = -0.3967x2 + 0.7312x + 0.0334 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 If we take the average PM(x) = [PL(x)+ PR(x)] / 2 We obtain a new 2nd degree polynomial interpolating the points between [ 0.6 , 1] Observe that it satisfys the nodes constraints: PM(0.6) = y2 , PM(1) = y3 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 If we compute the absolute errors |y(x) - PR(x)| , |y(x) - PL(x)|, |y(x) - PM(x)| in the range 0.6 ≤ x ≤ 1, we observe a significant error reduction for the average polynomial, as shown in the following graph. 0 0.001 0.002 0.003 0.004 0.005 0.006 0.4 0.6 0.8 1 1.2 average left right In this case the average-parabolic polynomial accuracy is comparable with the cubic polynomial. 0 0.0004 0.0008 0.0012 0.0016 0.4 0.6 0.8 1 1.2 cubic average But we have to consider that the simple cubic interpolation schema is more efficient then the average-parabolic. Generally odd degree interpolation schemas are preferable.
  • 200. Xnumbers Tutorial 199 Interpolation with continued fraction Fract_Interp_Coef(xi, yi, [DgtMax]) Fract_Interp(x, xi, coeff, [DgtMax]) These functions perform the interpolation with the continued fraction method. Given, for example, a set of 5 points xi = [ x0, x1, x2, x3, x4] , yi = [ y0, y1, y2, y3, y4 ] the function Fract_Interp_Coef returns the coefficients vector [ a0, a1, a2, a3, a4 ] of the continued fraction expansion given by the following formula: 4 3 3 2 2 1 1 0 0 a xx a xx a xx a xx ay − + − + − + − +≅ The function Fract_Interp returns the interpolate value y at the point x The optional parameter DgtMax sets the maximum digits of the multiprecision arithmetic. If omitted or 0, the functions works in faster double precision. Example: find the continued fraction interpolation for the following (x, y) samples These points are extracted from the following function 9.0 2 2 2 − + = x x y You can verify that the interpolation with these coefficients are better than 1E-14 for all x values in the range [0.4 − 1.6] Note also that this great precision is reached in spite of the pole (x ≅ 0.95) in the interpolation range. The continued fraction interpolation is just suitable for interpolating rational functions. The following graph shows very well how interpolate points fits the curve.
  • 201. Xnumbers Tutorial 200 -50 -40 -30 -20 -10 0 10 20 30 40 50 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 y samples y interp Interpolation with continued fraction The pink dots are the given knots. The blue line is obtained by interpolation There is a pole at x ≅ 0.95 (red dotted line) Note that the points outside the range [0.5, 1.2] keep also a good accuracy. (extrapolation ) Example: Find an interpolation formula for the function tan(x) in the range 0 ≤ x ≤ 1.5 with no more than 7 points. The function tan(x) has a pole at x ≅ 1.57 , near to the upper bound 1.5; so its presence suggests to adopt a fraction interpolation. Assume to sample the function tan(x) at the values (0, 0.2, 0.6, 1, 1.25, 1.45, 1.5). The column A contains the knots of the interpolations In colum B we have inserted the correspondent values of tan(x) In column C we have computed the coefficients of the fraction interpolation. Using the function FracInterp we can interpolate any value between 0 and 1.5 obtaining the graph to the left. The second graph shows the absolute error in the given range. You can verify that the interpolation is better than 1E-5 for any value x in the given range.
  • 202. Xnumbers Tutorial 201 Interpolation with Cubic Spline cspline_interp(Xin , Yin , Xtarget ) cspline_eval(Xin, Yin, Ypp, Xtarget) These functions1 perform the natural cubic spline interpolation Xin is the vector containing the x-values. Yin is the vector containing the y-values.. Xtarget is the x value which we want to compute the interpolation Xpp is the vector containing the 2nd derivative The cubic spline interpolation is based on fitting cubic polynomial curves through all the given set of points, called knots The cubic spline follows these rules: • the curves pass through all the knots • at each knot, the first and second derivatives of the two curves that meet there are equal • at the first and last knot, the second derivatives of each curve is equal to 0 (natural cubic spline constraint). The natural cubic spline has a continuous second derivative (acceleration). This characteristic is very important in many applied sciences (Numeric Control, Automation, etc...) when it is necessary to reduce vibration and noise in electromechanical motions. The drawback is that cubic spline is less efficient than other interpolation methods. The function cspline_eval is faster than cspline_interp, because it uses the information of the 2nd derivatives and does not have to calculate them all over again like the cspline_interp does. The 2nd derivatives can be computed by the function cspline_pre (see next function) Example: Cubic Spline 2nd derivatives cspline_pre(Xin , Yin) This function Returns the cubic spline 2nd derivatives at a given set of points (knots). Xin is the vector containing the x-values. Yin is the vector containing the y-values.. For n knots, it returns an array of n 2nd derivative values. The first and the last values are zero (natural spline constraint). The 2nd derivatives depend only by the given set of knots. So they can be evaluate only once for the whole range of the interpolation. By cspline_eval we can compute faster interpolation. 1 These functions appear thanks to the courtesy of Olgierd Zieba
  • 203. Xnumbers Tutorial 202 Example. Perform the sub-tabulation with ∆x = 0.1 of the following table The given table is in the range A3:B13 In the adjacent column C we have computed the 2nd derivatives by the function cspline_pre. Note that this function returns a vector of 11 values. It must be inserted with the ctrl+shift+enter keys sequence. At the right we have set the new table with step 0.1. the value of F3 has been interpolate by the formula = cspline_eval($A$3:$A$13; $B$3:$B$13; $C$3:$C$13; E3) The other values are computed simply by dragging down the cell F3. The following plot shows the knots and the cubic spline fitting The points of the original table was extracted from the function y = [cos(x)]4 . You can verify that the interpolation accuracy is better than 1% over the entire range.
  • 204. Xnumbers Tutorial 203 Cubic Spline Coefficients cspline_coeff(Xin , Yin) This function1 returns the coefficients of the cubic spline polynomials Xin is the vector containing the x-values. Yin is the vector containing the y-values.. It returns an (n-1 x 4 ) array where n is the number of knots. Each row contains the coefficients of the cubic polynomial of each segment s [as,3 as,2 as,1 as,0] 0,1, 2 2, 3 3, )()()( ssssssss axxaxxaxxay +−+−+−= where s = 1, 2, (n-1) Example. Find the cubic spline polynomials that fit the given knots 1 These functions appear thanks to the courtesy of Olgierd Zieba
  • 205. Xnumbers Tutorial 204 2D Mesh Interpolation =Interp_Mesh(TableXY) This function performs the linear interpolation of a bivariate function given in a pivot table XY. The x-values and y-values must be sorted but not necessarily equidistant. This function returns an array. Let's see how it works. Regularization As we can see, the use of this function is straight. Simply select the area where you want to insert the new table, insert the function Interp_mash and pass the old table as parameter. The function Interp_mesh returns the equidistant-linear-interpolated array. Or, in other words, it returns the regularized table Rescaling We can obtain a sub-tabulated function in an very fast way Simply select a larger area The function Interp_mesh counts the cells that you have selected and fill all the cells with the linear interpolated values In this case the given table has 5 x 4 = 20 values. The new table has 9 x 7 = 63 values; therefore, there are 43 new interpolated values
  • 206. Xnumbers Tutorial 205 Macro Mesh Fill This macro, accessible from the menu Macro / Regression, is useful for completing a 2D mesh by interpolation. Practically, given a (n x m) matrix with holes, it tries to recover the missing data. We could say a special case of bi-dimensional linear regression Using this macro is very simple. Select the matrix with holes and start the macro. Example Original data matrix complete filled. It was generated by the following linear formula jia 32ij −= the same data matrix with same missing values (holes). Select the matrix (range B13:H21) and start the macro The macro will try to rebuild the missing value (red) by linear interpolation, using the data of the nearest nodes Of course, this process gives exactly the original matrix only in the linear case For all other case we have an error: Generally, it is small if also the ratio between holes and filled nodes is small and if the original data is smooth
  • 207. Xnumbers Tutorial 206 Interpolation of tabulated data function Given a tabulated data (x i , y i), i = 1...N, generally not equidistant, we have to interpolate the function y for an arbitrary x value, where x1 ≤ x ≤ xN The points (x i , y i) are called knots of the interpolation Cubic Spline interpolation The goal of cubic spline interpolation is to get a polynomial interpolation formula that is smooth in the 1st derivative, and continuous in the 2nd derivative, within the interval and at each boundaries. This method ensures that the functions y(x), y’(x), and y’’(x) are equal at the interior node points for adjacent segments. The cubic polynomials Pi(x) satisfie these constrains. Pi(xi−1) = yi−1 for i = 2...N Pi(xi) = yi for i = 2...N P'i(xi) = P'i+1(xi) for i = 2...N−1 P''i(xi) = P''i+1(xi) for i = 2...N−1 One form to write the interpolation polynomials is: P(x) = A Pi + B Pi+1 + C P’’i + D P’’i+1 , for i = 1...(N-1) Where: A = (xi+1 – x)/ (xi+1 – xi) B = 1 – A C = 1/6 (A3 – A) (xi+1 – xi)2 D = 1/6 (B3 – B) (xi+1 – xi)2 The 2nd derivatives can be evaluated by the following linear equations (xi – x i-1) P’’i-1 + 2 (xi+1 – xi-1) P’’i + (xi+1 – xi) P’’i+1 = Hi for i = 2...(N-1) where: Hi = 6[(Pi+1 – Pi)/(xi+1 – xi) – (Pi – Pi-1)/(xi – xi-1)] , P’’1 = 0 , P’’N = 0 That gives the following tridiagonal matrix system 2(x3 – x1) (x3 – x2) 0 0 ... 0 P’’2 H2 (x3 – x2) 2(x4 – x2) (x4 – x3) 0 ... 0 P’’3 H3 0 (x4 – x3) 2(x5 – x3) (x5 – x4) ... 0 P’’4 = H4 0 0 (x5 – x4) 2(x6 – x4) ... ... P’’5 H5 ... ... ... ... ... (xN – xN-1) ... 0 0 0 ... (xN – xN-1) 2(xN – xN-2) P’’N-1 HN-1 Another common way to write the interpolation polynomial is: P(x) = a3i (x – xi)3 + a2i (x – xi)2 + a1i (x – xi) + a0i , xi ≤ x < xi+1 for i = 1...(N-1) Where the coefficients are: a3i = (P’’i+1 – P’’i)/(xi+1 – xi)/6 a2i = P’’i / 2 a1i = (Pi+1 – Pi)/(xi+1 – xi) – (xi+1 – xi) (2 P’’i + P’’i+1)/6 a0i = Pi The matrix of the system is tridiagonal, therefore can be solved in O(N) operations
  • 208. Xnumbers Tutorial 207 We note also that its solution (P’’1 , P’’2 , ... P’’N ) depends only by the given knots, therefore the 2nd derivatives can be evaluated only once for any interpolate. This example shows how the interpolation spline works. X Y 0 0 1 2 2.5 4 3 3 4 4 5 1 For these 6 knots we obtain 5 cubic polynomials having the following coefficients Polynomials a3 a2 a1 a0 Range 1st spline 0.20148927 0 1.79851073 0 0 ≤ x < 1 2nd spline -0.8783764 0.60446781 2.40297854 2 1 ≤ x < 2.5 3rd spline 5.54708717 -3.348226 -1.7126588 4 2.5 ≤ x < 3 4th spline -3.0718353 4.97240473 -0.9005694 3 3 ≤ x < 4 5th spline 1.41436706 -4.2431012 -0.1712659 4 4 ≤ x < 5 In the graphs below we can see the interpolated points (dotted line) fitting the data points and the cubic polynomials (green line) passing through the nodes of each segment. Each polynomial interpolates inside the proper segment. That is: the 1st spline works for 0 ≤ x < 1, the 2nd spline for 1 ≤ x < 2.5, and so on. In the graphs below are shown the entire interpolation line (left) and the 1st spline (right). -2 -1 0 1 2 3 4 5 6 -1 1 3 5 spline Interp knots -2 -1 0 1 2 3 4 5 6 -1 1 3 5 spline Interp knots 1st spline In the graphs below are shown the 2nd spline (left) and the 3rd spline (right) -2 -1 0 1 2 3 4 5 6 -1 1 3 5 spline Interp knots 2nd spline -2 -1 0 1 2 3 4 5 6 -1 1 3 5 spline Interp knots 3rd spline In the graphs below are shown the 4th spline (left) and the 5th spline (right) Assuming to have to sub-tabulate with a step ∆x = 0.1 a given function known only in the following 6 points Note that these points are unequal spaced
  • 209. Xnumbers Tutorial 208 -2 -1 0 1 2 3 4 5 6 -1 1 3 5 spline Interp knots 4th spline -2 -1 0 1 2 3 4 5 6 -1 1 3 5 spline Interp knots 5th spline Let’s examine the 1st and 2nd derivatives. We can compute them either analytically or numerically using – for example -the following derivative formulas: y’(xi) ≅ (yi+1 – yi-1)/2∆x y’’(xi) ≅ (yi+1 – 2 yi + yi-1)/ ∆x2 In both ways, we get the following graphs Y' -5 -4 -3 -2 -1 0 1 2 3 0 1 2 3 4 5 Y'' -10 -5 0 5 10 15 0 1 2 3 4 5 As we can see the 1st derivatives is smooth and the 2nd is continuous. This last feature is particularly appreciated in many fields of engineering. Although this algorithm is less efficient than other polynomial interpolation methods, it has the advantage of following the interpolated curve without the spurious oscillations that other schemes can create
  • 210. Xnumbers Tutorial 209 Other test functions In our last example we have found that both methods can provide acceptable interpolation for all range of x . Thus, there are same case, that the superiority of the spline interpolation is more evident. Gerald [2] used the “bump” test case to illustrate problems with other interpolation methods. Let’s see. Interpolate the following knots Y = (cos(x))10 , for x= -2 , -1 , -0.5 , 0 , 0.5 , 1 , 2 Plotting the interpolated values with a step of 0.1, we get the following graphs -0.2 0 0.2 0.4 0.6 0.8 1 1.2 -2 -1 0 1 2 cos(x)^10 cubic poly -0.2 0 0.2 0.4 0.6 0.8 1 1.2 -2 -1 0 1 2 cos(x)^10 cubic spline The curves appear acceptable in both graphs. The second shows a closer fit near the points x = 1 and x = -1 where are "knees" of the curve. But matching the error plots, we see clearly the better accuracy of the spline interpolation. -0.06 -0.04 -0.02 0 0.02 0.04 0.06 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 error cubic poly error cubic spline As we can see, the amplitude error of the cubic polynomial is much more than the spline. We can show that an higher order of the interpolation polynomial, is even worst In this case, the cubic spline is the better choice
  • 211. Xnumbers Tutorial 210 Continued fraction interpolation Continued fractions are often a powerful ways of interpolation when we work near the functions poles. In XNUMBERS the continued fraction coefficients can be obtained by the function FracInterpCoef and the interpolation value with FracInterp Example. Interpolate the following dataset y(x) = 1/(x+0.01)1/2 , for x = 0, 0.05, 01, 0.2, 0.5, 1 , with step ∆x = 0.1 In the graphs below we have plotted the interpolated values obtained with three different methods: cubic polynomial, cubic spline and continued fraction Note that y(x) have a poles in x = −0.01 very near to the node x = 0 0 2 4 6 8 10 12 0 0.2 0.4 0.6 0.8 1 1.2 y knots 0 2 4 6 8 10 12 0 0.2 0.4 0.6 0.8 1 1.2 y cubic poly 0 2 4 6 8 10 12 0 0.2 0.4 0.6 0.8 1 1.2 y cubic spline 0 2 4 6 8 10 12 0 0.2 0.4 0.6 0.8 1 1.2 y fraction We can see a good general accuracy except for the final part of the polynomial interpolation method. In this case, the worst accuracy is concentrated where the function is more flat, but, surprisingly, this perturbation is due to the distant pole in x = -0.01. We note also that both spline and fraction methods keep a good accuracy also for point external at the interpolation range ( extrapolation for x >1 ) 0.0001 0.001 0.01 0.1 1 10 0 0.2 0.4 0.6 0.8 1 poly spline fraction Method avg. err. Cubic spline 0.22 Cubic poly 0.19 Contined fraction 0.003
  • 212. Xnumbers Tutorial 211 Differential Equations Xnumbers contains functions for solving the following differential problem of the 1st order with initial conditions (Cauchy’s problem): 00 )(,),( ytyytfy ==′ and for solving the 1st order ordinary differential system. 00 )(,),( yyyfy ==′ tt ⇔               = = =        =′ =′ =′ 00 2002 1001 21 2122 2111 )( .... )( )( , )...,,( .... )...,,( )...,,( nnnnn n n yty yty yty yyytfy yyytfy yyytfy ODE Runge-Kutta 4 = ODE_RK4(Equations, VarInit, Step, [Par, …]) This function integrates numerically a 1st order ordinary differential equation or a 1st order differential system, with the Runge-Kutta formula of 4th order ),( )5.0,5.0( )5.0,5.0( ),( 34 23 12 1 khyhtfk khyhtfk khyhtfk ytfk ii ii ii ii ++= ++= ++= = ( )43211 22 6 kkkk h yy ii ++++=+ "Equations" is a math expression string containing the equation to solve. For a system It is a vector of equations. Examples of correct equation definition are: y’ = -2*y*x , v’ = 2*x-v^2+v , y1’ = -3*y1+y2+sin(10*t) Each string may contain symbolic functions with variables, operators, parenthesis and other basic functions. The parameter "VarInit" is a vector containing the initial values. It has two values for two variables [ t0, y0 ]. For a system with n+1 variables, "Varinit" is an (n+1) vector [ t0, y10 , y20, ….yn0 ]. The parameter "Step" is the integration step. The optional parameter "Par" contains the values of other extra parameters of the equations. Let’s see how it works with an example Solve numerically the following Cauchy’s problem for 0 ≤ x ≤ 3 1)0(,2 =−=′ yxyy We know that the exact solution is 2 x ey − = We can arrange a worksheet like the following
  • 213. Xnumbers Tutorial 212 As we can see, we have written in cell G5 the differential equation y’ = - 2*x*y In the range A5:B5 we have inserted the starting values of x and y. Note that we have written the labels just above theirs values. Labels are necessary for the correct variables assignment Finally, in the range A6:B6 - just below the starting values - we have inserted the ODE_RK4, that returns the value y(0.2) = 0.9607893… with a good accuracy of about 1E-7 (compare with the exact solution) Tip: In order to get all other values, select the range A6:B6 and simply drag it down. The cells below will be filled automatically Only remember to fix the constant cells in the function with the $ symbol =ODE_RK4($G$5,A5:B5,$F$5) We have also added the column with the exact values in order to check the approximation error. Both exact and approximated solutions are plotted in the following graph 0 0.2 0.4 0.6 0.8 1 1.2 0 0.5 1 1.5 2 2.5 3 y y exact The approximate solution (dots) fits very good the exact solution (pink curve).
  • 214. Xnumbers Tutorial 213 If you need, you can include parameters inside the differential equation Example. Solve the following differential problem 1)0( ' n = ⋅⋅−= y yxky where k = 2 and n = 1 Note that we have added the labels "k" and "n" above the cells D2 and E2. In this way, the parser will correctly substitute the value 2 for the variable "k" and 1 for the variable "n". in the differential equation Do not forget the labels "x" and "y" in the cells A5 and B5 Example: Solve the following linear differential equation 0)1(, 1 ' =⋅=+ yxay x y n For n = 3 and a = 1. Rearranging, we get 0)1(,' =−⋅= y x y xay n Note the labels "a" and "n" above the cells D2 and E2. In this way, the parser will substitute the value 1 for the variable "a" and 3 for the variable "n". in the differential equation Do not forget the labels "x" and "y" in the cells A5 and B5 With the step h = 0.1, we have a numerical solution with a very good approximation comparing with the exact solution )5/()1( 5 xxy −= , (better than 1E-6) 0.E+00 1.E-07 2.E-07 3.E-07 4.E-07 5.E-07 6.E-07 7.E-07 8.E-07 0 0.5 1 1.5 2 2.5 3 3.5 4 Relative Error
  • 215. Xnumbers Tutorial 214 This function can be used to solve ordinary differential systems. Example: Solve numerically the following differential system, where v(t) and i(t) are the voltage and the current of an electric network    ⋅+⋅−= ⋅−= vii viv 155' 7'    = = 0)0( 10)0( i v The computation can be arranged as following. Write the variables labels in the row 6. The labels “v” and “i” must be the same that you have written in the equations. Just one row below, insert the starting values in the same order. Select the range A8:C8 and insert the function ODE_RK4. The first step will be returned. Now select this row and drag it down for evaluating all the steps that you need The graph below shows the transient of v(t) and i(t) with good accuracy 0 2 4 6 8 10 12 0 0.4 0.8 1.2 v i Note that you can change the step “h” in order to re-compute the transient in a very fast and quick way. Optional constant parameters can be arranged. For example if you want to add a parameter R, independent from the time “t”, write: Constant parameters can be written in any part of the worksheet. You need only to add the labels with the same symbols with they appear in the differential equations. In this case, we have added the label "R" in the cell C1, upon its values. You can add as many optional parameters that you like
  • 216. Xnumbers Tutorial 215 ODE Multi-Steps Another very popular method for integrating ordinary differential equations adopts the multi- step Adams’ formulas. Even if a little formally complicated, they are very fast, and adapted to build a large family of ODE integration methods The multi-step Adams’ formulas can be generally written as: ( )10121 1 11 '...''' +−−−− = +−−+ ⋅+⋅+⋅+=⋅+= ∑ NiiNiNi N k kikNii yyy M h yy M h yy ββββ ( )20211 1 21 '...''' +−−+− = +−−+ ⋅+⋅+⋅+=⋅+= ∑ NiiNiNi N k kikNii yyy M h yy M h yy ββββ where ),(' iii ytfy = ihtti ⋅+= 0 The first formula generates the explicit formulas – also called predictor formulas. The second formula generates the implicit formulas – also called corrector formulas. The number N is the order of the formula. A formula of N order requires N starting steps. Of course, formulas with high N are more accurate. For N = 1 we get the popular Euler integration formulas iii yhyy '1 ⋅+=+ Euler’s predictor (1 step) ( )iiii yy h yy '' 2 11 +⋅+= ++ Trapezoid formula corrector (1 step) Theirs errors are given by )2(2 2 1 yhe ≈ Error predictor 1st order )3(3 12 1 yhe −≈ Error corrector 2st order For N = 4 we get the popular Adams-Bashfort-Moulton predictor-corrector formulas ( )3211 '9'37'59'55 24 −−−+ −+−⋅+= iiiiii yyyy h yy Predictor (4 step) ( )2111 ''5'19'9 24 −−++ +−+⋅+= iiiiii yyyy h yy Corrector (4 step) Theirs errors are given by )5(5 720 251 yhe ≈ Error predictor 4th order )5(5 720 19 yhe −≈ Error corrector 4th order There are a large set of predictor-corrector formulas
  • 217. Xnumbers Tutorial 216 Multi-step coefficents tables The following tables list the coefficents for the Adams’ predictor-corrector formulas up to the 9th order and relative errors Multi-step Predictor coefficients Ord. ⇒ 1 2 3 4 5 6 7 8 9 10 M 1 2 12 24 720 1440 60480 120960 3628800 7257600 β 0 1 -1 5 -9 251 -475 19087 -36799 1070017 -2082753 β 1 3 -16 37 -1274 2877 -134472 295767 -9664106 20884811 β 2 23 -59 2616 -7298 407139 -1041723 38833486 -94307320 β 3 55 -2774 9982 -688256 2102243 -91172642 252618224 β 4 1901 -7923 705549 -2664477 137968480 -444772162 β 5 4277 -447288 2183877 -139855262 538363838 β 6 198721 -1152169 95476786 -454661776 β 7 434241 -43125206 265932680 β 8 14097247 -104995189 β 9 30277247 Multi-step Corrector coefficients Ord. ⇒ 1 2 3 4 5 6 7 8 9 10 M 2 12 24 720 1440 60480 120960 3628800 7257600 β 0 1 -1 1 -19 27 -863 1375 -33953 57281 β 1 1 8 -5 106 -173 6312 -11351 312874 -583435 β 2 5 19 -264 482 -20211 41499 -1291214 2687864 β 3 9 646 -798 37504 -88547 3146338 -7394032 β 4 251 1427 -46461 123133 -5033120 13510082 β 5 475 65112 -121797 5595358 -17283646 β 6 19087 139849 -4604594 16002320 β 7 36799 4467094 -11271304 β 8 1070017 9449717 β 9 2082753 Error coefficient The general error is e ≈ -k⋅hOrd+1 y(Ord+1) where k is given by the following table Ord. ⇒ 1 2 3 4 5 6 7 8 9 10 predictor 0.5 0.41667 0.375 0.34861 0.32986 0.31559 0.30422 0.29487 0.28698 0.28019 corrector - -0.0833 -0.0417 -0.0264 -0.0188 -0.0143 -0.0114 -0.0094 -0.0079 -0.0068 The predictor-corrector algorithm Usually the multi-step formulas, implicit and explicit, are used together to build a Predictor- Corrector algorithm . Here is how to build the 2nd order PEC algorithm (Prediction-Evaluation- Correction). It uses the Euler’s formula as predictor and the trapezoidal formula as corrector Prediction Evaluation Correction yp1 = y0 +h f(t0, y0) ⇒ f(t1, yp1) ⇒ y1 = y0 +h/2 [f(t0, y0) +f(t1, yp1)] yp2 = y1 +h f(t1, yp1) ⇒ f(t2, yp2) ⇒ y2 = y1 +h/2 [f(t1, y1) +f(t2, yp2)] yp3 =…. …. …. The value y1 can be reused to evaluate again the function f(t1, y1), that can be used in the corrector formula to obtain a more accurate value for y1.
  • 218. Xnumbers Tutorial 217 If we indicate the first value obtained by the corrector with y1 (1) and the second value with y1 (2) we can arrange a new following schema Prediction Evaluation Correction Evaluation Correction yp1 ⇒ f(t1, yp1) ⇒ y1 (1) ⇒ f(t1, y1 (1) ) ⇒ y1 (2) This is the so called PECEC or P(EC)2 schema. The group EC can also be repeated m-times or even iterated still the convergence. In these cases we have the schemas P(EC)m and P(EC)∞ respectively. Note that, for m >> 1 the final accuracy depends mainly by the corrector. Let's come back to the PEC schema. We note that, at the step, we use the value f(t1, yp1) to predict the new value yp2 We could increase the accuracy if we take the better approximation f(t1, y1). The new schema becomes: Prediction Evaluation Correction Evaluation yp1 ⇒ f(t1, yp1) ⇒ y1 ⇒ f(t1, y1) ⇒ This schema is called PECE and it is used very often being a reasonable compromise between the accuracy and the computation effort. Using different schemas with different predictor-corrector formulas we can build a wide set of algorithms for the ODE integration. Of course they are not equivalent at all. Same of them have a high accuracy, others show a better efficiency and others have a better stability. This last characteristic may be very important for long integration intervals. In fact, the most algorithms, especially those with higher order, become unstable when the integration step grows over a limit. Algorithms that are stable for any integration step (so called A-stable algorithms) are much appreciated, but unfortunately they require implicit formulas that, generally, can be solved only by iterative algorithms. The 2nd order trapezoid formula is one of the most popular A-stable formula.
  • 219. Xnumbers Tutorial 218 Predictor- Corrector = ODE_PRE(yn, f, h) = ODE_COR(yn, fp, f, h) These functions perform the integration of the ordinary differential equations with the popular multi-step predictor-corrector Adams’ formulas 00 )(,),( ytyytfy ==′ The first function returns the predictor value yn+1,p while the second function returns the corrector yn+1. The parameter “yn” is the last point of the function y(t). The parameter “f” is a vector containing the last N values of the derivative of y(t). That are the last N-1 values of the corrector. The parameter “fp”, only for the corrector, is the best approximation of the derivative of y(t) at the step n+1. Usually it is provided by a predictor formula The parameter “h” sets the integration step PECE algorithm of 2nd order Now we see how arrange a PECE algorithm of 2nd order to solve a the following differential problem. 2)0(,' 2 =−= yxyy Let's set in a cell that we like the integration step “h” and then the heading of the data table. We set separate columns for predictor and corrector values Build the first row. Begin to insert the starting values (x0, y0) in the cells A6 and B6 respectively, and the formula evaluations of f(x,y) in the cell C6 and E6. The corrector value is set equal to the starting value B6 The second row is a bit more complicated. Let’s see. Select the first row A6:E6 and drag it down one row. This will copy the formula for fp and fc Insert in the cell A7 the increment formula xi+1 = xi+h Now we have to add the predictor and corrector function
  • 220. Xnumbers Tutorial 219 Insert in the cell B7 =ODE_PRE(yn, f, h) “yn” is the last value of y(x). contained in D6. “f” is the last value of f(x,y) contained in E6. “h” is the step B3. Insert in the cell D7 =ODE_COR(yn, fp, f, h) Where “yn” is the last value of y(x). In that case is D6. “f” is the last value of f(x,y), E6. “fp” is the predicted. value of f(x,y), C7 in this case. “h” is the constan step. Now the setting of the PECE algorithm of 2nd order is completed. Select the second row A7:E7 and drag it down in order to calculate the steps that you want. The yP and yC values can be compared with the ones of the exact solution. 2 1 2 x y + = The differences: )( )( iicic iipip xyyd xyyd −= −= are plotted in the graph at the right We note clearly the characteristic behavior of the predictor-corrector algorithm. -0.04 -0.02 0 0.02 0.04 0.06 0.08 0.1 0 1 2 3 err predictor 2nd PECE The second formula refines the approximation of the first one. The final accuracy of PECE algorithm is practically the accuracy of the corrector
  • 221. Xnumbers Tutorial 220 PECE algorithm of 4th order Now we solve the above differential equation with a 4th order PECE algorithm using the 4 steps Adams-Bashforth-Moulton formulas 2)0(,' 2 =−= yxyy To start this algorithm needs 4 steps. A good set of starting steps is: x y(x) 0 2 0.2 1.9230769231 0.4 1.7241379310 0.6 1.4705882353 The first 4 rows of the PECE algorithm are built as shown in the previous example. The first 4 values of yp and yc are the same. Now let's insert in the cell B10 =ODE_PRE(yn, f, h) where “yn” is the last value of y(x), D9 in that case. “f” is a vector of the the last four values of f(x,y), E6:E9 in this case. “h” is the step B3. Insert in the cell D10 =ODE_COR(yn, fp, f, h) where “yn” is the last value of y(x). In that case D9. “f” is a vector of the last 3 values of f(x,y), E7:E9. “fp” is the predicted value of f(x,y), C10 in this case. “h” is the step B3 T Now the setting of the PECE algorithm of 4th order is completed. Select the 5th row and drag it down in order to calculate the steps you want. he predictor-corrector error curves are shown in the following graph -0.01 -0.005 0 0.005 0.01 0.015 0 0.5 1 1.5 2 2.5 3 ep 4th PECE We do not investigate here how to get the extra 3 values (they could comes by Runge-Kutta method or by Taylor series approximation). The only thing that we have to point out is that these values must be sufficiently accurate in order to preserve the global accuracy of the algorithm
  • 222. Xnumbers Tutorial 221 In order to compare the accuracy of the solutions of the this algorithm with the 2nd order algorithm of the previous example let's draw both the error curves in a same graph -0.006 -0.004 -0.002 0 0.002 0.004 0.006 0.008 0.01 0 0.5 1 1.5 2 2.5 3 4th PECE 2nd PECE As we can see, the 4th order algorithm is evidently more accurated then the 2nd order. On the other hand, the first one requires an extra work for providing 3 starting points. ODE Predictor-Corrector 4 = ODE_PC4(Equations, VarInit, Step, [Par, …]) This function integrates numerically an ordinary differential equation of 1st order or differential system of 1st order, with the 4th order P(EC)^2 schema predictor-corrector of Adams- Bashforth-Moulton. The input arguments are identical to the function ODE_RK4 The function can return all the solution points in an array. Before inserting the function, select as many rows that you want to fill. For example if you select a (p x n+1) array , the function will return p solution points of a n x n differential system. Let’s see how it works with an example    −= −−−= wvw wvvvv ' )21)(1('    −= = 1)0( 0)0( w v (FitzHug-Naguno model) Insert the equations in the cell A2 and A3. Insert the starting point [t0, v0, w0 ] = [0, 0, -1] in the range A7:C7 Select the range A8:C167 in order to get simultaneously 160 integration points and insert the function ODE_PC4 by the CTRL+SHIFT+ENTER sequence.
  • 223. Xnumbers Tutorial 222 The following plot shows the obtained functions v(t) and w(t) for 0 <= t <= 8 The 4th order P(EC)2 schema predictor-corrector of Adams-Bashforth-Moulton is detailed in the following flow-graph The initial extra 3 steps, necessary for starting the predictor-corrector algorithm, are calculated by the function ODE_RK4 requiring 4 function evaluations for step. After that, the PC schema require only 2 function evaluations for step. Conventionally we measure the effort (EF) counting the number of function evaluation for n steps. EF = 12+2*n (for PC schema), EF = 4*n (for RK algorithm) For n >> 1 the effort reduction of the P(EC)2 schema is about 50% Compute the first 3 steps by the 4th order Runge-Kutta algorithm Starting x0, y0 , h Starting points x0, y0 x1, y1 x2, y2 x3, y3 Compute the next step by 4th order by the 4 th order Predictor Evaluate the function fp = f (x4, yp) yp Refine the step by the 4 th order Corrector fp Evaluate the function fc = f (x4, yc) yc P E C E Refine the step by the 4th order Corrector fc y4 right-shift the points and repeat x1, y1 x2, y2 x3, y3 x4, y4 C P (EC)2 Predictor-Corrector schema of 4th order
  • 224. Xnumbers Tutorial 223 ODE Implicit Predictor-Corrector ODE_PC2I(Equations, VarInit, Step, [Par, …]) This function integrates numerically an ordinary differential equation of 1st order or a differential system of 1st order, with the 2th order implicit Predictor-Corrector method. Thanks to its large stability it is suitable for solving "stiff" problems. The input arguments are identical to the function ODE_RK4 The function can return all the solution points in an array. Before inserting the function, select as many rows that you want to fill. For example if you select a (p x n+1) array , the function will return p solution points of a n x n differential system. This function uses the 1st order Euler formula as predictor and the 2nd order trapezoidal formula as the corrector. ),(1 nnnn ytfhyy ⋅+=+ Predictor (Euler formula) ( )),(),(2/ 111 +++ +⋅+= nnnnnn ytfytfhyy Corrector (Trapezoidal formula) The second non-linear equation is started with the predicted value and then solved respect to the variable yn+1 till the convergence. This algorithm, despite its low order, exibits a large stability and, thus, it is suitable for solving "stiff" problems Let’s see how it works with an example Solve numerically the following 2nd order Cauchy’s problem for 1 ≤ x ≤ 7 and k = 100 0)1(2 2 =+++ yk dx dy k xd yd , 5.0)1(',1)1( −== yy First of all, we transform the problem in a 1st order differential equations system taking ', 21 yyyy ==    +−−= = 212 21 )1(' ' ykyky yy    −= = 5.0)1( 1)1( 2 1 y y A possible arrangement may be the following We have written in cell A2 and A3 the differential equations In cell D3 we have inserted the k parameter and in the cell E3 the h step In the range A8:C8 we have put the starting values for x and y1 and y2. note that we have also added the labels "k" , "x", "y1", "y2" just above theirs values. Labels are necessary for the correct assignment in the symbolic equation. Now select the range A9:C40 and insert the function ODE_PC2I passing its arguments The scatter plot of the 30 points (x, y1) is shown in the following graph
  • 225. Xnumbers Tutorial 224 Compare the approximate solution (dotted line) with the exact one (pink continue line) The fitting, even with only 30 points, looks good. If we try to solve this problem with the 4th order Runge-Kutta algorithm we have to choose a step less then 0.025 for avoiding the instability and, thus, we need more than 250 points for reaching the same integration interval. The Lotcka-Volterra Model The following two-dimensional differential system       +−= −= yxaya dt dy yxaxa dt dx 43 21 is called the Lockta-Volterra model or also "prey-predator" model. It is very useful in biology, chemistry and many other fields. The numerical integration of this ODE family requires a stable algorithm otherwise the result are not acceptable The function x(t) and y(t), depending from the time, may represent different things. In a biological models, for example, they simulate respectively the population of pray (rabbits) and predator (foxes) at time t. The proportionality constants a1 , a2 , a3 , and a4 are positive. It is known that its solution leads to oscillate steady-state. . Let's see with a practical example    +−= −= xyxy xyxx 25' 22'    = = 1)0( 1)0( y x Insert the equation defintion in the cells A1 and A2 , the step in the cell C2 = 0.02 and the starting values [0, 1, 1] in the range A6:C6 Do not miss the labels "t", "x", "y" Then, select the range A7:C407 and insert the function ODE_PC2I with the ctrl-shift-enter keys sequence The following graph at the left shows the result of the function x(t) and y(t). By performing the scatter plot of x-y variable we have the steady-state plot at the right. It shows a closed loop, a limit cycle.
  • 226. Xnumbers Tutorial 225 0 1 2 3 4 5 6 0 2 4 6 8 10 12 x y 0 1 2 3 0 2 4 6 We can easily change the zoom of the graph by changing the integration step h Note that the oscillations are quite deep. The above system has two equilibrium points where dx/dt = 0 and dy/dt = 0 simultaneously, that can be found solving the algebric system    =+− =− 025 022 xyx xyx ⇒    = = 0 0 y x    = = 1 5.2 y x The non trivial solutions means that if the initial populations start from x0 = 2.5 and y0 = 1, then there will be no oscillation and the population fraction x/y will be always constant in the time. On the contrary, the populations become progressively to oscillate when the initial populations are different from the equilibrium point [2.5 , 1]. Here same examples: x(0) = 2.45 and y(0) = 1 x(0) = 2.4 and y(0) = 1 0 0.5 1 1.5 2 2.5 3 0 2 4 6 8 10 12 x y 0 0.5 1 1.5 2 2.5 3 0 2 4 6 8 10 12 x y
  • 227. Xnumbers Tutorial 226 Differential Systems OD Linear System = ODE_SYSL(A, y0, step, [b])) This function integrates numerically a system of ordinary linear differential equations of 1st order with constant coefficients, starting from an initial value. For example, the general form of a 3x3 linear differential system is:      +++= +++= +++= 31331321313 21231221212 11131121111 ' ' ' byayayay byayayay byayayay where all the coefficients aij and bi are constant. Such system can be put in the following handy matrix form. [ ] byAy +⋅=' where           = 333231 232221 131211 aaa aaa aaa A [ ]T yyyy 321 ,,= [ ]T bbbb 321 ,,= and with the initial condition: [ ]T yyyy )0( 3 (0) 2 (0) 10 ,,= The constant term b is optional. If omitted the system is called "homogeneous" This function uses the exponential expansion method that, for this kind of differential systems, is both accurate and efficient. The function returns an (n x m ) array containing all the nodes of the integration: m is the number of equations; n is the numbers of the nodes. The function automatically sets n equal to the rows of the range that you have selected before inserting it. Let's see how it works practically Solve the following homogeneous differential system with constant coefficients [ ] yAy ⋅=' where           −− − −− = 122 16616 191020 A [ ]T y 0,0,10 = For 0 ≤ x ≤ 4 and h = 0.05 The numerical solution can be arranged as in the following worksheet The initial values are in the first row (range B7:D7). The column "x" was added only for clarity but it is not indispensable at all. Select the range B8:D87, than insert the function ODE_SYSL giving the suitable parameters A, y0, h. Then press CTRL+SHIFT+ENTER
  • 228. Xnumbers Tutorial 227 All the 240 cells will be filled with the nodal solutions of y1, y2, y3. The scale can be esily arranged simply by changing the parameter h -1 -0.5 0 0.5 1 1.5 0 0.5 1 1.5 2 2.5 3 3.5 4 y1 y2 y3 -1 -0.5 0 0.5 1 1.5 0 0.25 0.5 0.75 1 y1 y2 y3 h = 0.05 h = 0.012 Compare with the exact solutions      +−= −= ++−= −− −− −−− xx xx xxx eey eey eeey 2 3 102 2 102 1 22 22 22 High order linear ODE The function ODE_SYSL can also be used to solve high order linear ODE with constant coefficients, that in general can be written as byayayayay n n n =++++ − − 012 )1( 1 )( '''.... with the initial conditions 00 )( yxy = , ')(' 00 yxy = , '')('' 00 yxy = , , )1( 00 )1( )( −− = nn yxy As known, such ODE can be transformed into a linear differential system of 1st order.                 +                                 −−−− =                 − − − by y y y aaaay y y y n n nn n 0 0 0 1000 0100 0010 ' ' ' ' 1 2 1 1210 1 2 1 LL M M KOKKK M M L having the initial conditions [ ] [ ]TnT n yyyyyyyyy )0(,)0('',)0(',)0()0(,)0(,)0(,)0( 1 3210 − ==
  • 229. Xnumbers Tutorial 228 For example assume tha you have to calculate the solution of following IVP problem: 05'9''5''' =+++ yyyy , with 2)0( =y , 3)0(' −=y , 4)0('' =y Introducing the axiliary variables y1 = y , y2 = y' , y3 = y'' , we get the following equivalent differential system           ⋅           −−− =           3 2 1 3 2 1 595 100 010 ' ' ' y y y y y y with      = −= = 4)0( 3)0( 2)0( 3 2 1 y y y Observe that the last row of the matrix contains the coefficients of the given ODE with the opposite sign; besides of that, it has only all "1" in the upper subdiagonal. Insert the initial values in the first row (range B10:D10). The column "x" was added only for clarity but it is not indispensable at all.Select the range B11:D40, where you want to otput the results and insert the function ODE_SYSL giving the suitable parameters: A, y0, h. Then press CTRL+SHIFT+ENTER The selected area will be filled with the numerical solution of the given system 0 0.5 1 1.5 2 2.5 0 1 2 3 4 5 6 y1 Exact Observe that the solution y1(x) is also the solution y(x) of the given ODE. Compare with the exact solution xeexy xx cos)( 2−− += Of course the above differential system can be also solved with other methods. In order to show the accuracy of the exponential method we put in a graph the average relative error e(x) = |yi - y(xi)| / | y(xi)| , obtained in the same condition, with 3 different methods: Exponential, Euler, and Runge-Kutta 4. The graph is eloquent. The error of the Exponetial method is several times more accurate then the others 1E-16 1E-14 1E-12 1E-10 1E-08 1E-06 0.0001 0.01 1 100 0 1 2 3 4 5 6 Euler RK 4 Expo Clearly it takes advantages using dedicated methods for linear differential equations. Another important feature of the exponential method is its high stability Let' s try the following test stiff system    −−= = 212 21 2120 ' yyy yy    −= = 6770293.0)4.0( 6706555.0)4.0( 2 1 y y
  • 230. Xnumbers Tutorial 229 The exact solution is     −−= += −− −− xx xx eey eey 20 2 20 1 20 In the following graph we shows the numerical result obtained with three different integration methods, Exponential, Runge Kutta of 4th order and Euler, using different integration steps The Expo method reaches the integration length of about x = 19 with a step h = 0.4 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 0 5 10 15 20 Expo method h = 0.42 The Runge-Kutta method, using a step h = 0.14 gives accurate only for x < 3; after that the solution begins to diverge. -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 0 5 10 15 20 Runge Kutta 4 h = 0.14 The Euler method, with a step h = 0.11, begins to oscillate even from the first steps. At this step the Euler method is completely unstable. -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 0 5 10 15 20 Euler method h = 0.11 As we can see the stability step of the Exponential method is here about 3 times greater than the others methods.
  • 231. Xnumbers Tutorial 230 ODE for integral function solving The integral function y(x) that we want to calculate is defined as Taking the derivatives of both sides and remembering Leibniz's rule, we have ∫= x x dxxfxy 0 )()( ⇔ )()(' xfxy = , 0)( 0 =xy As we can see, the computing of any integral function is equivalent of solving an ODE. Example. Plot the integral function of x exxf − =)( Insert in the cell A1 the expression y' = x*exp(-x) Choose a suitable step, for example h = 0.15. Select the range A7: B54 and insert the function ODE_RK4 with crtl+shift+enter In the adjacent column C we insert the integration function f(x) = x*exp(-x) Now select all the range A7:C54 and plot together. The result is shown in the following graph 0 0.2 0.4 0.6 0.8 1 1.2 0 2 4 6 8 f(x) ∫f(x) Comparing with the exact integral function x exxy − +−= )1(1)( , we have obtained a good solution with an average error of about 5E-7. For this example we have taken about 50 points. Remark. This method works fine when the function f(x) is smooth together with its derivative and has no singularity in the integration range. Otherwise we have to choose some quadrature algorithm as the double exponential method (see chap Integration for further details)
  • 232. Xnumbers Tutorial 231 Example. Solve the following linear differential equation of 3rd order. 029'33''5''' =+++ yyyy with 2)0( =y , 3)0(' −=y , 20)0('' −=y This linear equation with constant terms can be transformed into the equivalent differential system:           ⋅           −−− =           3 2 1 3 2 1 53329 100 010 ' ' ' y y y y y y with      −= −= = 20)0( 3)0( 2)0( 3 2 1 y y y Solving numerically this differential system is very rapid. Simply write the system matrix A in the worksheet, for example, in the range B5:D7. Insert the row starting values, [2,-3,-20] in the range B10:D10, where you want to begin the functions tabulation. Choose a suitable step that you like, for example h = 0.05 Then, select the range B11:D60 and insert the function =ODE_SYSL(B5:D7;B10:D10;B3) And give the ctrl+shift+enter key sequence. The selected cells will be automatically filled with the calculated solutions. Compare with the exact solution )5cos(2 xeey xx −− += The following graph shows the exact solutions (continue lines) compared with the calculated solutions (dotted lines). We note a global good fitting. -5 -4 -3 -2 -1 0 1 2 3 4 5 0 0.5 1 1.5 2 2.5 y1 y2 y3
  • 233. Xnumbers Tutorial 232 Macro ODE Solver This macro performs the numerical integration of ordinary differential equations systems (ODE) using the Runge-Kutta-Fehlberg method of 4th order. It is useful for solving ODE with initial values (Cauchy problem) ),(' yxfy = , 00 )( yxy = max0 xxx ≤≤ as well an ODE system ),(' yfy x= , 00 )( yy =x max0 xxx ≤≤ where [ ]T nyyy ..., 21=y is the vector of depend variables The macro works with equation definited by worksheet formulas (on site) or even with equation definited by symbolic string. The macro implements a control of the local error and changes the integration step consequently Therefore the precision can be set independently from the tabulating step. The independence of the integration step from the tabulation step is the main feature of this macro. The macro implement a predefined simple input schema. Let's see how to use it with a simple example Example 2. Find a numerical solution of this simple differential system    −−= = 212 21 54' ' yyy yy with    = = 2)0( 0)0( 2 1 y y and with 50 ≤≤ x Arrange the worksheet reserving two columns for [y1 , y2 ] and the two adjacent columns for the derivatives [y1', y2']. In the first column set a x-grid with 50 step of increment ∆x = 0.1. Then select the area A4:E55 and start the macro ODE Solver. Differential Equations links to the cells where are the equation formulas: = C5, = - (4*B5+5*C5) . Variables are the cells (x, y1, y2) referenced by the formulas; in that case A4:C5 Starting values indicate the starting cells of the table (they may or not may coincide with the variables cells). If we have followed the above schema, the input fields are correctly filled. We have only to set the required precision: Err. Max = 1E-6 Optionally, if we want the macro to fill also the derivative columns, activate the check box Press "Run" to start the RKF integration algorithm. After few seconds and about 1400 function evaluations, the macro output its result. Each solution node has a relative error less then 1E-6. Compare it with the exact solution
  • 234. Xnumbers Tutorial 233 xx eey 4 1 −− −= xx eey 4 2 4 −− +−= The following graphs shows the exact solution (pink line) and the calculated solution (dotted line) Solution y1(x) Solution y2(x) Note the general good fitting. It is a remarkable result overall if we consider the relative few points in the initial range where both functions have high swinging. For further details see " ODE tutorial" by Foxes Team, 2006 Macro ODE - Slope Grid This macro generate and visualize the slope y'(x) of a 1st ODE solution over a rectangular domain. It is didactically useful for studying the 1st order differential equation ),(' yxfy = For example we have to study the following equation x eyy − =+' ⇒ yxey x −= − ' Therefore we have to plot the slope given by the bivariate function yeyxf x −= − ),( in a suitable rectangular domain, for example in D = {-2 ≤ x ≤ 2 , {-2 ≤ y ≤ 2 } For using this macro, set the variables x and y in adjacent cells, for example I4 and J4. Then, in the adjacent right cell K4, insert the formula defining y'. thus: = EXP(-I4)-J4 Select the range I4:K4 and start the macro ODE Slope Field Option Tab The Grid number, from 4 to 24, set the density of the grid. The dots check box adds the grid points to the plot. The axes check box adds the x-y axes
  • 235. Xnumbers Tutorial 234 In the Axes tab we can set the domain D. Click the "Draw" button to generate the plot The macro generates a graphical object showing the slope y' of each point of the grid. Every solution y(x) of the differential equation follows the direction of the slope field. Therefore the field gives a global view of the solutions crossing the given domain. For example, the solution crossing the point (0,0) is xey x− = ( red line) We can see that this solution evolves following the slope directions. In this case, it never crosses the slope in any point. Other interesting examples xyy 2' −= 2 1' yxy −=
  • 236. Xnumbers Tutorial 235 Nonlinear Equations Bisection =Zero_bisec(a, b, func, [step], [param]) Approximates the zero of a monovariable function f(x) with the bisection method 0)( =xf This function needs two starting points [a, b] bracketing the zero. Parameter "func" is a math expression string containing the symbolic function f(x) Examples of correct function definitions are: -2*ln(x) , 2*cos(x)-x , 3*x^2-10*exp(-4*x) , etc. The optional parameter "step" sets the maximum number of steps allowed. If omitted the function iterates still the convergence. Step = 1 is useful to study the method step-by-step Param contains labels and values for parameters substitution (if there are) At the first step, the function returns a new segment [ a1, b1] where a1 < x0 < b1 At the second step, the function return a new segment [ a2, b2] where a1< a2 < x0 < b2 < b1. After several iterations, the interval [an , bn ], with n>>1, will be very closed to the value x0 Example: Find the approximated zero of the following equation and show the first steps of the bisection method. 05 )(log 2 )(log3 10 10 =−+⋅ x x The plot indicates two zeros: one trivial x = 10 and another in the interval 2 < x < 9 Starting the algorithm with a = 2 and b = 9 we get x0 = 4.64158883361278 The root approximates the exact zero x0 = 1001/3 with an error < 1E-14
  • 237. Xnumbers Tutorial 236 We can also solve this equation step-by-step in order to investigate how this algorithm works As we can see, the convergence is quite low but very robust because the zero always remains bracketed between the interval limits [a, b]. The error estimation is also very quick. Simply taking the difference |b-a| Secant =Zero_sec(a, b, func, [step], [param], [DgtMax]) Approximates the zero of a monovariable function f(x) with the secant method 0)( =xf This function needs two starting points [a, b] bracketing the zero. This version accepts also a worksheet function. The parameter "Func" is a math string containing the symbolic function f(x) or a range of two adjacent cells containing the worksheet functions [ f(a) , f(b) ] (see example). Examples of correct symbolic function definitions are: -2*ln(x) , 2*cos(x)-x , 3*x^2-10*exp(-4*x) , etc. The optional parameter "Step" sets the maximum number of iterations. If omitted, the function iterates still the convergence. Step = 1 is useful for investigation about the algorithm. "Param" contains labels and values for parameters substitution (if there are) Both parameters "step" and "param" are ignored if funct is not a symbolic function. The optional parameter "DgtMax" set the number of the precision digits. If omitted, the function works in the faster double precision. At the first step, the function returns a new segment [ a1, b1] where a1 < x0 < b1 At the second step, the function return a new segment [ a2, b2] where a1< a2 < x0 < b2 < b1. After several iterations, the interval [an , bn ], with n>>1, will be very closed to the value x0 Use the CTRL+SHIFT+ENTER sequence to paste this function Example: Find the approximated zero of the following equation and show the first steps of the secant method. 0)sin())2ln(3exp( =⋅−⋅− xx π The plot indicates one zeros into the interval 0 < x < 0.5
  • 238. Xnumbers Tutorial 237 -1 -0.5 0 0.5 1 1.5 0 0.2 0.4 0.6 0.8 1 exp(-6*x*Ln(2))-sin(pi*x) Starting the algorithm with a = 0 and b = 0.5 we get x0 = 0.166666666666667 The root approximates the exact zero x0 = 1/6 with error < 1E-15 Let’s see now the iteration trace setting the parameter step = 1 As we can see this convergence is much faster than the one of the bisection method. On the other hand, it is no guaranteed that the zero always remains bracketed into the interval. Zero finder for worksheet function Example we want to find the zero of the function f(x) = x − cos(x) , using the standard worksheet function = COS(x). Arrange a worksheet like the following
  • 239. Xnumbers Tutorial 238 Insert in A3:B3 the array function zero_sec(a, b, func) using the sequence Ctrl+Shift+Enter In this case, none parameter is necessary because the function always returns one step of the secant algorithm. Now insert the functions in the range C3:D3 by simply dragging-down the range C2:D2 Insert the function =ABS(A3-B3) in E3. This is not necessary but it is usefull for checking the convergence. Now, let's select the last row A3:E3 and drag it down until the convergence shows the minimal error We can compute this result also in multiprecision. In this case we have use, of course, the equivalent x-functions; in cell D2 = xsub(A4, xcos(A4)), E2 = xsub(B4, xcos(B4)) and in cell E3 = ABS(xsub(A5, B5)). In the range A3:B3 insert the array function {Zero_sec(A4, B4, C4:D4, , , 30)}. The parameter DgtMax = 30 switches the function in multiprecision mode Following the other steps we get the result below As we can see, the final value is x = 0.739085133215160641655312087673 that is the zero of the function f(x) = x − cos(x) with 30 significant digits
  • 240. Xnumbers Tutorial 239 Derivatives First Derivative =Diff1(x, func, [lim], [param]) Approximates the first derivative of an univariate function f(x) at the given point x )()(' xf dx d xf = The parameter “func” is a math expression string containing the symbolic function f(x) Examples of function definition are: -2*Ln(x) , 2*cos(x) , 3*x^2-10*exp(-4*x) , x^2+4*x+1 , etc. The optional parameter “lim” (default = 0) sets the way how the limit approach to x. If lim = 1, it approaches from the right; if lim = -1, it approaches from the left; if lim = 0 , it approaches centrally. That is, it returns the following derivatives        = = = = + +→ → − −→ )(')(lim )(')(lim )(')(lim )(' 0 0 0 xfxf xfxf xfxf xf h h h "Param" contains labels and values for parameters substitution (if there are) This function uses the following formulas to approximate each derivative ( ))4(3)3(16)2(36)(48)(25 12 1 )(' hxfhxfhxfhxfxf h xf −+−−−+−−≅− ( ))2()(8)(8)2( 12 1 )(' hxfhxfhxfhxf h xf +−++−−−≅ ( ))4(3)3(16)2(36)(48)(25 12 1 )(' hxfhxfhxfhxfxf h xf +++−+++−≅+ Example. Evaluate numerically the left, right and central derivatives of the given function at the point x = 0, and check if the given function is differentiable in that point 1|| )( 2 ++ = xx x xf As we can see all derivatives are equal, so the function is differentiable in x = 0
  • 241. Xnumbers Tutorial 240 Second Derivative =Diff2(x, func, [param]) It approximates the second derivative of an univariate function f(x) at the given point )()('' 2 2 xf dx d xf = The parameter "func" is a math expression string containing the symbolic function f(x) Examples of function definition are: -2*Ln(x) , 2*cos(x) , 3*x^2-10*exp(-4*x) , x^2+4*x+1 , etc. "param" contains labels and values for parameters substitution (if there are) Example: Evaluate the first and second derivatives at the point x = 2 for the following function 1 3 )( 2 + + = x x xf Gradient =Grad(p, func, [param]) Approximates the gradient of a multivariate function f(x, y, z) at the given point       ∂ ∂ ∂ ∂ ∂ ∂ =∇ z f y f x f zyxf ,,),,( The parameter "p" is the vector of the variables [x, y, z] The parameter "func" is an expression string containing the function f(x, y, z). Examples of function definition are: -2*ln(x+3y), 2*exp(-x)*cos(3*t), 3*x^2-y^2+z^2, (x^2+y^2)^(1/3),etc. For performance problem, the number of variables is restricted to 4, “x”, “y”, “z”, “t”. The variables values must be always passed in this order. "param" contains labels and values for parameters substitution (if there are) Example. Evaluate the gradient of the following function at the point P(1, 1) 22 5 1 ),( yx yxf + =
  • 242. Xnumbers Tutorial 241 Jacobian matrix =Jacobian (p, func, [param]) Approximates the Jacobian’s matrix of a multivariate vector-function F(x, y, z) at the given point (x, y, z)           = ),,( ),,( ),,( ),,( 3 2 1 zyxf zyxf zyxf zyxF                   ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ = z f y f x f z f y f x f z f y f x f zyxJ 333 222 111 ,, ,, ,, ),,( The parameter "p" is the vector of the variables [x, y, z] The parameter "func" is an expression string containing the function f(x, y, z). Examples of function definition are: -2*ln(x+3y), 2*exp(-x)*cos(3*t), 3*x^2-y^2+z^2, (x^2+y^2)^(1/3),etc. For performance problem, the number of variables is restricted to 4, “x”, “y”, “z”, “t”. The variables values must be always passed in this order. "param" contains labels and values for parameters substitution (if there are) Example. Evaluate the Jacobian’s matrix of the following vector-function at the point P(1, 1) 2221 5 1 ),,( zyx zyxf ++ = )2ln(),,(2 yxzzyxf +⋅= xyzzyxf 4),,(3 = Hessian matrix =Hessian (p, func, [param]) Approximates the Hessian’ matrix of a multivariate function f(x, y) at the given point p(x, y)             ∂ ∂ ∂∂ ∂ ∂∂ ∂ ∂ ∂ = 2 22 2 2 2 , , ),( y f xy f yx f x f yxH The parameter "p" is the vector of the variables [x, y] The parameter "Func" is an expression string containing the function f(x, y, z). Examples of function definition are: -2*ln(x+3y), 2*exp(-x)*cos(3*t), 3*x^2-y^2+z^2, (x^2+y^2)^(1/3), etc.
  • 243. Xnumbers Tutorial 242 For performance problem, the number of variables is restricted to 4, “x”, “y”, “z”, “t”. The variables values must be always passed in this order. "Param" contains labels and values for parameters substitution (if there are) This function returns a square a matrix (n x n) of the second derivatives The accuracy of this function is about 1E-10 Example. Approx. the Hessian’s matrix of the following function at the point (2,1,1) 222 5 1 ),,( zyx zyxf ++ = Parameters. All the differential functions accept parameters into the string definition. They must be different from the canonical variables "x", "y", "z", "t" but apart this, can have any name that you like. Example: Calculate the Hessian matrix of the function ayx zk zyxf ++ ⋅ = 22 ),,( for (x, y, z) = (1, -3, 4) and for parameters k = 1.5 and a = -1 Note the "param" range G2:H3: the parameters must be always passed with theirs labels "k" and "a". On the contrary the variables do not need labels but must always be passed in the order x, y, z. Using the trick of the scaling for 243, the inverse of the determinant, we get an integer matrix, that represent the numerator of the Hessian itself. Then, simplifying each element for 243, we obtain the Hessian matrix in the following exact fractional form
  • 244. Xnumbers Tutorial 243 Non-linear equation solving with derivatives Derivatives play a strategic role in solving non-linear equation and non-linear system The most efficient algorithms use the derivatives information in order to speed up the convergence to the final solution. From the point of view of numeric calculus, derivatives are rarely used because they tend to magnify the truncation error. This is general true and a many rootfinding algorithm avoid the derivatives. In solving non-linear problem, however, the derivatives can be very useful because they can greatly improve the convergence without influence the final result accuracy, that depends only by the evaluation function f(x) Let's see and example. Solve the following equation xx sin2 = with an accuracy better then 1e-25. First of all we build the function xxxf sin)( 2 −= and draw its plot. The point x where 0)( =xf is the solution of the given equation We see that the zero exists and it is near the point 1. We note also that in the interval [0.5 1.5] the function is monotonic In this interval the Newton-Raphson iterative algorithm, starting from x = 1.5, should work fine. ...21,0,n )(' )( n n n1n =−=+ xf xf xx To implement this algorithm we need the evaluation function f(x) with about 30 significant digits. For that, it comes in handy the multiprecision function xeval. For the derivative we have to way: computing the function f'(x) by hand and evaluating it by xeval or approximating the derivative by the function diff1 in standard precision. Because we are a bit lazy and the derivatives is not so immediate, we chose the second way. A simple spreadsheet arrangement may be the following As we can see the convergence is superb!. After few iteration the solution is x ≅ 0.9496166887146629471509830317 with |f(x)| < 1e-28 This excellent result has been obtained in spite of the limited precision (1e-13) of the derivative. The reason is simple: the accuracy of the derivative does not influence the final accuracy of the root. We note that the derivative, after very few iterations, remains constant: we might substitute this value with an even more approximated values, i.e. f' = 1.57, for all iterations. The final accuracy will not change. We will need only more few steps, at the most.
  • 245. Xnumbers Tutorial 244 But this method show its power overall for non-linear systems. For a 2 variables problem the Newton-Raphson method becomes    = = 0),( 0),( yxg yxf ⇒ n 1 n1n1n             −      =      − ++ g f gg ff y x y x yx yx The (2 x 2) matrix is the Jacobian calculated at the point (xn, yn). In Xnumbers it can be evaluated by the function Jacobian Example. Solve the following system     =−++ =−−+− − 01 0625 yxe xyx yx setting: 6),( 25 −−+−= xyxyxf 1),( −++= − yxeyxg yx the contour plots of the functions f = 0 and g = 0 show two intersection points: one near the point (-1 2) and another near (-1, -2) The function f(x,y) and g(x,y) are evaluated and converted in double precision by the nested functions =xcdbl(xeval(B4,B7:B8)) =xcdbl(xeval(B5,B7:B8)) At the begin, insert the starting point (-1, 2) in the cells B7:B8. The new point is calculated in the cells E7:E8. Copy this range and re- insert in the range B7:B8. At each iteration the increments dx,dy of the range E10:E11 becomes more and more small. Starting from (-1, 2) and (-1, -2) the iteration algorithm converges to the correct solutions x y x y -1 2 -1 -2 -1.0201151219 1.9698273171 -0.7964138633 -2.3053792051 -1.0196483063 1.9693084022 -0.8079928505 -2.2042117521 -1.0196480758 1.9693081215 -0.8107932120 -2.1997452584 -1.0196480758 1.9693081215 -0.8108021826 -2.1997248438
  • 246. Xnumbers Tutorial 245 Conversions Decibel =dBel(A, [MinLevel]) Converts a positive number A into decibel ( )AA 10dB log20= If zero, A is substituted with the value contained in the parameter "MinLevel" (default 1E-15) Example A A dB 1 0 0.5 -6.0206 0.1 -20 0.05 -26.021 0.01 -40 0.001 -60 0.0001 -80 0 -300 Base conversion cvDecBin(DecNum) base 10 ⇒ base 2 cvBinDec(BinNum) base 2 ⇒ base 10 cvDecBase(DecNum, Base) base 10 ⇒ any base (2-16) cvBaseDec(BaseNum, Base) any base (2-16) ⇒ base 10 baseChange(number, old_base, new_base) 1 any base (2 - 36) ⇒ any base (2 - 36) xBaseChange 2 Any base (2 .. >1 million) These functions perform the number conversion between different bases. Example: Converts the decimal number n = 902023485 into bases 2 and 3. cvDecBin(902023485) = 110101110000111100100100111101 (base 2) cvDecBase(902023485, 3 )= 2022212022112121020 (base 3) Example: Converts the hexadecimal number n = 35CFFF3D into decimal cvBaseDec(35CFFF3D) = 902823741 (base 10) You can also convert directly base-to-base, nesting two functions. Example convert n = 35CFFF3D from base 16 into 8 cvDecBase(cvBaseDec(35CFFF3D,16),8) = 6563777475 (base 8) For this scope you can also use the baseChange function 1 The function baseChange appears thanks to Richard Huxtable 2 The function xBaseChange appears thanks to Ton Jeursen
  • 247. Xnumbers Tutorial 246 In spite of its digits limitation (15), this function has several interesting features It converts any number into many different bases (up to 36). The digits greater then 9 are indicated as A, B, C, D E, F, G, H, etc. It converts also decimal numbers. It formats the result consistently with the source cell. Let's see how it works The cell C4 is formatted with 7 digits and consequently its results have the same format; the cell D4 is formatted with 2 decimals and its result has the same format. Multiprecision Base Conversion = xBaseChange(xBC_OldNum, [xBC_NewBase], [deep]) This function1 converts a number from any base (2 .. >1 million) xBC_OldNum is the number to convert in the following formats 3642455[7] (integer number in base 7) 10C2AE[16] (integer number in base 16) 1098414 (integer number in base 10) 213.02[4] (decimal number in base 4) The oldnumber is assumed to be base 10; otherwise oldbase is provided between square brackets []. xBC_NewBase is the new base. If omitted it is assumed 10 as default. If the base > 35 the letters are no more sufficients and the digits are represented as group of digits separate with colon : 9:34:9:23[36] (integer number in base 36) 6:54:122:42:65[200] (integer number in base 200) 100:368.356:980[1000] (decimal number in base 1000) deep is an optional parameter (0 to 50) setting the max number of decimals showed (default 50) Example: xBaseChange("9:34:9:23[36]", 7) Results in: 3642455[7] Example: xBaseChange(1098414 , 16) Results in: 10C2AE[16] 1 The function xBaseChange appears thanks to Ton Jeursen.
  • 248. Xnumbers Tutorial 247 Example: xBaseChange(1098414 , 46) Results in: 11:13:4:26[46] Example: xBaseChange("1F0C2AE[16]") Results in: 32555694[10] Example xBaseChange(10092.102 , 2048 , 6) Results in 4:1900.208:1835:16:786:884:1507...[2048] Note that, as in the last case, the digits groups can have different lenght: one , two, or more digits If we wan to force all the groups having the same number of digits simply insert the new base with prefix "0" Example xBaseChange(10092.102 , "02048" , 6) Results in 0004:1900.0208:1835:0016:0786:0884:1507...[02048] Log Relative Error = mjkLRE(q, c, NoSD) = xLRE(q, c, NoSD, [DgtMax]) This function1 returns the log relative error (LRE) for an estimated value (q) and a certified value (c), which has a specified number of significant digits (NoSD). The LRE is a measure of the number of correct significant digits only when the estimated value is “close” to the exact value. Therefore, each estimated quantity must be compared to its certified value to make sure that they differ by a factor of less than two, otherwise the LRE for the estimated quantity is zero. Definition. The base-10 logarithm of the relative error is defined as:    <−= >= = 1|q|),)log(min(LRE 1|q|if0LRE 0cif NoSDq      = == ≤≤≠−−= ≠ lseif0LRE qcifLRE 2|q/c|1/2andqcif),|)|/|log(|min(LRE 0cif e NoSD NoSDccq Example: Assume that you want to compare an approximate value with a 15 digits certified value of π. LRE metric can show this in a easy way Certified value C = 3.14159265358979 Approx. value Q = 3.14159265300001 mjkLRE(C, Q, 15) = 9.7 This means that two values are close for about 10 significant digits. LRE metric rejects non significant digits. Look at this example: Certified value C = 0.000133333333333333 1 These functions appear by courtesy of Michael J. Kozluk. This algorithm was first programmed into an Excel user function, by Michael, in standard 32 bit precision. As it works fine also for comparing long extended numbers (NoSD> 15), we have now developed its multiprecision version xLRE().
  • 249. Xnumbers Tutorial 248 Approx. value Q = 0.000133333333333311 mjkLRE(C, Q, 15) = 12.8 The two numbers appear exact up to the 17th digit, but the relative error is about 1E-13 LRE is very useful when you work with long string of extended numbers. For example, compare this approximation of "e" (Napier’s number) Certified value C = 2.71828182845904523536028747111 Approx. value Q = 2.71828182845904523536028747135 xLRE(C, Q, 30) = 28.1 At the first sight it is hard to say, but the LRE function shows immediately a precision of about 28 digits
  • 250. Xnumbers Tutorial 249 Special Functions The computation of special functions is a fundamental aspect of numerical analysis in virtually all areas of engineering and the physical sciences. All these special functions have a high-fixed-precision. Because most of these special functions are in the form of infinite series or infinite integrals, their numeric solutions are quite complicated, and we have spent many times for selecting and testing many different algorithms in order to achieve the highest possible accuracy in 32 bit arithmetic. Error Function Erf(x) errfun(x) Returns the error function ∫ − = x t dtexerf 0 22 )( π Accuracy: about 1E-14 per x > 0 Exponential integral Ei(x) exp_integr(x) Returns the exponential integral ∫ ∞ − − −= x t dt t e xEi )( Accuracy: about 1E-14 for any x ≠ 0 This version1 accept also negative argument For definition is 0)()( 1 >=− xxExEi Exponential integral En(x) expn_integr(x, n) Returns the exponential integral of n-th order dt t e xE n xt ∫ ∞ − −= 1 n )( Accuracy: about 1E-14 for x> 0 and n > 0 1 This extension appears thanks to the courtesy of Hans Günter
  • 251. Xnumbers Tutorial 250 Euler-Mascheroni Constant γ xGm([Digit_Max]) Returns the Euler-Mascheroni gamma constant. The optional parameter Digit_Max sets the maximum digits (default 30, max 415)       −+++= ∞→ )log( 1 ... 3 1 2 1 1lim n nn γ Example: compute the gamma constant with 40 significant digits xGm(40) = 0.5772156649015328606084804798767149086546 Gamma function Γ(x) xGamma(x, [Prec]) Returns the gamma function with precision up to 30 digits. Definition: ∫ ∞ −− =Γ 0 1 )( dtetx tx Optional parameter Prec sets the maximun precision from 15 (default) to 30 digits When Prec = 15 the routine works in faster double arithmetic. This routine uses the excellent Lanczos series approximation1       + +      ++ ≅Γ ∑= + N i i x g ix c c e gx e x 1 0 2 1 2 1 2 )( π where: g = 607/128, ci are the Lanczos' coefficients and 14 < N < 22. Relative accuracy in double precision is better than 10^-14, (except very near to the poles x = 0, -1. -2, -3, etc.). In extended precision the accuracy is better then 1E-30 xGamma(5.1) = 27.9317537383684 xGamma(5.1, 30) = 27.9317537383683833586731052773 This function works also with large argument because it uses the multiprecision format to avoid the overflow for arguments greater than 170. Example, x xgamma(x) Rel. Error 0.0001 9.99942288323162E+3 1.00E-15 0.001 9.99423772484596E+2 1.02E-15 0.01 9.94325851191507E+1 1.00E-15 0.1 9.51350769866874 9.33E-16 1 1 0 10 3.6288E+5 0 100 9.33262154439441E+155 5.64E-16 1 This high accuracy algorithm has been extracted form a very good note by Paul Godfrey, Intersil , C.2001
  • 252. Xnumbers Tutorial 251 1,000 4.02387260077093E+2564 1.92E-15 10,000 2.84625968091705E+35655 1.58E-15 100,000 2.82422940796034E+456568 2.75E-15 1,000,000 8.26393168833122E+5565702 2.54E-15 Note that relative accuracy is better than 5E-15 in any case. You can convert in double only the values with x ≤ 170, otherwise you will get #VALUE! (error). You can manipulate these large values only by the "x-functions", or, separating mantissa and exponent (see xsplit()) FACTORIAL: Thanks to its efficence and accuracy, this function can also be used to calculate the factorial of a big integer number, using the relation n! = Γ(n+1) Example: xfact(10002) = 2.84711361574652325360317551421E+35667 30 digits, slower xgamma(10003) = 2.84711361574651E+35667 15 digits, faster Log Gamma function xGammaln(x) xGammalog(x) These function return the natural and decimal logarithm of the gamma function. xgammaln(100000) = 1051287.7089736568948 xgammalog(100000) = 456568.45089997090835 Relative accuracy is better than 10^-(14+|log(x)|) for x>0 These functions are added only for compatibility with Excel and other math packages. They are useful to avoid overflow in standard precision arithmetic for large arguments of gamma function. However if you use directly the xgamma() and multiprecision arithmetic, you need no more to use these functions. Gamma quotient xGammaQ(x1, x2) Performs the division of two gamma functions. q = Γ(x1) / Γ(x2) Relative accuracy is better than 1E-14, for x1 > 0 and x2 > 0 Example: suppose you have to calculate for v =1,000,000 the following quotient )( )( 2 2 1 v v q Γ Γ = + Taking x1 = 500,000.5 and x2 = 500,000 , we have easily xgammaq(500000.5 , 500000) = 707.106604409874 (rel error = 5.96E-16 ) Note that if you have used the standard GAMMALN() function, you should have:
  • 253. Xnumbers Tutorial 252 EXP(GAMMALEN(500000.5) - GAMMALEN(500000)) = 707.106604681849 (with a rel. error = 3.846E-10) As we can see, In this case, the error is more than 500,000 times bigger that the previous one! Gamma F-factor xGammaF(x1, x2) Returns the gamma factor of the Fischer distribution.       Γ⋅      Γ       + Γ = 22 2 21 21 xx xx k Relative accuracy is better than 1E-14, for x1 > 0 and x2 > 0 Digamma function digamma(x) Returns the logarithmic derivative of the gamma function ( ) )( )(' )(ln)( x x x dx d x Γ Γ =Γ=Ψ Relative accuracy is better than 1E-14, for x > 0 Example digamma(x) value rel. error 0.01 -100.560885457869 3.24E-15 0.1 -10.4237549404111 2.23E-15 1 -0.577215664901532 1.49E-15 10 2.25175258906672 4.92E-16 100 4.60016185273809 5.65E-16 1000 6.90725519564881 2.97E-16 Note that Ψ(1) = − γ (Eulero- constant) Beta function xBeta(a, b) Returns the beta function ∫ −− −= 1 0 11 )1(),( dtttbaB ba Relative accuracy is better than 1E-14, for a > 0 and b > 0
  • 254. Xnumbers Tutorial 253 Incomplete Gamma function xGammaI(a, x, [sel]) Returns one of the following Incomplete Gamma functions specified by the "sel" parameter (default 1) Sel =1 Sel =2 Sel = 3 Sel = 4 (Tricomi) ∫ −− = x ta dtetxa 0 1 ),(γ ∫ ∞ −− =Γ x ta dtetxa ),( 1 )( ),( ),( a xa xaP Γ = γ )( ),( ),(* ax xa xa a Γ = γ γ Relative accuracy is better than 1E-14 for : a > 0, 0≥x Incomplete Beta function xBetaI(x, a, b, [sel]) Returns one of the following Incomplete Beta functions specified by the "sel" parameter (default 1) Sel = 1 Sel = 2 ∫ −− −= x ba x dtttbaB 0 11 )1(),( ),( ),( ),( baB baB baI x x = where B(a, b) is the Beta function Relative accuracy is better than 1E-14 for a > 0, b > 0. 10 ≤≤ x . Combinations function xcomb_big(n, k) Returns the combination, or binomial coefficients, for large integer numbers !)!( ! , nkn n k n C kn − =      = Relative accuracy is better than 1E-14, for n >> 0 and k >> 0 This function uses the gamma function to calculate the factorials. It is much faster than xcomb function. For this reason is adapted for large integer values (10,000 - 1,000,000) xcomb(5000,2493) = 1.5627920156854189438574778889E+1503 (30 digits, slow) xcomb_big(5000,2493) = 1.56279201568542E+1503 (15 digits , fast)
  • 255. Xnumbers Tutorial 254 Bessel functions of integer order BesselJ (x, [n]) Bessel function of 1° kind, order n: Jn(x) BesselY (x, [n]) Bessel function of 2°kind, order n: Yn(x) BesseldJ (x, [n]) First derivative of Bessel functions of 1° kind, order n: J'n(x) BesseldY (x, [n]) First derivative of Bessel functions of 2° kind, order n: Y'n(x) BesselI (x, [n]) Modified Bessel function of 1° kind, order n: In(x) BesselK (x, [n]) Modified Bessel function of 2°kind, order n: Kn(x) BesseldI (x, [n]) First derivative of mod. Bessel functions of 1° kind, order n: I'n(x) BesseldK (x, [n]) First derivative of mod. Bessel functions of 2° kind, order n: K'n(x) Relative accuracy is better than 1E-13, for x > 0 and n any integer These routines1 have a high general accuracy. Look at the following example. We have compared results obtained from our BesselJ with the standard Excel similar function x J0(x) (BesselJ) Rel. Error J0(x) (Excel standard) Rel. Error 0.1 0.997501562066040 1.11E-16 0.997501564770017 2.71E-09 0.5 0.938469807240813 1.06E-15 0.938469807423541 1.95E-10 1 0.765197686557967 7.25E-16 0.765197683754859 3.66E-09 5 -0.177596771314338 2.66E-15 -0.177596774112343 1.58E-08 10 -0.245935764451374 1.06E-13 -0.245935764384446 2.72E-10 50 0.055812327669252 3.98E-15 0.055812327598901 1.26E-09 As we can se, the general accuracy is better than 200,000 times! Cosine Integral Ci(x) CosIntegral(x) Returns the Cosine integral defined as: dt t t x x )cos( )(ci ∫ ∞ −= Relative accuracy is better than 1E-13, for x > 0 Sine Integral Si(x) SinIntegral(x) Returns the sine integral defined as: dt t tsin x x )( )(si 0 ∫= Relative accuracy is better than 1E-13, for x > 0 1 All these special functions are provided thanks to the FORTRAN 77 Routines Library for Computation of Special Functions developed by Shanjie Zhang and Jianming Jin . The programs and subroutines contained in this library are copyrighted. However, authors kindly gave permission to the user to incorporate any of these routines into his programs.
  • 256. Xnumbers Tutorial 255 Fresnel sine Integral Fresnel_sin(x) Returns the Fresnel's sine integral defined as: dttsinxS x )()( 2 0 2 1 π∫= Relative accuracy is better than 1E-13, for x > 0 Remember also the following relation ( )∫ ⋅=⋅ x zkSdttk 0 2 )(sin where: π 2 =k Fresnel cosine Integral Fresnel_cos(x) Returns the Fresnel's cosine integral defined as: dttxC x )cos()( 2 0 2 1 π∫= Relative accuracy is better than 1E-13, for x > 0 Remember also the following relation ( )∫ ⋅=⋅ x zkCdttk 0 2 )cos( where: π 2 =k Fibonacci numbers xFib(n, [DgtMax]) Returns the Fibonacci's numbers defined by the following recurrent formula: 2n1nn21 ,2,1 −− +=== FFFFF Example: xFib(136) = 11825896447871834976429068427 xFib(4000) = 3.99094734350044227920812480949E+835
  • 257. Xnumbers Tutorial 256 Hypergeometric function Hypgeom(a, b, c, x) Returns the Hypergeometric function The parameter "a" is real, "b" is real, "c" is real and different form 0, -1, -2, -3 ... The variable "x" is real with |x| < 1 Relative accuracy is better than 1E-14, for -1 < x < 1 The hypergeometric function is the solution of the so called Gaussian-hypergeometric differential equation ( ) ( )( ) 011 =+′++−+′′− yabyxbacyxx An integral form of the hypergeometric function is ( ) ( )∫ −−−− −− −ΓΓ Γ = 1 0 11 11 )()( )( ).,,( dtxttt bcb c xcbaF abcb More known is the series expansion that converges for !x| < 1 ... !6)2)(1( )2)(1()2)(1( !2)1( )1()1( !1 1).,,( 32 + ++ ++++ + + ++ ++= x ccc bbbaaax cc bbaax c ab xcbaF Special result are: p xxpF − −= )1(),1,1,( x x xF )1ln( ),2,1,1( + =− 6.1),,,( 32 27 6 5 3 2 3 1 =F Zeta function ζ(s) Zeta(s) The Riemann zeta function ζ(s) is an important special function of mathematics and physics which is intimately related with results surrounding the prime number, series, integrals, etc. Relative accuracy is better than 1E-14, for any s ≠ 1 It uses the fast Borwein formula. Definition. For |s|>1 the function is defined as: ∑ ∞ = = 1 1 )( k s k sζ Analytic continuation. It can be defined for 0< s <1 by the following analytic continuation: ∑ ∞ = − − − − = 1 1 1 )1( 21 1 )( k s k s k sζ For s< 0 the function is defined by the following relation: )()()cos()2(2)1( 2 1 ssss s ζππζ Γ=− −
  • 258. Xnumbers Tutorial 257 Same known exact results are: ζ(2) = (π2 ) /6 , ζ(4) = (π4 )/90 Zeta function is very useful in computing series. Look at this example: 4 5 )2( 2 1 1 11 )2( 1 2 1 2 2 2 0 2 −=−−== + ∑∑∑ ∞ = ∞ = ∞ = ζ kkk kkk So, the final result is (π2 )/6 - 5/4 Airy functions = AiryA(x) = AiryB(x) = AiryAD(x) = AiryBD(x) Return the Airy functions A(x), B(x) and theirs derivatives A'(x), B'(x) A(x) = ( )( )( )       ++Γ ∑ ∞ = 3 )1(2 sin3 ! 1 3 1 3/1 0 3 1 3/2 π π n x n n n n B(x) = ( )( )( )       ++Γ ∑ ∞ = 3 )1(2 sin3 ! 1 3 1 3/1 0 3 1 6/1 π π n x n n n n Variable x is real. Relative accuracy is better than 1E-14 Elliptic Integrals = IElliptic1(φ, k) = IElliptic2(φ, k) Return the Jacobian Elliptic Integrals of 1st and 2nd kind IElliptic1 = ∫ − = φ θ θ φ 0 22 sin1 ),( k d kF IElliptic2 = θθφ φ dkkE sin1),( 0 22 ∫ −= Variable φ is real, 0 < k < 1. Relative accuracy is better than 1E-14
  • 259. Xnumbers Tutorial 258 Kummer confluent hypergeometric functions = Kummer1(a, b, z) = Kummer2(a, b, z) They return the Kummer conflent hypergeometric functions of 1st and 2nd kind, also called M(a, b, z), and U(a, b, z) respectively. The confluent hypergeometric functions give the complete solutions of the confluent hypergeometric differential equation. 0)(2 2 =−−+ aw dz dw ab dz wd z having the complete solution ),,(),,( 21 zbaUqzbaMqw += The parameters "a" and "b" can be positive or negative but always different from 0, -1, -2, -3 ... The variable z must be positive for Kummer2 For a > 0 and b > 0, the confluent hypergeometrix functions have an integral representation (Abramowitz and Stegun 1972, p. 505) ∫ −−− − Γ−Γ Γ = 1 0 11 )1( )()( )( ),,( dttte bab b zbaM abazt ∫ ∞ −−−− + Γ = 0 11 )1( )( 1 ),,( dttte a zbaU abazt a b z M(a,b,z) U(a,b,z) 0.5 0.2 1 5.85803416181 0.70928623024 0.5 0.2 5 601.287114305 0.40255172241 0.5 0.2 10 112016.644576 0.29849708947 0.5 0.2 20 3063046479.13 0.21689337800 0.5 0.2 30 7.6396125E+13 0.17882728338 8.2 1.4 1 41.1797940694 1.39280467E-06 -0.9 0.4 10 -345.535973205 7.72952303674
  • 260. Xnumbers Tutorial 259 Integral of sine-cosine power = IntPowSin(n, x) = IntPowCos(n, x) These functions return the integral of sine and cosine raised to the n integer power ∫= x n dttnxSPI 0 )(sin),( ∫= x n dttnxCPI 0 )(cos),( Variable x ≥ 0 is real, n = 0, 1, 2, 3 ... is integer. Relative accuracy is better than 1E-15 -0.2 0 0.2 0.4 0.6 0.8 1 1.2 1.4 0 0.5 1 1.5 2 2.5 3 3.5 4 CPI(x) SPI(x) n = 8 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 -6 -4 -2 0 2 4 6 CPI(x) SPI(x) n = 8 These functions use the fast recursive algorithms )2,( 1)(sincos ),( 1 − − +−= − nxSPI n n n xx nxSPI n , xxSPI cos1)1,( −= , xxSPI =)0,( )2,( 1)(cossin ),( 1 − − += − nxCPI n n n xx nxCPI n , xxCPI sin)1,( = , xxCPI =)0,( The recursive formula of CPI(x, n) is very stable and accurate for all values x ≥ 0 , n ≥ 0, n ∈ N On the contrary, the recursive formula of SPI(x, n) degrades precision for small value of x In that case, the function automatically switches on the power expansion series       ++ + + = ∑∫ ∞ = + 1 21 0 121 1 )(sin k kkn x n y nk a n ydtt , 1<x where )2...(642 )12...(531 2 12 1 k k i i a k i k ⋅⋅ −⋅⋅ =      − = ∏= , xy sin=
  • 261. Xnumbers Tutorial 260 Spherical Bessel functions of integer order = BesselSphJ(x, [n]) = BesselSphY(x, [n]) These functions return the Spherical Bessel functions of 1st and 2nd kind of integer order n = 0, 1, 2... (default n = 0), and for any real x > 0, definited as: )( 2 )( 2 1 xJ x xj nn + = π Spherical Bessel functions of 1st kind )( 2 )( 2 1 xY x xy nn + = π Spherical Bessel functions of 2nd kind (also Neuman function) The Spherical Bessel functions are the solutions of the Helmholtz radial equation in spherical coordinates 0)(2 22 2 2 2 =−−++ ynnx dx dy x dx yd x n x jn(x) yn(x) -------------------------------------------------------------------------------- 0 0.5 0.958851077208406 -1.755165123780750 1 1 0.301168678939757 -1.381773290676040 2 3 0.298637497075734 -0.267038335264499
  • 262. Xnumbers Tutorial 261 Formulas Evaluation Multiprecision Expression Evaluation These functions realize a little math shell, putting together the power of multiprecision numeric computation with the ease of symbolic calculus. Sometime we may want to perform the computation using symbolic formulas. We would pass these strings to a routine for evaluation, returning the numerical results with a given accuracy. These functions perform this useful task. xeval( Formula, [Var], [DgtMax], [Angle],) xevall( Formula, [Var1, Var2 …] ) These functions return the evaluation of a math expression in multiprecision arithmetic. They use the same algorithm1 and have the same variable accuracy. They differ only for the input parameters. The parameter "Formula" is a math expression string containing variables, operators, parenthesis and other basic functions. Examples. 3+1/(x^2+y^2), sin(2*pi*t)+4*cos(2*pi*t), (x^4+2x^3+6x^2-12x-10)^(1/2) The optional parameter "Var" is an array containing one or more value for variables substitution. Before computing, the parser substitutes each symbolic variable with its correspondent value. It can be a single value, an array of values or, even an array of values + labels (see examples). The optional parameter "Var1", "Var2"... are single values or array as "Var" but without labels, because the function xevall automatic finds by itself the appropriate labels. (See example) The optional parameter "DgtMax" – from 1 to 200 - sets the maximum number of precision digits (default=30). Setting DgtMax = 0 will force the function to evaluate in faster standard precision. The optional parameter "Angle" sets the angle unit "RAD” (default) “DEG”, “GRAD”.of for trigonometric computation: Example: xeval("(1+sqr(2))/2+5^(1/3))") = 2.91708272786324451375395323463 xeval("(1+cos(x))/2+x^y" , {5, 1.2}) = 7.5404794000376872941836369067 xeval("(a+b)*(a-b)", {2, 3})= (2+3)*(2-3) = -5 All the function parameters can also be passed by cell references, like A1, $B$2, etc. Example. Tabulate the following function for x = 1, 1.5, 2, … with 30 significant digits 2 1 1 )( x x xf + + = 1 The algorithm is divided into two steps: parsing and evaluation. The first step is performed by the MathParser class. The evaluation is performed with the x-functions of XNUMBERS.
  • 263. Xnumbers Tutorial 262 Note how the use of this function is simple and straight comparing with the correspondent nested formulas =xdiv(xadd(1,A6),xsqr(xadd(1,xpow(A6,2)))) Calculating functions with more than one variable a bit complication arises, because we have to pay attention which values are assigned to the variables. Let’s see this example Calculate the following bivariate function for x = 2.4, y = 5.5 2 1 )ln( ),( x xyy yxf + + = In order to pass to the parameter "Var" the correct value for each variable we select the variables range B2:C3 including the labels "x" and "y" (header). The labels must contain the same symbols contained into the formula string Note. If we pass the range B3:C3 without labels, the function assigns the values to the variables in the same order that they appear in the formula, from left to right. In our example the first variables is “y” and the second is “x”, so the function assignes the first value 2.4 to “y” and the second value 5.5 to “x” To by-pass the variable order rule, the function uses the trick of the “variables labels”. On the contrary, for one or none variable it is impossible to make confusion so the header can be omitted. Variables order. The function returns the variables order in the Excel function insertion panel In our example we see the string “y x” , that means you have to pass the first value for the variable “y” and the second value for the variables “x”
  • 264. Xnumbers Tutorial 263 The variables order is by-passed by labels. Using labels you do not need to worry about the variables order Let’s see another example. Calculat,e with 30 digits precision, the following function )2cos()sin(),,( xbxabaxf −= for x = 30° deg , a = 1 , b = -2 Note that we have set the angle unit = “DEG” Note also that in this case the variable order would be “a, x, b”, but with the aid of the labels the function can associate the exact parameters in the right way. Sometime it is not possible to add a label near its value (in the middle of a table, for example). Neither all parameters are located adjacent each others. For these cases we can use the second evaluator function xevall This function accepts separate parameters or separate array. We do not need to pass variables labels. The function automatically finds all labels present on the active worksheet Of course all this has a cost. the function xevall is about 10 times slower then xeval. Let’s see how it works with an example. Tabulate the given trigonometric function, from t = 0 to 0.5, with step = 0.1 and an error less then 1E-20 )cos()cos()( tbtatf ⋅⋅+⋅⋅= ππ where a = 0.5 and b = -2 The above sheet shows a possible arrangement. If we look the last cell B8 we discover that the parameters are: Var1 the cell A8 containing the value of the independent variable “t” Var2 the range “C4:D4”, containing the values of the parameters “a” and “b” Var3 range “C7:D7”, containing the internal parameter “DgtMax” and “AngleSet” The internal parameters “DgtMax” and “AngleSet” are reserved word and must write as is. Note also that the cell A8 has no label, but the function performs the correct assignment to the “t” variable.
  • 265. Xnumbers Tutorial 264 Label Rules. Labels must stay always at the top or at the left of the corresponding values. Labels can have any alphanumeric name starting with any letter and not containing any blank. In the example: t = 0.1, a = 0.5 , DgtMax = 30 The function xevall only assigns the cell value to the variable on top to the correspondent column. Othewise it assigns the value to the variable on the left to the correspondent row.
  • 266. Xnumbers Tutorial 265 Complex Expression Evaluation =cplxeval( Formula, [Var1, Var2 …] ) This function1 evaluates a math expression in complex arithmetic. The parameter "Formula" is a math expression string containing variables, operators, parenthesis and other basic functions. (3+8j)*(-1-4j) , (1+i)*ln(1+3i) , ((x+3i)/(x+4-2i))^(1-i) The optional parameter "Var1", "Var2",... can be single or complex value. See How to insert a complex number for better details Example: Evaluate the given complex polynomial for z = 2 – i )52()3(2 iziz ++++ Note that we use the complex rectangular format, i.e. "3+2j", only in the symbolic math formula. When we pass a complex value to a variable we must always use the double cell format. Note also that we can write “i” or “j” as well for imaginary symbol, the parser can recognize both of them. For complex numbers labels are not supported. When we have formulas with two or more variables, we must provide the values for variable substitutions in the exact order that they appear in the formula, starting from left to right. The formula wizard will easily help you. Look at this example. Example. Compute the expression for the given complexes values ))(( )( sk bsas e sF −− = s = 1 + j , a = 1 − 4j , b = 3 + 6j , k = − 0.5 In the cell B2 we have inserted the string “exp(k*s)/((s-a)(s-b)” When we enter the formula, the parser recognizes the variables symbols and shows us the exact order in which we have to pass to the function itself. In this case: k, s, a, b 1 This function uses the clsMathParserC class by A. De Grammont and L. Volpi
  • 267. Xnumbers Tutorial 266 Multiprecision Excel Formula Evaluation = xCalc(Formula, [DgtMax]) This function1 is useful for performing multiprecision calculation directly in a worksheet Formulas can be entered just like in Excel and it may also contain references to other cells. The optional parameter "DgtMax" – from 1 to 200 – sets the maximum number of precision digits (default=30). This smart functions has several advantages: it is easy to use and can handle long formula; the formula is not changed at all and can keep the references to other cells. The only drawback remains its speed because the function have to parse the formula and evaluate the result using the multiprecision arithmetic; therefore the evaluation takes about 5-6 times slower then basic x-functions. Of course, not all the Excel functions can be converted. The functions recognized are. + - * / ^ ABS ACOS ACOSH ASIN ASINH ATAN ATANH COMBIN COS COSH EXP FACT INT LN LOG MOD PI ROUND SIGN SIN SINH SQRT TAN TANH Nota. The functions are indicated with their original English names, that usually are different from the local names. Let's see how it works. Assume to have in a cell the following Excel formula =(A2*EXP(B2)+A4*EXP(B4))/(A2^2+A4^2) Note that the formula references the values of other 4 cells: A2, B2, A4, B4 The formula value, calculated in standard precision, is 0.0140820785524786 We can get the value in 30 significant digits applying the xCalc function =xCalc( (A2*EXP(B2)+A4*EXP(B4))/(A2^2+A4^2) ) For that, simply, select the cell D2 and move the mouse to the input field; insert xCalc( ... ) just around the original formula. The result in the cell D4 has now a precision of 30 significat digits Observe that the the external cells links remain active and the formula is unchanged as in a standard Excel formula 1 The function xCalc appears thanks to Ton Jeursen.
  • 268. Xnumbers Tutorial 267 Note that, using the x-function, the above formula should be translated as =xdiv(xadd(xmult(A2,xexp(B2)),xmult(A4,xexp(B4))),xadd(xpow(A2,"2"),xpow(A4,"2"))) The result is equivalent but it is evident the great advantage to keep the original formula.
  • 269. Xnumbers Tutorial 268 Math expression strings Functions like Integr, Series, xeval, xevall, cplxeval operate with symbolic math expressions by the aid of clsMathParser and claMathparserC (two internal class modules). These programs (for real and complex numbers) accept in input any string representing an arithmetic or algebraic expression with a list of variable values and return a multiprecision numeric result. Typical math expressions are: 1+(2-5)*3+8/(5+3)^2 sqr(2)+asin(x) (a+b)*(a-b) x^2+3*x+1 1.5*exp(-t/12)*cos(pi*t + pi/4) (1+(2-5)*3+8/(5+3)^2)/sqr(5^2+3^2) 2+3x+2x^2 0.25x + 3.5y + 1 sqr(4^2+3^2) 1/(1+e# ) + Root(x,6) (-1)^(2n+1)*x^n/n! |x-2|+|x-5| And((x<2),(x<=5)) sin(2*pi*x)+cos(2*pi*x) Variables can be any alphanumeric string and must start with a letter x, y, a1, a2, time, alpha , beta Also the symbol "_" is accepted to build variable names in "programming style". time_1, alpha_b1 , rise_time Capitals are accepted but ignored. Names such as "Alpha", "alpha", "ALPHA" indicate the same variable. Implicit multiplication is not supported because of its intrinsical ambiguity. So "xy" stands for variable named "xy" and not for x*y. The multiplication symbol "*" generally cannot be omitted. It can be omitted only for coefficients of the classic math variables x, y, z. It means that string like 2x and 2*x are equivalent 2x, 3.141y, 338z^2 ⇔ 2*x, 3.141*y, 338*z^2 On the contrary, the following expressions are illegal in this context. 2a, 3(x+1), 334omega Constant numbers can be integer, decimal, or exponential 2 -3234 1.3333 -0.00025 1.2345E-12 Note: This version support also the comma "," as decimal separator, depending on your system internation option setting. 1,3333 -0,00025 1,2345E-12 Logical expressions are now supported x<1 x+2y >= 4 x^2+5x-1>0 t<>0 (0<x<1) Logical expressions return always 1 (True) or 0 (False). Compact expressions, like “0<x<1” , are now supported; you can enter: (0<x<1) as well (0<x)*(x<1) Numerical range can be inserted using logical symbols and Boolean functions. For example: For 2<x<5 insert (2<x)*(x<5) or also (2<x<5) For x<2 , x>=10 insert OR(x<2, x>=10) or also (x<2)+(x>=10) For -1<x<1 insert (x>-1)*(x<1) , or (-1<x<1) , or also |x|<1
  • 270. Xnumbers Tutorial 269 Piecewise Functions. Logical expressions can also be useful for defining a piecewise function, such as:  2x-1-ln(2) x ≤ 0.5 f(x) =  ln(x) 0.5< x < 2  x/2-1+ln(2) x ≥ 2 The above function can be written as: f(x) = (x<=0.5)*(2*x-1-ln(2))+(0.5<x<2)*ln(x)+(x>=2)*(x/2-1+ln(2)) The parser adopts a new algorithm for evaluating math expressions depending on logical expressions, which are evaluated only if the logical conditions are true (Conditioned-Branch algorithm). Thus, the above piecewise expression can be evaluated for any real value x without any domain error. Note that without this features the formula could be evaluated only for x>0. Another way to compute piecewise functions is splitting it into several formulas (see example 6) Math Constants supported are: Pi Greek (π), Euler-Napier (e) pi = 3.14159265358979 or pi# = 3.14159265358979 e# = 2.71828182845905 Angle expression This version supports angles in RAD radians, DEG degree, or GRAD degree. For example if you set the unit "DEG", all angles will be read and converted into degrees sin(120) => 0.86602540378444 asin(0.86602540378444) => 120 rad(pi/2) => 90 , grad(400) => 360 , deg(360) => 360 Angles can also be write in DMS format like for example 29d 59m 60s sin(29d 59m 60s) => 0.5 , 29d 59s 60m => 30 Note This format is only for sexagesimal degree. It is independent from the unit set Complex number can be indicated in a formula string as an ordered couple of number enclosed into parenthesis “(..)” and divided by a comma “,” like for example: (2, 3) (a, b) (-1, -0.05) (-1.4142135623731, -9.94665E-18) On the other hand, complex numbers can also be indicate by the common rectangular form: 3+3j a+bj -1 − 0.05j -1.4142135623731 − 9.94665E-18j You note that the second form is suitable for integer numbers, while, on the contrary, for decimal or exponential number the first one is clearer. The parenthesis form is more suitable also in nested results like ((2+3*4), (8-1/2)) that gives the complex number (14, 7.5) Note: Pay attention if you want to use the rectangular convention in nested formulas. wrong (2+3*4)+(8-1/2)j. correct (2+3*4)+(8-1/2)*j . Do not omit the product symbol “*” before j because the parser recognize it as an expression, not a complex number. The product symbol can be omitted only when before the letter “j” is a constant number Note: You can use both “j” and “i” for indicating the imaginary number 1− Functions Functions are called by their function-name followed by parentheses. Arguments can be: numbers, variables, expressions, or even other functions. sin(x) log(x) cos(2*pi*t+phi) atan(4*sin(x)) max(a,b) root(x,y) comb(n,k) beta(x,y)
  • 271. Xnumbers Tutorial 270 For functions having more than one argument, the successive arguments are separated by commas "," or alternatively, by semicolon ";" if your system has comma as decimal separator. max(a;b) root(x;y) comb(n;k) beta(x;y) (only if comma "," is decimal sep.) max(a,b) root(x,y) comb(n,k) beta(x,y) (only if point "." is decimal sep.)
  • 272. Xnumbers Tutorial 271 List of basic functions and operators The following table lists all functions and operators recongize in a math expression string Use: R = Real, C = Complex, M = Multiprecision The functions with M can be evaluated either in standard or multiprecision The functions with C alone must be used with cplxeval only The functions with R alone must be used with xeval, xevall setting DgtMax = 0 (standard precision) Function Use Description Note - R C M subtraction ! R C M factorial 5!=120 (the same as fact) % R M percentage 35% = 0.35 , 100+35% =103.5 * R C M multiplication / R C M division 35/4 = 8.75 R C M integer division 354 = 8 ^ R C M raise to power 3^1.8 = 7.22467405584208 | | R C M absolute value |-5|=5 (the same as abs) + R C M addition < R M less than return 1 (true) 0 (false) <= R M equal or less than returns 1 (true) 0 (false) <> R M not equal returns 1 (true) 0 (false) = R M equal returns 1 (true) 0 (false) > R M greater than returns 1 (true) 0 (false) >= R M equal or greater than returns 1 (true) 0 (false) abs(x) R C M absolute value abs(-5)= 5 acos(x) R C M inverse cosine argument -1 ≤ x ≤ 1 acosh(x) R C M inverse hyperbolic cosine argument x ≥ 1 acot(x) R M inverse cotangent acoth(x) R M inverse hyperbolic cotangent argument x<-1 or x>1 acsc(x) R M inverse cosecant acsch(x) R M inverse hyperbolic cosecant alog(z) C complex exponential with base 10 10^z and(a,b) R logic and returns 0 (false) if a=0 or b=0 arg(z) C polar angle of complex number -pi < a <= pi asec(x) R M inverse secant asech(x) R M inverse hyperbolic secant argument 0 ≤ x ≤ 1 asin(x) R C M inverse sine argument -1 ≤ x ≤ 1 asinh(x) R C M inverse hyperbolic sine atanh(x) R C M inverse hyperbolic tangent argument -1 < x < 1 atn(x), atan(x) R C M inverse tangent beta(x,y) R C beta argument x>0 y>0 betaI(x,a,b) R Beta Incomplete function x >0 , a >0 , b >0 cbr(x) R cube root cbr(2) =1.25992104989487, also 2^(1/3) clip(x,a,b) R Clipping function return a if x<a , return b if x>b, otherwise return x. comb(n,k) R C M combinations comb(6,3) = 20 conj(x) C conjugate cos(x) R C M cosine argument in radians cosh(x) R C M hyperbolic cosine cot(x) R M cotangent argument (in radians) x≠ k*π with k = 0, ± 1, ± 2… coth(x) R M hyperbolic cotangent argument x>2 csc(x) R M cosecant argument (in radians) x≠ k*π with k = 0, ± 1, ± 2… csch(x) R M hyperbolic cosecant argument x>0 dec(x) R M decimal part dec(-3.8) = -0.8 deg(x) R M degree sess. conversion conv. degree (60) into current unit of
  • 273. Xnumbers Tutorial 272 angle digamma(x) R C digamma argument x>0 Ei(x) R C exponential integral function argument x>0 Ein(x,n) R Exponential integral of n order x >0 , n = 1, 2, 3… Elli1(x) R Elliptic integral of 1st kind ∀ φ , 0 < k < 1 Elli2(x) R Elliptic integral of 2st kind ∀ φ , 0 < k < 1 erf(x) R C error Gauss's function argument x>0 erfc(x) R C error Gauss's function for complex value eu R M Euler-Mascheroni constant Gamma constant 0.577.. exp(x) R C M exponential exp(1) = 2.71828182845905 fact(x) R M factorial argument x >0 fix(x) R M integer part fix(-3.8) = 3 FresnelC(x) R Fresnel's cosine integral ∀ x FresnelS(x) R Fresnel's sine integral ∀ x gamma(x) R C gamma argument x>0 gammaln(x) R C logarithm gamma argument x>0 gcd(a,b) R greatest common divisor The same as mcd grad(x) R degree cent. conversion conv. degree (100) into current unit of angle HypGeom(x,a,b,c) R Hypergeometric function -1 < x <1 a,b >0 c ≠ 0, −1, −2… im(z) C immaginary part of complex number int(x) C integer part int(-3.8) = 4 integral(f,z,a,b) C Def. integral of complex function f(z) Integral('1/z^2','z',1-i,1+i) inv(x) C inverse of a number 1/x lcm(a,b) R M lowest common multiple The same as mcm ln(x), log(x) R C M logarithm natural argument x>0 max(a,b,...) R M maximum max(-3.6, 0.5, 0.7, 1.2, 0.99) = 1.2 min(a,b,...) R M minimum min(13.5, 24, 1.6, 25.3) = 1.6 mcd(a,b,...) R M maximun common divisor mcm(4346,174) = 2 mcm(a,b,...) R M minimun common multiple mcm(1440,378,1560,72,1650) = 21621600 mod(a,b) R M modulus mod(29, 6) = 5 mod(-29, 6) = 1 nand(a,b) R logic nand returns 1 (true) if a=1 or b=1 neg(z) C opposit of complex -z nor(a,b) R logic nor returns 1 (true) only if a=0 and b=0 not(a) R logic not returns 0 (false) if a ≠ 0 , else 1 nxor(a,b) R logic exclusive-nor returns 1 (true) only if a=b or(a,b) R logic or returns 0 (false) only if a=0 and b=0 pi R M Pi greek 3.141…. PolyCh(x,n) R Chebycev's polynomials ∀x , orthog. for -1 ≤ x ≤1 PolyHe(x,n) R Hermite's polynomials ∀ x , orthog. for −∞ ≤ x ≤ +∞ PolyLa(x,n) R Laguerre's polynomials ∀ x , orthog. for 0 ≤ x ≤1 PolyLe(x,n) R Legendre's polynomials ∀ x , orthog. for -1 ≤ x ≤1 rad(x) R M radians conversion converts radians into current unit of angle re(x) C real part of complex number rnd(x) R random returns a random number between x and 0 root(x,n) R C M n-th root argument x ≥ 0 (the same as x^(1/n) round(x,d) R M round a number with d decimal round(1.35712, 2) = 1.36 sec(x) R M secant argument (in radians) x≠ k*π/2 with k = ± 1, ± 2… sech(x) R C M hyperbolic secant argument x>1 serie(….) C serie expansion of complex value z Serie('z/n','n',1,10,'z',1+3i) sgn(x) R C sign returns 1 if x >0 , 0 if x=0, -1 if x<0 sin(x) R C M sin argument in radians sinh(x) R C M hyperbolic sine
  • 274. Xnumbers Tutorial 273 sqr(x) R C M square root sqr(2) =1.4142135623731, also 2^(1/2) tan(x) R C M tangent argument (in radians) x≠ k*π/2 with k = ± 1, ± 2… tanh(x) R C M hyperbolic tangent xor(a,b) R logic exclusive-or returns 1 (true) only if a ≠ b BesselI(x,n) R Bessel's function of 1st kind, nth order, mod. x >0 , n = 0,1, 2, 3… BesselJ(x,n) R Bessel's function of 1st kind, nth order x ≥0 , n = 0, 1, 2, 3… BesselK(x,n) R Bessel's function of 2nd kind, nth order, mod. x >0 , n = 0,1, 2, 3… BesselY(x,n) R Bessel's function of 2nd kind, nth order x ≥0 , n = 0,1, 2, 3… J0(x) R Bessel's function of 1st kind x ≥0 Y0(x) R Bessel's function of 2nd kind x ≥0 I0(x) R Bessel's function of 1st kind, modified x >0 K0(x) R Bessel's function of 2nd kind, modified x >0 Si(x) R Sine integral ∀ x Ci(x) R Cosine integral x >0 zeta(x) R Riemman's zeta function argument x<-1 or x>1 AiryA(x) R Airy function Ai(x) ∀x, example cbr(2) = 1.2599, cbr(-2) = - 1.2599 AiryB(x) R Airy function Bi(x) argument (in radian) x≠ k*π/2 with k = ± 1, ± 2… Mean(a,b,...) R M Arithmetic mean mean(8,9,12,9,7,10) = 9.1666 Meanq(a,b,...) R M Quadratic mean meanq(8,9,12,9,7,10) = 9.300 Meang(a,b,...) R M Arithmetic mean meang(8,9,12,9,7,10) = 9.035 Var(a,b,...) R M Variance var(1,2,3,4,5,6,7) = 4.6666 Varp(a,b,...) R M Variance pop. varp(1,2,3,4,5,6,7) = 4 Stdev(a,b,...) R M Standard deviation Stdev(1,2,3,4,5,6,7) = 2.1602 Stdevp(a,b,...) R M Standard deviation pop. Stdevp(1,2,3,4,5,6,7) = 2 Step(x,a) R Haveside's step function Returns 1 if x ≥ a , 0 otherwise DSBeta(x, a, b, [j]) R Beta distribution (j=1 cumulative) 0 < x < 1 , a > 0, b > 0, DSBinomial(k, n, p, [j]) R Binomial distribution (j=1 cumulative) k integer, n integer , 0 < p < 1 DSCauchy(x, m, s, n, [j]) R Cauchy (generalized) distribution (j=1 cumul.) n integer , s > 0 DSChi(x, r, [j]) R Chi distribution (j=1 cumulative) r integer, x > 0 DSErlang(x, k, l, [j])) R Erlang distribution (j=1 cumulative) k integer, x > 0 DSGamma(x, k, l, [j])) R Gamma distribution (j=1 cumulative) x > 0, k > 0, l > 0 DSLevy(x, l, [j])) R Levy distribution (j=1 cumulative) x > 0, l > 0 DSLogNormal(x, m, s, [j])) R Log-normal distribution (j=1 cumulative) x > 0, m ≥ 0, s > 0 DSLogistic(x, m, s, [j])) R Logistic distribution (j=1 cumulative) x > 0, m ≥ 0, s > 0 DSMaxwell(x, a, [j])) R Maxwell-Boltzman distribution (j=1 cumulative) x > 0, a > 0 DSMises(x, k, [j])) R Von Mises distribution (j=1 cumulative) k > 0, -π < x < π DSNormal(x, m, s, [j])) R Normal distribution (j=1 cumulative) s > 0 DSPoisson(k, z, [j])) R Poisson distribution (j=1 cumulative) k integer, z > 0 DSRayleigh(x, s, [j])) R Rayleigh distribution (j=1 cumulative) x > 0, s > 0 DSRice(x, v, s, [j])) R Rice distribution (j=1 cumulative) x > 0, v ≥ 0 , s > 0 DSStudent(t, v, [j])) R Student distribution (j=1 cumulative) v integer degree of freedom DSWeibull(x, k, l, [j])) R Weibull distribution (j=1 cumulative) x > 0, k integer, l > 0 Symbol "!" is the same as "Fact", symbol "" is the integer division, symbols “|x|” is the same as Abs(x) Logical function and operators returns 1 (true) or 0 (false) Note: the arguments separator of the functions changes automatically from "," to ";" if your system decimal separator is comma ",".
  • 275. Xnumbers Tutorial 274 Function Optimization Macros for optimization on site These macros has been ideated for performing the optimization task directly on the worksheet. This means that you can define any function that you want simply using the standard Excel built-in functions. Objective function. For example: if you want to search the minimum of the bivariate function ( ) ( )2 100 352 100 51 ),( −+−= yxyxf insert in the cell E4 the formula "=(B4-0.51)^2+(C4-0.35)^2", where the cells B4 and C4 contain the current values of the variables x and y respectively. Changing the values of B4 e/o C4 the function value E4 also changes consequently. For optimization, you can choose two different algorithms Downhill-Simplex 1 The Nelder–Mead downhill simplex algorithm is a popular derivative-free optimization method. Although there are no theoretical results on the convergence of this algorithm, it works very well on a wide range of practical problems. It is a good choice when a one-off solution is wanted with minimum programming effort. It can also be used to minimize functions that are not differentiable, or we cannot differentiate. It shows a very robust behavior and converges for a very large set of starting points. In our experience is the best general purpose algorithm, solid as a rock, it's a "jack" for all trades. For mono and multivariate functions Divide-Conquer 1D For monovariable function only, it is an high robust derivative free algorithm. It is simply a modified version of the bisection algorithm Adapt for every function, smooth or discontinue. It converges for very large segments. Starting point not necessary For monovariable function only. It needs the segment where the max or min is located Example assume to have to minimize the following function for x > 0 )4cos()3sin()( 3 xexexf xx −− += 1 The Downhill-Simplex of Nelder and Maid routine appears by the courtesy of Luis Isaac Ramos Garcia
  • 276. Xnumbers Tutorial 275 We try to search the minimum in the range 0 < x < 10 Choose a cell for the variable x , example B6, and insert the function = SIN(3*B6)*EXP(- 2*B6) + COS(4*B6)*EXP(-B6) in a cell that you like, for example C6. After this, add the constrain values into another range, for example B3:C3 The values of the variables at the start are not important Select the cell of the function C6 and start the macro "1D divide and conquer", filling the input field as shown Stopping limit. Set the maximum evaluation points allowed. Max/Min. The radio buttons switches between the minimization and maximization algorithm The "Downhill-Simplex" macro is similar except that: • The constrain box is optional. • It accepts up to 9 variables (range form 1 to 9 cells) • The algorithm starts from the point that you give in the variable cells. If the constrain box is present, the algorithm starts from a random point inside the box Let's see how it works with some examples1 . 1 The following examples are extracted from "Optimization and Nonlinear Fitting" , Foxes Team, Nov. 2004
  • 277. Xnumbers Tutorial 276 Example 1 - Rosenbrock's parabolic valley This family of test functions is well known to be a minimizing problem of high difficult ( ) ( )222 1),( xxymyxf −+−⋅= The parameter "m" tunes the difficult: high value means high difficult in minimum searching. The reason is that the minimum is located in a large flat region with a very low slope. The following 3D plot shows the Rosenbrock's parabolic valley for m = 100 The following contour plot is obtained for m = 10 The function is always positive except in the point (1, 1) where it is 0. it is simple to demonstrate it, taking the gradient ( ) ( )    =− =−⋅−+⋅ ⇒=∇ 02 022124 0 2 3 xym ymxxm f
  • 278. Xnumbers Tutorial 277 From the second equation, we get ( ) 22 02 xyxym =⇒=− Substituting in the first equation, we have ( ) 1022022124 23 =⇒=−⇒=−⋅−+⋅ xxxmxxm So the only extreme is the point (1, 1) that is the absolute minimum of the function To find numerically the minimum, let's arrange a similar sheet. We can insert the function and the parameters as we like Select the cell D4 - containing the objective function - and start the macro "Downhill-Simplex". The macro fills automatically the variables-field with the cells related to the objective function. But, In that case, the cell A4 contains the parameter m that the macro must not change. So insert only the range B4:C4 int the variables field. The cells B4:C4 will change for minimizing the objective function in the cell D4 Leave empty the constraint input box and press "Run" Starting from the point (0, 0) we obtain the following good results m Algorithm x y error time 10 Simplex 1 1 2.16E-13 2 sec 100 Simplex 1 1 4.19E-13 2 sec where the error is calculated as |x-1|+|y-1| Example 2 - Constrained minimization Example: assume to have to minimize the following function 710442),( 22 +−+−+= yyxxyxyxf with the ranges constrains 5.00,20 ≤≤≤≤ yx The Excel arrangement can be like the following
  • 279. Xnumbers Tutorial 278 Compare with the exact solution x = 1.5, y = 0.5 Note that the function has a free minimum at x = 1, y = 1 Repeat the example living empty the constrains box input, for finding those free extremes.
  • 280. Xnumbers Tutorial 279 Example 3 - Nonlinear Regression with Absolute Sum This example explains how to perform a nonlinear regression with an objective function different from the "Least Squared". In this example we adopt the "Absolute Sum". We choose the exponential model xk eakaxf ⋅− ⋅=),,( The goal of the regression is to find the best couple of parameters (a, k) that minimizes the sum of the absolute errors between the regression model and the given data set. ∑ −= |),,(| kaxfyAS ii The objective function AS depends only by parameter a, k. Giving in input this function to our optimization algorithm we hope to solve the regression problem A possible arrangement of the worksheet may be: We hope that changing the parameters "a" and "k" int the cells E2 and F3, the objective function (yellow cell) goes to its minimum value. Note that the objective function depends indirectly by the parameters a and k. 0 0.2 0.4 0.6 0.8 1 1.2 0 0.2 0.4 0.6 0.8 1 1.2 y y* The starting condition is the following, where y indicates the given data and y* is the regression plot (a flat line at the beginning) Start the Downhil-Simplex and insert the appropriate ranges: objective function = G3 and variables = E3:F3 Starting form the point (1, 0) you will see the cells changing quickly until the macro stops itself leaving the following "best" fitting parameters and the values of the regression y* Best fitting parameters a k 1 -2 The plot of the y* function and the samples y are shown in the graph. As we can see the regression fits perfectly the given dataset.
  • 281. Xnumbers Tutorial 280 Example 4 - Optimization of Integral function The method of optimization on-site used by Xnumbers macros and by the Excel Solver is very flexible. This is expecially useful when the optimization function has a complicated form. Assume, for example, to have to maximize the following integral respect to the parameter α ∫ − − 1 0 )( dxexx xα α The function integr returns the integral in the cell B5. The integration limits and the parameter α are contained in the cells B2, B3 and B4 respectively. Start the optimization macro that you like, setting the objective cell B5 and changing the cell B4 Here, we have choose three different algorithms. They seem work very fine for this problem Algorithm α (max) error Divide-conquer 2.210360464 9.8E-09 Downhill-Simplex 2.210360527 5.4E-08 Excel Solver 2.210361438 9.6E-07 Another example. Find the maximum of the following function for 0 < ω < 2 ∫= π ωω 0 )sin()( dxxf Here we use the Excel Solver for finding the value of the cell D2 (the variable ω) maximizing the cell E2 (the integral), subjected to the constraints 0 < ω < 2 . The final result w = 0.74201929194068 has an high precision, better 1E-8.
  • 282. Xnumbers Tutorial 281 How to call Xnumbers functions from VBA There are two ways for calling Xnumbers functions from other user VBA macros • by reference method • by Application.Run method Both methods have advantages and drawbacks. Let's see. Reference method Prepare a new worksheet called, for example test.xls First of all , after we have installed and loaded Xnumbers.xla in Excel, turn on the VBA editor by shift+F11 and add a new module. Our VBA editor should look like this. In particular we must see the XNUMBERS (xnumbers.xla) project and our new project VBAProject (test.xls) Now we add a reference link in our VBAProject to XNUMBERS project From the menu tool/ references... Switch on the box of XNUMBERS library and click OK Note that a references object has been added to our project From now on all functions (except the functions declared private) of XNUMBERS are available in our macros. Of course all functions that you use in worksheet are public. But not all the functions that we can use in a worksheet can be called from VBA and vice versa. The link that we have added is permanent. This means that when we load our test.xls file, the Xnumbers.xla will also be automatically loaded. If you want to remove the link to Xnumbers.xla simply return to menu tools/references and deselect the box Now prepare the following simple macro that reads two numbers from the sheet and returns the division in multiprecision
  • 283. Xnumbers Tutorial 282 Sub Test() Dim a, b, c, DgtMax DgtMax = Range("B2") a = Range("B3") b = Range("B4") c = XNUMBERS.xdiv(a, b, DgtMax) Range("B5") = "'" & c End Sub Note that the above code works also if we write the compact statement c = xdiv(a, b, DgtMax) The XNUMBERS prefix is not necessary; it is useful only - at the developing time - to generate automatically the list of the functions when we write the dot "." after XNUMBERS. The result of the Test macro will be: Of course the x-functions can be nested as any other function. For example assume now to have to compute the expression = a * Ln(b) The code will be Sub Test1() Dim a, b, c, DgtMax DgtMax = Range("B2") a = Range("B3") b = Range("B4") c = xmult(a, xln(b, DgtMax), DgtMax) Range("B5") = "'" & c End Sub The output of the Test1 macro will be
  • 284. Xnumbers Tutorial 283 As we have seen calling x-function using the reference method is very easy and straightforward because it works as we have "virtually" copied all the Xnumbers.xla code in our project The main drawback is that the link set in our workbook is static. This mean that if we move or install the Xnumbers.xla addin in another directory, also the link must be removed and then rebuilt. Application.Run method A metod for calling external functions that always works, independently where you have installed the addin, uses the Excel Application.Run method. Using this method we do not need to add any reference link. We have only to load the addin in Excel. Therefore our VBA environment looks like as simple as possible and the code of the macro Test will modified as the following Sub test() Dim a, b, c, DgtMax DgtMax = Range("B2") a = Range("B3") b = Range("B4") With Application c = .Run("xdiv", a, b, DgtMax) End With Range("B5") = "'" & c End Sub The first argument of the "Run" method is the name of the function that we want to call, that is "xdiv" in our case (note the quotes around the name). The other arguments are, in sequence, the parameters that we pass to the function xdiv. The "Run" method returns in the variable c the result of the xdiv function. This method is both simple and efficient. The only drawback is that it cannot accept nested functions. Thus the Test1 macro becomes a bit more tricky. Sub test1() Dim a, b, c, DgtMax DgtMax = Range("B2") a = Range("B3") b = Range("B4") With Application c = .Run("xLn", b, DgtMax) c = .Run("xmult", a, c, DgtMax) End With Range("B5") = "'" & c End Sub The final result, of course, is the same.
  • 285. Xnumbers Tutorial 284 References & Resources "MSIEVE: A Library for Factoring Large Integers", Jason Papadopoulos , v1.17, March 2007 "LAPACK -- Linear Algebra PACKage 3.0", Updated: May 31, 2000 "EISPACK Guide, Matrix Eigensystem Routines”, Smith B. T. at al. 1976 "Numerical Analysis" F. Sheid, McGraw-Hill Book Company, New-York, 1968 "Handbook for Autom. Computation - Linear Algebra", vol II, Wilkinson, Martin, Peterson, 1970 "Matrix Analysis and Applied Linear Algebra", C. D. Mayer, Siam, 2000 "Linear Algebra", J. Hefferon, Saint Michael’s College, Colchester, Vermont, 2001, "Numerical Methods that usually work", F. S. Acton, The Mathematica Association of America, 1990 "Analysis Nunerical Methods", E. Isaacson, H. B. Keller, Wiles & Sons, 1966 "Calculo Numérico", Neide M. B. Franco, 2002 "Metodos Numericos" Sergio R. De Freitas, 2000 "Numerical Mathematics in Scientific Computation", G. Dahlquist, Å. Björck, vol II. "Numerical Recipes in FORTRAN 77- The Art of Scientific Computing - 1986-1992 by Cambridge University Press. Programs Copyright (C) 1986-1992 by Numerical Recipes Software "Computation of Special Functions", by Shanjie Zhang and Jianming Jin - John Wiley and Sons, Inc "Lanczos Implementation of the Gamma Function" by Paul Godfrey, Intersil Corp, 2001 "clsMathParser - A Class for Math Expressions Evaluation in Visual Basic", Leonardo Volpi , Michael Ruder, Thomas Zeutschler, Lieven Dossche, . 3.2 Jan. 2003, by .Volpi "clsMathParserC -A Class for Complex Math Expressions Evaluation in Visual Basic", Arnaud de Grammont, Leonardo Volpi, v. 3.2 , Jan. 2003 "F F T (Fast Fourier Transform)", Paul Bourke, June 1993, http://astronomy.swin.edu.au "2 Dimensional FFT" , Paul Bourke, June 1998, http://astronomy.swin.edu.au “Solutions Numeriques des Equations Algebriques”, E., Durand, Tome I, Masson,Paris ,1960. ”A modified Newton method for Polynomials” , W.,Ehrilich, Comm., ACM, 1967, 10, 107-108. “Ein Gesamschrittverfahren zur Berechnung der Nullstellen eines Polynoms”, O., Kerner, Num.Math., 1966, 8, 290-294. “Iteration methods for finding all the zeros of a polynomial simultaneously”, O. Aberth, Math. Comp. ,1973, 27, 339-344. “The Ehrlich-Aberth Method for the nonsymmetric tridiagonal eigenvalue problem”, D. A. Bini, L. Gemignani, F, Tisseur, AMS subject classifications. 65F15 “Progress on the implementetion of Aberth’s method”, D. A. Bini, G. Fiorentino, , 2002, The FRISCO Consortiunm (LTR 21.024) “A Method for Finding Real and Complex Roots of Real Polynomials With Multiple Roots”, by C. Bond, July 10, 2003 “A Robust Strategy for Finding All Real and Complex Roots of Real Polynomials”, by C. Bond, April 24, 2003 "Nonlinear regression", Gordon K. Smyth Vol. 3, pp 1405/1411, in Encyclopedia of Environmetrics (ISBN 0471 899976), Edited by John Wiley & Sons, Ltd, Chichester, 2002 "Optimization", Gordon K. Smyth Vol. 3, pp 1481/1487, in Encyclopedia of Environmetrics (ISBN 0471 899976), Edited by John Wiley & Sons, Ltd, Chichester, 2002 "Process Modeling", The National Institute of Standards and Technology (NIST) website for Statistical Reference Datasets, (http://www.itl.nist.gov/div898/handbook/pmd/pmd) "Metodos numericos con Matlab"; J. M Mathewss et al.; Prentice Hall "Genetic and Nelder-Mead", E. Chelouan, et al, EJOR 148(2003) 335-348 "Convergence properties", J.C. Lagarias, et al, SIAM J Optim. 9(1), 112-147
  • 286. Xnumbers Tutorial 285 "Optimization for Engineering Systems", Ralph W. Pike, 2001, Louisiana State University (http://www.mpri.lsu.edu/bookindex) "Zeros of Orthogonal Polynomials" , Leonardo Volpi, Foxes Team (http://digilander.libero.it/foxes/Documents) "How to tabulate the Legendre’s polynomials coefficients" Leonardo Volpi, Foxes Team (http://digilander.libero.it/foxes/Documents) "DE-Quadrature (Numerical Automatic Integrator) Package", by Takuya OOURA, Copyright(C) 1996 “Advanced Excel for scientific data analysis", Robert de Levie, 2004, Oxford University Press "Microsoft Excel 2000 and 2003 Faults, Problems, Workarounds and Fixes", David A. Heiser, web site http://www.daheiser.info/excel/frontpage.html "NIST/SEMATECH e-Handbook of Statistical Methods", January 26, 2005 (http://www.itl.nist.gov/div898/handbook) "DE-Quadrature (Numerical Automatic Integrator) Package", by Takuya OOURA, Copyright(C) 1996, Research Institute for Mathematical Sciences, Kyoto University, Kyoto 606-01, Japan (http://momonga.t.u-tokyo.ac.jp/~ooura) "A Comparison of three high-precision quadrature schemes", David H. Bailey and Xiaoye S. Li, Copyright 2003, University of California (http://repositories.cdlib.org/lbnl/LBNL-53652) "Asymptotic Bit Cost of Quadrature Formulas Obtained by Variable Transformation" P. Favati, Appl. Math. Lett. Vol. 10, No. 3, pp. 1-7, 1997, Pergamon Copyright 1997 "Gli algoritmi della crittografia a chiave pubblica", Giovanni Fraterno, settembre 2000. http://digilander.libero.it/crittazione Bailey, D. H.; Borwein, J. M.; Calkin, N. J.; Girgensohn, R.; Luke, D. R.; and Moll, V. H. "Integer Relation Detection." §2.2 in Experimental Mathematics in Action. Wellesley, MA: A K Peters, pp. 29-31, 2006. Bailey, D. H. and Broadhurst, D. J. "Parallel Integer Relation Detection: Techniques and Applications." Math. Comput. 70, 1719-1736, 2001. Bailey, D. H. and Ferguson, H. R. P. "Numerical Results on Relations Between Numerical Constants Using a New Algorithm." Math. Comput. 53, 649-656, 1989. Bailey, D. and Plouffe, S. "Recognizing Numerical Constants." http://www.cecm.sfu.ca/organics/papers/bailey/. Borwein, J. M. and Lisonek, P. "Applications of Integer Relation Algorithms." To appear in Disc. Math. http://www.cecm.sfu.ca/preprints/1997pp.html. Weisstein, Eric W. "Integer Relation." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/IntegerRelation.html Helaman R. P. Ferguson and David H. Bailey , "A Polynomial Time, Numerically Stable Integer Relation Algorithm" RNR Technical Report RNR-91-032, July 14, 1992
  • 287. Xnumbers Tutorial 286 Credits Software developed Xnumbers contains code developed by the following authors that kindly contributed to this collection. Luis Isaac Ramos Garcia Orthogonal Polynomials and Downhill Simplex Nelder-Mead routine Olgierd Zieba Cubic Spline and documentation Alfredo Álvarez Valdivia Robust Method fitting routines Michael J. Kozluk Log Relative Error function, Linear regression debugging, and documentation (StRD benchmark) Ton Jeursen Format function, Xnumber debugging; Xnumber for Excel95, xBaseChange - Multiprecision BaseChange xCalc - multiprecision conversion function Richard Huxtable ChangeBase and Prime functions Michael Ruder MathParser improvement and debugging Thomas Zeutschler MathParser improvement and debugging Lieven Dossche Class MathParser development Arnaud de Grammont Complex MathParser developement Rodrigo Farinha MathParser improvement and debugging Vladimir Zakharov Installation and initialization improvement Hans Günter Extension of exponential integral for negative x David A. Heiser Weilford's algorithm for statistic standard deviation John Jones Modular arithmetic and Xnumber converter parser Software translated Xnumbers contains VB code translated from the following packages: Takuya OOURA DE-Quadrature (Numerical Automatic Integrator) Package Shanjie Zhang - Jianming Jin FORTRAN routines for computation of Special Functions Software used This documentation was developed thanks to: Microsoft Help Workshop 4 Compiler for Help-on line Microsoft Word and Equation Editor RTF main document and formulas Winplot ( Peanut Software by Richard Parris) Math plots and graphical elaboration Derive 5 (Texas Instrument) Math graph and symbolic elaboration MathGV 3.1 (by Greg VanMullem) Math graphs and bitmap elaboration Msieve 1.17 by Jason Papadopoulos. Large number factoring Many thanks to everybody that have contributed to this document and made it more intelligible. I am especially grateful for invaluable help on many occasions to Robert de Levie.
  • 288. Xnumbers Tutorial 287 Analytical Index Analytical index A Absolute; 28 Adams; 211; 212 Adams-Bashfort-Moulton; 211 Addition; 23 Airy functions; 252 AiryA; 252 AiryAD; 252 AiryB; 252 AiryBD; 252 Aitken; 158; 183 Arccos; 62 Arcsine; 62 Arctan; 62 Arithmetic Mean; 40 B Base conversion; 240 baseChange; 240 Bessel functions; 249 BesselSphJ; 255 Beta function; 247 Bisection; 231 Bivariate Polynomial; 88 Brouncker-Pell Equation; 120 C Cebychev; 78 Change sign; 28 Check digits; 32 Check Integer; 112 Check odd/even; 112 Check Prime; 109 Cholesky; 131 Coefficients of Orthogonal Polynomials; 96 Combinations; 39 Combinations function; 248 Complement of right angle; 63 Complex absolute; 99 Complex Addition; 98 Complex ArcCos; 102 Complex ArcSin; 102 Complex ArcTan; 102 Complex Complementary Error Function; 104 Complex conjugate; 101 Complex Cos; 102 Complex digamma; 103 Complex Division; 98 Complex Error Function; 104 Complex Exp; 101 Complex Exponential Integral; 103 Complex Expression Evaluation; 260 Complex Function Integration (Romberg method); 156 Complex Gamma Function; 104 Complex Hyperbolic Cosine; 103 Complex Hyperbolic Sine; 102 Complex Hyperbolic Tan; 103 Complex inverse; 101 Complex Inverse Hyperbolic Cos; 103 Complex Inverse Hyperbolic Sin; 103 Complex Inverse Hyperbolic Tan; 103 Complex Log; 101 Complex Logarithm Gamma Function; 104 Complex Multiplication; 98 Complex negative; 101 Complex power; 100 Complex Quadratic Equation; 105 Complex Roots; 100 Complex Series Evaluation; 184 Complex Sin; 102 Complex Subtraction; 98 Complex Tangent; 102 Complex Zeta Function; 104 Constant e; 59 Constant Ln(10); 59 Constant Ln(2); 59 Constant pi; 62 Continued Fraction; 108 Continued Fraction of Square Root; 109 Convert extended into double; 34 Convol; 189 corrector; 211 Corrector; 212; 214 Cos; 61 Cosine Integral Ci(x); 249 CosIntegral; 249 cplxabs; 99 cplxacos; 102 cplxacosh; 103 cplxadd; 98 cplxasin; 102 cplxasinh; 103 cplxatan; 102 cplxatanh; 103 cplxconj; 101 cplxcos; 102 cplxcosh; 103 cplxdigamma; 103 cplxdiv; 98 cplxei; 103 cplxEquation2; 105 cplxerf; 104 cplxerfc; 104 cplxeval; 260 cplxExp; 101 cplxgamma; 104 cplxgammaln; 104
  • 289. Xnumbers Tutorial 288 cplxintegr; 156 cplxinv; 101 cplxLn; 101 cplxmult; 98 cplxneg; 101 cplxpolar; 99 cplxpow; 100 cplxrect; 99 cplxroot; 100 cplxserie; 184 cplxsin; 102 cplxsinh; 102 cplxsub; 98 cplxtan; 102 cplxtanh; 103 cplxzeta; 104 Crout; 131 cspline_coeff; 199 cspline_eval; 197 cspline_interp; 197 cspline_pre; 197 Cubic Spline 2nd derivatives; 197 Cubic Spline Coefficients; 199 cvBaseDec; 240 cvBinDec; 240 cvDecBase; 240 cvDecBin; 240 D Data Conditioning; 53 Data Integration (Newton Cotes); 157 dBel; 240 Decibel; 240 Decimal part; 29 DFSP; 140 DFSP_INV; 140 DFT; 137; 142 DFT_INV; 139 Diff1; 234 Diff2; 235 digamma; 247 Digamma function; 247 Digit_Max; 22; 35 Digits count; 31 Digits sum; 33 DigitsAllDiff; 32 Diophantine; 119 Diophantine Equation; 119 DiophEqu; 119 Discrete 2D Fourier Transform; 141 Discrete Convolution; 189 Discrete Fourier Inverse Transform; 139 Discrete Fourier Spectrum; 140 Discrete Fourier Transform; 137 Division; 24 Double Data integration; 179 Double Exponential; 152 Double Integral; 168 Double integration function; 174 Double Series; 185 Downhill; 268 DPOLYN; 74 E Elliptic Integrals; 252 errfun; 244 Error Function Erf(x); 244 Euler; 211 Euler constant γ; 60 Euler-Mascheroni Constant; 245 Euler's Totient function; 121 exp_integr; 244 expn_integr; 244 Exponential; 58 Exponential any base; 58 Exponential integral Ei(x); 244 Exponential integral En(x); 244 Extended Number Check; 32 F Factor; 116 Factorial; 39 Factorial with double-step; 39 Factorize; 113 Factorize function; 116 FFT; 137; 142 FFT_INV; 139 FFT2D; 141 FFT2D_INV; 141 Fibonacci numbers; 250 First Derivative; 234 Flip; 33 Format Extended Number; 32 Fourier; 165 Fourier_cos; 164 Fourier_cos; 166 Fourier_cos; 166 Fourier_sin; 164 fract; 107 Fract_Interp; 195 Fract_Interp_Coef; 195 FractCont; 108 FractContSqr; 109 Fresnel cosine Integral; 250 Fresnel sine Integral; 250 Fresnel_cos; 250 Fresnel_sin; 250 Function Integration (Double Exponential method); 152 Function Integration (mixed method); 154 Function Integration (Newton-Cotes); 159 Function Integration (Romberg method); 151 G Gamma F-factor; 247 Gamma function); 245 Gamma quotient; 246 GCD; 106 Geometric Mean; 40 Grad; 235 Gradient; 235 Greatest Common Divisor; 106 H Hermite; 78 Hessian; 236 Hessian matrix; 236 Hyperbolic Arc Cosine; 60 Hyperbolic Arc Sine; 59
  • 290. Xnumbers Tutorial 289 Hyperbolic Arc Tangent; 60 Hyperbolic Cosine; 60 Hyperbolic Sine; 59 Hyperbolic Tangent; 60 Hypergeometric function; 251 Hypgeom; 251 I I BesselSphY; 255 IElliptic1; 252 IElliptic2; 252 Incomplete Beta function; 248 Incomplete Gamma function; 248 Infinite integral; 177 Infinite Integration of oscillating functions; 165 Integer Division; 24 Integer part; 29 Integer polynomial; 86 Integer relation; 122 Integer Remainder; 25 Integer roots; 70 integr; 166 Integr; 154 Integr_2D; 174 Integr_fcos; 162 Integr_fsin; 162 Integr_nc; 159 Integr_ro; 151 Integr2D; 168 Integr3D; 171 Integral function; 147 Integral of sine-cosine power; 254 Integral_Inf; 177 Integration of oscillating functions (Filon formulas); 162 Integration of oscillating functions (Fourier transform); 164 IntegrData; 147 IntegrData2D; 179 IntegrDataC; 157 Interp_Mesh; 200 Interpolation with continue fraction; 195 Interpolation with Cubic Spline; 197 IntPowCos; 254 IntPowSin; 254 Inverse; 24 Inverse 2D Discrete Fourier Transform; 141 Inverse Discrete Fourier Spectrum; 140 J Jacobian; 236 Jacobian matrix; 236 K Kummer confluent hypergeometric functions; 253 Kummer1; 253 Kummer2; 253 L LCM; 106 Least Common Multiple; 106 Legendre; 78 Linear Regression - Coefficients; 50 Linear Regression - Standard Deviation of Estimate; 45 Linear Regression Coefficients; 43 Linear Regression Covariance Matrix; 47 Linear Regression Evaluation; 49 Linear Regression Formulas; 46 Linear Regression Min-Max; 56 Linear Regression Statistics; 48 Linear Regression with Robust Method; 55 Log Gamma function; 246 Log Relative Error; 242 Logarithm in any base; 58 Logarithm natural (Napier’s); 58 M Macro for Double Integration; 168 Macro for Multiprecision Matrix operations; 135 Macro for Triple Integration; 171 Macro Integer relation finder; 126 Macro Mesh Fill; 201 Macro Poly Regression; 51 Macro Regression; 51 Macro Sampler; 145 Macros for optimization on site; 267 Macros X-Edit; 35 Math expression strings; 263 MathParser; 265 matrix; 135 Matrix Addition; 129 Matrix Determinant; 129 Matrix Inverse; 129 Matrix LL T decomposition; 131 Matrix LU decomposition; 131 Matrix Modulus; 130 Matrix Multiplication; 129 Matrix Power; 130 Matrix Subtraction; 129 Maximum Common Divisor; 106 MCD; 106 MCM; 106 Mesh Interpolation 2D; 200 Minimum Common Multiple; 106 mjkLRE; 242 Modular Addition; 110 Modular Division; 110 Modular Multiplication; 110 modular power; 110 Modular Power; 110 Modular Subtraction; 110 Multiple roots; 72 Multiplication; 24 Multiprecision Base Conversion; 241 Multiprecision Excel Formula Evaluation; 261 Multiprecision Expression Evaluation; 256 N Next Prime; 109 NextPrime; 109 Non Linear Equation Solving; 238 N-Root; 28 Numbers comparison; 31
  • 291. Xnumbers Tutorial 290 O Objective function; 267 OD Linear System; 222 ODE Implicit Predictor-Corrector; 219 ODE Multi-Steps; 211 ODE Predictor-Corrector 4; 217 ODE Runge-Kutta 4; 207 ODE_COR; 214 ODE_PC2I; 219 ODE_PC4; 217 ODE_PRE; 214 ODE_RK4; 207 ODE_SYSL; 222 Orthogonal polynomials; 92 Orthogonal Polynomials; 92 Orthogonal Polynomials evaluation; 93 oscillating; 165 P Partial; 89 PECE; 214; 216 Perfect Square; 112 permutation; 40 Permutations; 40 Polar Conversion; 99 Poly_ChebychevT; 93 Poly_ChebychevU; 93 Poly_Gegenbauer; 93 Poly_Hermite; 93 Poly_Jacobi; 93 Poly_Laguerre; 93 Poly_Legendre; 93 Poly_Weight_ChebychevT; 95 Poly_Weight_ChebychevU; 95 Poly_Weight_Gegenbauer; 95 Poly_Weight_Hermite; 95 Poly_Weight_Jacobi; 95 Poly_Weight_Laguerre; 95 Poly_Weight_Legendre; 95 PolyAdd; 76 PolyBuild; 83 PolyBuildCfx; 85 PolyCenter; 81 PolyDiv; 77 PolyInt; 86 PolyInterp; 191 PolyInterpCoef; 191 PolyMult; 76 POLYN; 73 POLYN2; 88 Polynomial addition; 76 Polynomial building from roots; 83 Polynomial building with multi-precision; 85 Polynomial center; 81 Polynomial coefficients; 75 Polynomial derivatives; 74 Polynomial division quotient; 77 Polynomial division remainder; 77 Polynomial evaluation; 73 Polynomial interpolation; 191 Polynomial multiplication; 76 Polynomial Regression - Standard Deviation of Estimates; 50 Polynomial Regression Statistics; 50 Polynomial roots radius; 82 Polynomial shift; 81 Polynomial solving; 86 Polynomial subtraction; 77 Polynomial System of 2nd degree; 87 Polynomial writing; 76 PolyRadius; 82 PolyRem; 77 PolyShift; 81 PolySolve; 86 PolySub; 77 Polyterms; 80 PolyTerms; 75 predictor; 211 Predictor; 212 Predictor; 214 Prime; 109 Prime Numbers Generator; 117 Prime Test; 117 PrimeGenerator; 117 Product; 27 PSLQ; 122 Q Quadratic Mean; 40 R Raise to power; 27 Rational Fraction approximation; 107 Rectangular Conversion; 99 RegLinMM; 56 RegLinRM; 55 regression; 272 Relative Rounding; 30 Root Error Estimation; 68 Rounding; 29 S Scalar Product; 130 Scientific Format; 34 Secant; 232 Second Derivative; 235 Serie_trig; 186 Serie2D_trig; 188 Series acceleration with ∆^2; 183 Series Evaluation; 182 sign; 31 Significant Digits count; 31 Similarity Transform; 130 Simplex; 268 Sin; 61 Sine Integral Si(x); 249 SinIntegral; 249 Solve Linear Equation System; 132 SortRange; 33 Spherical Bessel functions of integer order; 255 Split scientific format; 34 Square Delta Extrapolation; 133 Square Root; 28 Standard Deviation; 40 Sub-Tabulation; 53 Subtraction; 23 Sum; 27 sumDigits; 33 SYSPOLY2; 87
  • 292. Xnumbers Tutorial 291 T Tan; 62 tanh-sinh transformation; 152 Totient; 121 Trigonometric double serie; 188 Trigonometric series; 186 Truncating; 29 U Univariate Statistic; 42 V Variance; 41 Vector flip; 33 Vector Product; 132 W Weight of Orhogonal Polynomials; 95 X x2pi; 62 xabs; 28 xacos; 62 xacosh; 60 xadd; 23 xaddmod; 110 xanglecompl; 63 xasin; 62 xasinh; 59 xatan; 62 xatanh; 60 xBaseChange; 241 xBeta; 247 xBetai; 248 xCalc; 261 xcdbl; 34 xcomb; 39 xcomb_big; 248 xcomp; 31 xcos; 61 xcosh; 60 xcplxabs; 99 xcplxadd; 98 xcplxconj; 101 xcplxdiv; 98 xcplxExp; 101 xcplxinv; 101 xcplxLn; 101 xcplxmult; 98 xcplxneg; 101 xcplxpolar; 99 xcplxpow; 100 xcplxrect; 99 xcplxroot; 100 xcplxsub; 98 xcvexp; 34 xdec; 29 xDgt; 31 xdiv; 24 xdivint; 24 xdivmod; 110 xdivrem; 25 xe; 59 xeu; 60 xeval; 26; 256 xevall; 256 xexp; 58 xfact; 39 xfact2; 39 xFib; 250 xFormat; 32 xfrac; 107 xFractCont; 108 xGammaF; 247 xGammaI; 248 xGammaln; 246 xGammalog; 246 xGammaQ; 246 xGm; 245 xgmean; 40 xint; 29 xinv; 24 xIsInteger; 112 xIsOdd; 112 xIsSquare; 112 xIsXnumber; 32 xLn; 58 xLn10; 59 xLn2; 59 xLog; 58 xLRE; 242 xMatAbs; 130 xMatAdd; 129 xMatBAB; 130 xMatDet; 129 xMatInv; 129 xMatLL; 131 xMatLU; 131 xMatMult; 129 xMatPow; 130 xMatSub; 129 xMCD; 106 xMCM; 106 xmean; 40 xmult; 24 xmultmod; 110 xneg; 23; 28 xpi; 62 xpi2; 62 xpi4; 62 xpow; 27 xpowmod; 110 xProdScal; 130 xProdVect; 132 xqmean; 40 xRegLinCoef; 43 xRegLinCoef; 45 xRegLinEval; 49 xroot; 28 xround; 29 xroundr; 30 xSerie2D; 185 xsin; 61 xsinh; 59 xsplit; 34 xsqr; 28 xstatis; 42 xstdev; 40 xstdevp; 40
  • 293. Xnumbers Tutorial 292 xsub; 23 xsubmod; 110 xsum; 27 xSYSLIN; 132 xtan; 62 xtanh; 60 xtrunc; 29 xUnformat; 32 xvar; 41 xvarp; 41 Z Zero_bisec; 231 Zero_sec; 232 Zeros of Orthogonal Polynomials; 95 Zeta; 251 Zeta function; 251
  • 294. Xnumbers Tutorial 293  2007, by Foxes Team ITALY Oct 2007