2D Spline Interpolation with ALGLIB

I have updated the ALGLIB Spline and Matrix Function spreadsheet to include 2D interpolation of tabular data, including both linear and cubic spline interpolation.  The spreadsheet including the new functions with full open source code may be downloaded from AL-Spline-Matrix03.zip for Excel 2003 and earlier and AL-Spline-Matrix07.zip for the Excel 2007 and 2010 version.

Typical input and output is shown in the screenshots below:

CSpline2DA - click for full size view

 

LinSpline2DA and interpolation data

CSpline2DA and LinSpline2DA Output

Posted in AlgLib, Excel, Maths, Newton, UDFs, VBA | Tagged , , , , | 6 Comments

Depiction and Excel Webinar

Depiction is a low cost GIS program.  I received an e-mail today that they are hosting a webinar on using Depiction with Excel from 10-11 AM PDT on Wed 18th August.  Click here for details.

Looks interesting, but it’s 3:00-4:00 AM Sydney time, and I’m not quite that interested.

Posted in Excel | Tagged , , | Leave a comment

ALGLIB Statistics Functions

I have recently imported all the ALGLIB statistics functions into VBA, and written interface functions where required, so that all the functions may now be used as User Defined Functions (UDFs) on the spreadsheet.  A spreadsheet including full open source code and descriptions and examples of each function may be downloaded from AL-Stats.zip.  The available functions are shown on the screenshots below: 

ALGLIB Statistics Functions - 1 (click for full size view)

ALGLIB Statistics Functions - 2

ALGLIB Statistics Functions - 3

The screen shots show output from the ALGLIB functions together with the equivalent Excel results, where available.  Note that many of the Excel functions are only available in Excel 2010, and will not work in earlier versions.  Also note that in some cases Excel returns statistics for an estimate of the population value, whereas the ALGLIB functions always return the statistic for the sample.  See for example this Wikipedia article on the Kurtosis function. 

The ALGLIB functions with names not preceded by AL_  call the ALGLIB code directly.  For the remainder it has been necessary to write an interface function for various reasons: 

  • The ALGLIB routine is written as a sub, rather than a function
  • The ALGLIB routines often require a 1D, base 0, array as input, or have several values as output, which must be assigned to an array so they can be returned by a UDF entered as an array function
  • A few of the ALGLIB routines written as functions, which could otherwise be called directly, have the same name as the equivalent Excel function

Code for a typical interface function is shown below: 


Public Function AL_DistMoments(XA As Variant) As Variant
  Dim XA0() As Double, N As Long, Ncols As Long, Rtn As Long, DMRes(1 To 4) As Double

  Dim Mean As Double, Variance As Double, Skewness As Double, Kurtosis As Double
Rtn = VarAtoDouble1D_0(XA, XA0, N, Ncols)
Call CalculateMoments(XA0, N, Mean, Variance, Skewness, Kurtosis)
  DMRes(1) = Mean

  DMRes(2) = Variance

  DMRes(3) = Skewness

  DMRes(4) = Kurtosis
   AL_DistMoments = DMRes
End Function

The VarAtoDouble1D_0 routine converts a VBA Variant array into a 1D, base 0, Double array and returns the number of rows and columns of the array: 

Function VarAtoDouble1D_0(XL_A As Variant, ByRef Cpp_A() As Double, ByRef Nrows As Long, ByRef Ncols As Long) As Long
    Dim i As Long, j As Long, LB As Long
    On Error GoTo iErr
    If TypeName(XL_A) = "Range" Then XL_A = XL_A.Value2

    LB = LBound(XL_A)

    Nrows = UBound(XL_A)

    Ncols = UBound(XL_A, 2)
    ' Copy 2D base 1 variant array to 1D base 0 double array
    ReDim Cpp_A(0 To Nrows * Ncols - 1)

    For i = 1 To Nrows

        For j = 1 To Ncols
            Cpp_A((i - 1) * Ncols + j - 1) = XL_A(i, j)

        Next j
    Next i
    VarAtoDouble1D_0 = 0
    Exit Function
iErr:
    VarAtoDouble1D_0 = 1
End Function

The download spreadsheet includes all the necessary code, but for the benefit of those wanting to use the code direct from the ALGLIB site, or to use the versions in different languages, all the necessary code modules are listed below: 

  • binomialdistr: Binomial distribution
  • chisquaredistr: Chi-Square distribution
  • correlation: Pearson/Spearman correlation coefficients
  • correlationtests: Hypothesis testing: correlation tests
  • descriptivestatistics: Descriptive statistics: mean, variance, etc.
  • fdistr: F-distribution
  • gammafunc
  • hqrnd: High quality random numbers generator
  • ibetaf
  • igammaf
  • jarquebera: Hypothesis testing: Jarque-Bera test
  • mannwhitneyu: Hypothesis testing: Mann-Whitney-U test
  • nearunityunit
  • normaldistr: Normal distribution
  • poissondistr: Poisson distribution
  • stest: Hypothesis testing: sign test
  • studenttdistr: Student’s t-distribution
  • studentttests: Hypothesis testing: Student’s t-test
  • variancetests: Hypothesis testing: F-test and one-sample variance test
  • wsr: Hypothesis testing: Wilcoxon signed rank test
Posted in AlgLib, Excel, Maths, Newton, UDFs, VBA | Tagged , , , , , | 4 Comments

The hole through the middle of the Earth – filled with air

Three air molecules go into a hole,
 Well, I say three; could have been four or five.
 Could have been nine or ten, doesn’t matter.
Could have been fifteen, twenty – fifty.
Round it up. Hundred. Let’s go mad, eh – two-fifty.

Alright, make it a lot simpler, all the air molecules on the planet go into a hole, right?

Hang on – they wouldn’t fit would they?  What do you think? What proportion of the Earth’s atmosphere could we fit in a hole through the middle of the Earth.  The atmosphere, if it was compressed to sea level pressure occupies about 4 billion cubic km, and a hole through the Earth of 1 square metre cross section would occupy about 0.013 cubic km, so even allowing for a fair bit of extra compression, we aren’t going to get much of the atmosphere into the hole are we?  Let’s do the numbers.

Using the AlgLib ODE solver we can easily write a function to calculate the pressure at any distance from the Earth’s centre, starting at normal pressure (say 100 kPa) at the surface.  The ODE and the function code are shown in the screen-shot below. 

ODE for air pressure down a hole to the centre of the Earth

 To keep things simple I have made the following assumptions:

  1. Air acts as an ideal gas all the way down the hole
  2. Temperature changes are ignored
  3. The acceleration due to gravity varies linearly from the surface to zero at the centre

1 and 2 are obviously fairly gross approximations, but we only want an order of magnitude result, so let’s see how they work out.

To check out the ODE solver I have started off going from the surface up to an elevation of 10,000 m:

Air pressure variation to 10,000 m

This gives a result of 27.2 kPa, which compares pretty well with the actual pressure (extracted from the Internet) of 26 kPa.  Also the ODE function was in close agreement with a simple Runge-Kutta solution carried out on the spreadsheet (see screenshot).

The next step was to go downwards in 100 steps of approximately 64 km each.  Here we run into a problem.  The AlgLib function fails to converge over this distance.  The problem is that it has an iterative error minimisation routine, but very big numbers cause it to crash.  To get around this I have used the simple Runge-Kutta method on the spreadsheet, without any error minimisation.  Using this we find a pressure at the centre of about 4.3E148 kPa (43 followed by 147 zeros!):

Pressure to the Centre of the Earth

Here is a graph of the pressure against depth on a log vertical scale:

Pressure distribution to the centre of the Earth (log pressure scale)

The total mass of air in the hole would be 2.6E164 kg, which is composed of about 1.5E200 atoms.

It is difficult to comprehend just how big these numbers are, but some idea is given by considering that the total number of atoms in the observable universe is estimated to be of the order of 1E80.  We would need 1.5E120 Universes to fill the hole.  To look at it another way, if every atom in the observable universe was a universe of equal size itself, and if every atom in every one of those universes contained a mass of 200 billion tonnes, then that would provide enough perfect gas to fill the hole so that the pressure at the surface was just 100 kPa.

It is fortunate that no real gas behaves remotely like a perfect gas at high pressures!

In the next post in this series I will look at how much air we would need using real gas behaviour.  I just need to find a source of information for compressibility of air at very high pressures.

Posted in AlgLib, Differential Equations, Excel, Maths, Newton, UDFs, VBA | Tagged , , , , , , | 4 Comments

Three blokes go into a pub …

“…  Well, I say three; could have been four or five.
 Could have been nine or ten, doesn’t matter.
Could have been fifteen, twenty – fifty.
Round it up. Hundred. Let’s go mad, eh – two-fifty.
Tell you what, double it up – five hundred.
Thousand!
Oh, I’ve gone mad! Two thousand!
Five thousand!
(adopting auctioneer persona)
anyone five thousand,
six thou,
six thousand,
ten thousand!
Small town in Hertfordshire goes into a pub!
Fifteen thousand blokes!
Alright, let’s go – population of Rotterdam.
The Hague.
Whole of Northern Holland.
Mainland U.K.
Let’s go all the way to the top – Europe, alright?
Whole of Europe goes –
I say Europe. Could be Eurasia.
Not the band, obviously, that’s just two of them.
Alright, continents – North America!
Plus South America!
Plus Antarctica – that’s just eight blokes in a weather station.
Not a good example.
Alright, make it a lot simpler, all the blokes on the planet go into the pub, right?

And the first bloke goes up to the bar and he says “I’ll get these in.”

What an idiot.”

Ref: Bailey, Bill; some stand-up gig somewhere probably, date unknown

Posted in Bach | Tagged , , , | 1 Comment