[Scipy-svn] r4642 - branches/Interpolate1D/docs
scipy-svn at scipy.org
scipy-svn at scipy.org
Tue Aug 12 13:15:20 EDT 2008
Author: fcady
Date: 2008-08-12 12:15:10 -0500 (Tue, 12 Aug 2008)
New Revision: 4642
Added:
branches/Interpolate1D/docs/geology_dataset1.txt
branches/Interpolate1D/docs/optimization.png
branches/Interpolate1D/docs/plot_temp_vs_depth.png
branches/Interpolate1D/docs/plot_temp_vs_depth_clean.png
branches/Interpolate1D/docs/spline.png
branches/Interpolate1D/docs/thickness_data.dat
Modified:
branches/Interpolate1D/docs/tutorial.rst
Log:
prettier tutorial, including better examples and more pictures
Added: branches/Interpolate1D/docs/geology_dataset1.txt
===================================================================
--- branches/Interpolate1D/docs/geology_dataset1.txt 2008-08-12 00:11:21 UTC (rev 4641)
+++ branches/Interpolate1D/docs/geology_dataset1.txt 2008-08-12 17:15:10 UTC (rev 4642)
@@ -0,0 +1,10 @@
+1.499536604568347800e+000 1.658453575548198300e-001
+3.697762547686129800e+000 3.994002011895560500e-001
+4.093644614730469500e+000 4.393270321822482200e-001
+7.671079003802294200e+000 1.000000000000000000e+003
+8.269696620589108700e+000 7.949075438330595000e-001
+1.007630409931378800e+001 8.999214508357034100e-001
+1.263468232585750100e+001 9.860973953103480100e-001
+1.312256663311384800e+001 9.936523208151388800e-001
+1.659125799620406700e+001 1.000000000000000000e+003
+1.939929889697446800e+001 8.338880137988362600e-001
Property changes on: branches/Interpolate1D/docs/geology_dataset1.txt
___________________________________________________________________
Name: svn:executable
+ *
Added: branches/Interpolate1D/docs/optimization.png
===================================================================
(Binary files differ)
Property changes on: branches/Interpolate1D/docs/optimization.png
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:mime-type
+ application/octet-stream
Added: branches/Interpolate1D/docs/plot_temp_vs_depth.png
===================================================================
(Binary files differ)
Property changes on: branches/Interpolate1D/docs/plot_temp_vs_depth.png
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:mime-type
+ application/octet-stream
Added: branches/Interpolate1D/docs/plot_temp_vs_depth_clean.png
===================================================================
(Binary files differ)
Property changes on: branches/Interpolate1D/docs/plot_temp_vs_depth_clean.png
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:mime-type
+ application/octet-stream
Added: branches/Interpolate1D/docs/spline.png
===================================================================
(Binary files differ)
Property changes on: branches/Interpolate1D/docs/spline.png
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:mime-type
+ application/octet-stream
Added: branches/Interpolate1D/docs/thickness_data.dat
===================================================================
--- branches/Interpolate1D/docs/thickness_data.dat 2008-08-12 00:11:21 UTC (rev 4641)
+++ branches/Interpolate1D/docs/thickness_data.dat 2008-08-12 17:15:10 UTC (rev 4642)
@@ -0,0 +1,10 @@
+9.424048978739288900e-001 -9.508761403068971800e+000
+9.998766914279348900e-001 -9.895012964291035800e+000
+1.127952820779362300e+000 -3.904822709859180600e+000
+1.157936681816710500e+000 -1.605861744330539200e+000
+1.202510554645833900e+000 1.940671938130194500e+000
+1.314422096865679900e+000 8.869036982549690900e+000
+1.398645599167923900e+000 9.813198077596094700e+000
+1.458882224612567200e+000 7.804047162000441200e+000
+1.545769275246878100e+000 1.988814246858684700e+000
+1.603025472727923000e+000 -2.549859447665460800e+000
Property changes on: branches/Interpolate1D/docs/thickness_data.dat
___________________________________________________________________
Name: svn:executable
+ *
Modified: branches/Interpolate1D/docs/tutorial.rst
===================================================================
--- branches/Interpolate1D/docs/tutorial.rst 2008-08-12 00:11:21 UTC (rev 4641)
+++ branches/Interpolate1D/docs/tutorial.rst 2008-08-12 17:15:10 UTC (rev 4642)
@@ -95,14 +95,14 @@
There is a large selection of strings which specify a range of interpolation methods. The list includes:
-#. 'linear' : linear interpolation, same as the default
-#. 'block' : "round new_x down" to the nearest values where we know y.
-#. 'nearest' : take the y value of the nearest point
-#. 'logarithmic' : logarithmic interpolation (linear in log(y) )
-#. 'spline' : spline interpolation of default order (currently 3)
-#. 'cubic' : 3rd order spline interpolation
-#. 'quartic' : 4th order spline interpolation
-#. 'quintic' : 5th order spline interpolation
+#) 'linear' : linear interpolation, same as the default
+#) 'block' : "round new_x down" to the nearest values where we know y.
+#) 'nearest' : take the y value of the nearest point
+#) 'logarithmic' : logarithmic interpolation (linear in log(y) )
+#) 'spline' : spline interpolation of default order (currently 3)
+#) 'cubic' : 3rd order spline interpolation
+#) 'quartic' : 4th order spline interpolation
+#) 'quintic' : 5th order spline interpolation
The same flexibility is afforded for extrapolation by the keywords low and high, which
specify how to treat values below and above the range of known data: ::
@@ -315,16 +315,20 @@
and 2) estimate the average temperature.
::
- In []: data_array = loadtxt('dataset1.txt')
+ In []: data_array = loadtxt('geology_dataset1.txt')
In []: shape(data_array)
Out []: (12, 2)
In []: depth = data_array[:,0]
In []: temp = data_array[:,1]
In []: max(depth)
- Out []: 20
+ Out []: 19.39929
In []: plot(depth, temp)
+.. image :: plot_temp_vs_depth.png
+
+::
+
# He realizes that many of the temperatures are 1000, indicating
# a measurement error, which makes it look terrible.
# And what is there doesn't look smooth
@@ -332,8 +336,12 @@
In []: import interpolate as I
In []: plot( I.interp1d(depth, temp, linspace(0,20,100), bad_data = [1000])
# much better, but he wants to see it smoother too
- In []: plot( I.interp1d(depth, temp, linspace(0,20,100), kind='cubic', bad_data = [1000])
+ In []: plot( I.interp1d(depth, temp, linspace(0,20,100), kind='cubic', bad_data = [1000]))
+.. image :: plot_temp_vs_depth_clean.png
+
+::
+
# To find the average temp he can't average the data points because the samples
# are not necessarily uniform, but it is easy to uniformly sample the interpolated function
In []: average_temp = average( I.interp1d(depth, temp, linspace(0,20,100), 'cubic', bad_data=[1000]) )
@@ -397,7 +405,7 @@
prototype, and repeat. If she does this, she can "zoom in" on the optimal thickness.
::
- In []: data_array = loadtxt('data.dat')
+ In []: data_array = loadtxt('thickness_data.dat')
In []: thickness = data_array[:,0]
In []: performance = data_array[:,1]
In []: new_thick = linspace( min(thickness), max(thickness), 200 )
@@ -406,11 +414,20 @@
# somewhat expensive, making prototypes is much more so
In []: new_perf = interp1d(thickness, performance, new_thick, kind = 'quintic')
In []: guess_perf = max(new_perf)
- In []: guess_thick = new_thick( find( new_perf == best_perf ) )
+ In []: guess_thick = new_thick[ find( new_perf == guess_perf ) ]
In []: len(guess_thick)
Out []: 1 # make sure she only got one answer.
+ # To get a feel, she plots the data, the interpolated curve, and the
+ # estimated optimal point
+ In []: plot(thickness, performance, 'r', new_thick, new_perf)
+ In []: scatter(guess_thick, [guess_perf], marker='>') #guess_perf in braces to make iterable
+
+.. image :: optimization.png
+
+::
- # At this point she builds the prototype and calculates its performance.
+ # Now she builds the prototype using the estimated optimal thickness
+ # and measures its performance.
# She wants to re-insert it into the array and interpolate again
In []: measured_perf = 10.7 #the measured performance
In []: where_to_insert = max( find(thickness < guess_thick) ) +1
@@ -457,6 +474,11 @@
they are required to agree in the first k-1 derivatives (ie all but the highest). A spline is specified by the
locations of its knots and, for each interval, the coefficients of polynomial that describes it.
+Below is a simple cubic spline, with the knots indicated by red dots. The third derivative is not
+continuous at these points, but it is not visualy apparent; the curve appears smooth.
+
+.. image :: spline.png
+
For interpolation purposes, the knots are typically chosen to be the known data points. It
is also common for splines to include smoothing of data, so that the curve does not pass
through all the data points but is smoother than it would be if it had to. k=3 is the most
@@ -476,7 +498,7 @@
In []: new_x = linspace(0, 2*pi, 40)
In []: plot(x, y, 'r', new_x, interp(new_x), 'g')
-..image :: spline_of_sin.png
+.. image :: spline_of_sin.png
Notice that the resulting curve is extremely smooth. It is this smoothness that makes splines
in general (and cubic splines in particular) so sought after.
@@ -493,23 +515,24 @@
At instantiation:
-#. bbox
-This is a 2-element list specifying the endpoints of the approximation interval.
-It default to [x[0],x[-1]]
-#. w
-a 1D sequence of weights which defaults to all ones.
-#. s
-If s is zero, the interpolation is exact. If s is not 0, the curve is smoothe subject to
-the constraint that sum((w[i]*( y[i]-s(x[i]) ))**2,axis=0) <= s
+#) bbox
+ This is a 2-element list specifying the endpoints of the approximation interval.
+ It default to [x[0],x[-1]]
+#) w
+ List of non-negative weights indicating the "importance" of each data point.
+ Not relevant unless s is not zero.
+#) s
+ If s is zero, the interpolation is exact. If s is not 0, the curve is smoothe subject to
+ the constraint that sum((w[i]*( y[i]-s(x[i]) ))**2,axis=0) <= s
BEWARE : in the current implementation of the code, if s is small but not zero,
- instantiating Spline can become painfully slow.
+instantiating Spline can become painfully slow.
At calling:
-#. nu
-Spline returns, not the spline function S, but the (nu)th derivative of S. nu defaults
-to 0, so Spline usually returns the zeroth derivative of S, ie S.
+#) nu
+ Spline returns, not the spline function S, but the (nu)th derivative of S. nu defaults
+ to 0, so Spline usually returns the zeroth derivative of S, ie S.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -518,19 +541,19 @@
The following special methods are also available, which are not wrapped by Interpolate1d :
-#. set_smoothing_factor(s = 0.0)
-#. get_knots
+#) set_smoothing_factor(s = 0.0)
+#) get_knots
returns the positions of the knots of the spline
-#. get_coeffs
+#) get_coeffs
returns the coefficients of the
-#. get_residual
- returns the weighted sum of the errors (due to smoothing) at the data points
- sum((w[i]*( y[i]-s(x[i]) ))**2,axis=0)
-#. integral(a, b)
+#) get_residual
+ returns the weighted sum of the squared errors (due to smoothing) at the data points:
+ sum( w[i]*( y[i]-s(x[i]) )^2 )
+#) integral(a, b)
returns the integral from a to b
-#. derivatives(x)
+#) derivatives(x)
returns all the derivatives of the spline at point x
-#. roots
+#) roots
This only works for cubic splines. But it returns the places where the spline
is identically zero.
@@ -539,10 +562,8 @@
2D Interpolation
================================================
-*[This is being written preemptively]*
-
In 2D interpolation, known data are of the form (x, y, z), and we interpolate
-z_new from (x_new, y_new).
+newz at the point(s) (newx, newy).
As in the case of 1D interpolation, there is a convenient functional interface
for 2D interpolation as well as a callable object which can be more efficient.
@@ -560,17 +581,17 @@
If they are scalars or zero-dimensional arrays, newz will be a scalar as well. Otherwise
a vector is returned. The only differences from intper1d are
-#. The known data points are specified by 3 arrays (x, y and z) rather than 2 (x and y).
+#) The known data points are specified by 3 arrays (x, y and z) rather than 2 (x and y).
z is the dependent variable, while x and y are independent variables.
-#. Where to interpolate values is specified by two arrays, newx and newy, rather
+#) Where to interpolate values is specified by two arrays, newx and newy, rather
than only one array.
-#. The extrapolation keywords "low" and "high" are replaced by the single keyword "out"
+#) The extrapolation keywords "low" and "high" are replaced by the single keyword "out"
for out-of-bounds.
-#. Not all of the same keyword arguments are available for 1D and 2D. The main ones like
+#) Not all of the same keyword arguments are available for 1D and 2D. The main ones like
'linear', 'cubic' and 'spline', however, work in both cases, and we try to give analogous
methods the same name. But some methods are particular to, or have only been written
for, one praticular dimensionality.
-#. In particular, 2D supports the keywork '526', which implements TOMS algorithm 526.
+#) In particular, 2D supports the keywork '526', which implements TOMS algorithm 526.
See below for more information.
As in 1D, linear interpolation is used by default, while out of bounds returns NaN.
More information about the Scipy-svn
mailing list