New Version 6.0! |
---|

Try it for free with our fully functional 60-day trial version. |

QuickStart Samples

# Cubic Splines QuickStart Sample (IronPython)

Illustrates using natural and clamped cubic splines for interpolation using classes in the Extreme.Mathematics.LinearAlgebra namespace in IronPython.

C# code Visual Basic code F# code Back to QuickStart Samples

import numerics from Extreme.Mathematics import * # The CubicSpline class resides in the # Extreme.Mathematics.Curves namespace. from Extreme.Mathematics.Curves import * #/ Illustrates creating natural and clamped cubic splines using #/ the CubicSpline class in the Extreme.Mathematics.Curves #/ namespace of the Extreme Optimization Mathematics #/ Library for .NET. # A cubic spline is a piecewise curve that is made up # of pieces of cubic polynomials. Its value as well as its first # derivative are continuous, giving it a smooth appearance. # # Cubic splines are implemented by the CubicSpline class, # which inherits from PiecewiseCurve. # # For an example of piecewise constant and piecewise # linear curves, see the PiecewiseCurves QuickStart # Sample. # # # Creating Cubic Splines # # In order to define a spline curve completely, two extra # conditions must be imposed. # 'Natural' splines have zero second derivatives. This is # the default. # The data points are specified as double arrays containing # the x and y values: xValues = Vector([ 1, 2, 3, 4, 5, 6 ]) yValues = Vector([ 1, 3, 4, 3, 4, 2 ]) naturalSpline = CubicSpline(xValues, yValues) # 'Clamped' splines have a fixed slope or first derivative at the # leftmost and rightmost points. The slopes are specified as # two extra parameters in the constructor: clampedSpline = CubicSpline(xValues, yValues, -1, 1) # # Curve Parameters # # The shape of any curve is determined by a set of parameters. # These parameters can be retrieved and set through the # Parameters collection. The number of parameters for a curve # is given by this collection's Count property. # # Cubic splines have 2n+2 parameters, where n is the number of # data points. The first n parameters are the x-values. The next # n parameters are the y-values. The last two parameters are # the values of the derivative at the first and last point. For natural # splines, these parameters are unused. print "naturalSpline.Parameters.Count =", naturalSpline.Parameters.Count # Parameters can easily be retrieved: print "naturalSpline.Parameters[0] =", naturalSpline.Parameters[0] # Parameters can also be set: naturalSpline.Parameters[0] = 1 # # Piecewise curve methods and properties # # The NumberOfIntervals property returns the number of subintervals # on which the curve has unique definitions. print "Number of intervals:", naturalSpline.NumberOfIntervals # The IndexOf method returns the index of the interval # that contains a specific value. print "naturalSpline.IndexOf(1.4) =", naturalSpline.IndexOf(1.4) # The method returns -1 when the value is smaller than the lower bound # of the first interval, and NumberOfIntervals if the value is equal to or larger than # the upper bound of the last interval. # # Curve Methods # # The ValueAt method returns the y value of the # curve at the specified x value: print "naturalSpline.ValueAt(2.4) =", naturalSpline.ValueAt(2.4) # The SlopeAt method returns the slope of the curve # a the specified x value: print "naturalSpline.SlopeAt(2) =", naturalSpline.SlopeAt(2) # You can verify that the clamped spline has the correct slope at the end points: print "clampedSpline.SlopeAt(1) =", clampedSpline.SlopeAt(1) print "clampedSpline.SlopeAt(6) =", clampedSpline.SlopeAt(6) # Cubic splines do not have a defined derivative. The GetDerivative method # returns a GeneralCurve: derivative = naturalSpline.GetDerivative() print "Type of derivative:", derivative.GetType().ToString() print "derivative(2) =", derivative.ValueAt(2) # You can get a Line that is the tangent to a curve # at a specified x value using the TangentAt method: tangent = clampedSpline.TangentAt(2) print "Slope of tangent line at 2 =", tangent.Slope # The integral of a spline curve can be calculated exactly. This technique is # often used to approximate the integral of a tabulated function: print "Integral of naturalSpline between 1.4 and 4.6 =", naturalSpline.Integral(1.4, 4.6)

Copyright Â© 2003-2018, Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual Studio.NET*, and the *Optimized for Visual Studio* logo

are registered trademarks of Microsoft Corporation.