MaplePortal/CurveFitting - Maple Help

Curve Fitting in Maple

Go to Maple Portal

 Introduction Maple includes a number of assistants that allows a user to experiment and easily perform key tasks.  The Curve Fitting Assistant provides a quick and easy method to fit a curve to a set of data points and to experiment to find the best curve for your data set.  The assistant provides a graph showing both the points in your data as well as the curve returned by the chosen curve fitting algorithm.   Maple also has a number of Task Templates that help with curve fitting.  Use these task templates to find a function that fits your data points using B-spline, least squares approximation, polynomial or rational interpolation, spline, or Thiele's continued fraction interpolation methods. Two interactive task templates let you draw points, view the Bézier curve, and create an interpolation with equispaced points.

Using the Curve Fitting Assistant

Providing Data

To begin a curve fitting problem, you will first need to provide data. The data can be specified as a collection of points, or as separate collections of $x$-values and $y$-values.

You may want to import data from a file using Tools>Assistants>Import Data....  For more information on importing data, see the Importing Data section of the Tutorial on Data Manipulation.

If you want to define the data in Maple, you can list the pairs of data, or define a 2-column Array or Matrix containing the data.

 list of data points $\left[\left[-5,5\right],\left[0,2\right],\left[4,0\right]\right]$ two lists, for $x$-values and $y$-values $\left[-5,0,4\right],\left[5,2,0\right]$ Figure 1 - Two ways to provide data

After you have provided the data, select the data and choose Curve Fitting>Interactive Curve Fitting from the Context Panel.

 > $\mathrm{CurveFitting}[\mathrm{Interactive}]();$
 > 

Figure 2 - Access the Curve Fitting Assistant in the context menu

You will be asked to provide a name for the independent variable. Then the assistant opens with the data loaded.

Alternatively, you can use the CurveFitting[Interactive] command to call the Curve Fitting Assistant.

$\mathrm{CurveFitting}[\mathrm{Interactive}]\left(\left[\left[-5,5\right],\left[0,2\right],\left[4,0\right]\right]\right)$

Note: It is possible to launch the Curve Fitting Assistant directly from the Tools>Tutors>Statistics menu.  You will then have an opportunity to import the data.  In the Curve Fitting Assistant's first window, click the Import button to use Maple's Import Assistant:

However, this method is less practical because after you close the Curve Fitting Assistant, you cannot review the data or access it for further analysis.

Fitting the Curve

After the data is imported into the Curve Fitting Assistant you can explore different curve fitting methods.  The following data is used in this section:

 Figure 3 - Curve Fitting Assistant

This window displays a plot of the points, an indication of the vertical range of the plot, and a selection of relevant curve fitting commands to the right.  At the bottom of the window there is a box that displays the interpolant found with the selected curve fitting algorithm.

Note: Rational interpolation is only available if the data contains no decimal values.  Thiele interpolation is available if the dependent variable values in the data are unique.

For the rational interpolation and splines options, sliders allow you to control the maximum degree of the numerator and denominator used in the rational interpolant, or the degree of the spline being used during the curve fitting procedure.

The least squares section provides an input box where you can specify an expression in terms of $x$.  The parameters will be fit by the Least Squares command with values according to this procedure.  Only expressions that are linear in terms of the parameters are allowed.

In this example, enter the expression a*x^2+b*x+c.  Then click the Plot button for the least squares method to see the result.

 Figure 4 - Fitting a curve to the data points

Click Done to return the interpolant for the least-squares approximation.

$\stackrel{\text{curve fitting assistant}}{\to }$$\frac{{346}}{{7}}{-}\frac{{1018}}{{21}}{}{x}{+}\frac{{253}}{{21}}{}{{x}}^{{2}}$

The Curve Fitting Assistant allows you to explore different curve fitting algorithms and easily see the plot of the resulting curve.  This interface also allows for experimenting with setting different settings for the curve fitting procedure.  When you find the curve that you are looking for, you can choose to return the interpolant (equation) or the plot when you finish using the assistant.

Use these task templates to find a function that fits your data points using B-spline, least squares approximation, polynomial or rational interpolation, spline, or Thiele's continued fraction interpolation methods. Two interactive task templates let you draw points, view the Bézier curve, and create an interpolation with equispaced points.

Fit a Function to your Data

The Task Templates show you how to perform this task in Maple using polynomial, rational, spline, B-spline, least squares approximation, or Thiele's continued fraction interpolation methods.

Insert a Task into a worksheet and replace the placeholders with your information.  (See Using Tasks for details.)

Example: Polynomial Interpolation

Here, the task was inserted with the Insert Default Content button.  This inserts the title and description as well as the example.

To replace the placeholders, place your cursor at the beginning of the first execution group and press tab.  Replace the highlighted data with your data.  Press tab to move to the next placeholder (${x}$) and replace with a variable name of your choice.

Polynomial Interpolation

Calculate the interpolated polynomial of specified data points. This produces a polynomial of degree n, where n is one less than the number of data points.

Enter a list of data points.

 > $\left[\left[{-5}{,}{5}\right]{,}\left[{0}{,}{2}\right]{,}\left[{4}{,}{0}\right]\right]$
 $\left[\left[{-}{5}{,}{5}\right]{,}\left[{0}{,}{2}\right]{,}\left[{4}{,}{0}\right]\right]$ (1)

Specify the independent variable, and then calculate the interpolated polynomial.

 > $p:=\mathrm{CurveFitting}\left[\mathrm{PolynomialInterpolation}\right]\left(,{x}\right)$
 ${p}{:=}\frac{{1}}{{90}}{}{{x}}^{{2}}{-}\frac{{49}}{{90}}{}{x}{+}{2}$ (2)
 > 

For example, the data gets replaced with 5 new data points and called the variable $t$.

The returned curve is a polynomial of degree 4.

 > $\left[\left[0,0\right],\left[2,-3\right],\left[3,3\right],\left[4,0\right],\left[5,5\right]\right]$
 $\left[\left[{0}{,}{0}\right]{,}\left[{2}{,}{-}{3}\right]{,}\left[{3}{,}{3}\right]{,}\left[{4}{,}{0}\right]{,}\left[{5}{,}{5}\right]\right]$ (3)
 > $p:=\mathrm{CurveFitting}\left[\mathrm{PolynomialInterpolation}\right]\left(,t\right)$
 ${p}{:=}\frac{{11}}{{12}}{}{{t}}^{{4}}{-}{10}{}{{t}}^{{3}}{+}\frac{{421}}{{12}}{}{{t}}^{{2}}{-}{39}{}{t}$ (4)
 > 

Example: Least Squares Approximation

Here, in addition to specifying the data points and the variable name, you can also specify the format of the function to be returned.

The Task shows ${a}{{x}}^{{2}}{+}{b}{x}{+}{c}$.  Other examples include $ax+b$ and  .



Least Squares Approximation

Calculate a least squares approximation using specified data points.

Least Squares Fit of Data by a Specified Curve

List of Data Points:

 > $\left[\left[2,-1\right],\left[4,3\right],\left[6,-7\right],\left[7,5\right],\left[9,-2\right]\right]$
 $\left[\left[{2}{,}{-}{1}\right]{,}\left[{4}{,}{3}\right]{,}\left[{6}{,}{-}{7}\right]{,}\left[{7}{,}{5}\right]{,}\left[{9}{,}{-}{2}\right]\right]$ (5)

Fitting Curve:

 >
 ${a}{}{{x}}^{{2}}{+}{b}{}{x}{+}{c}$ (6)

Independent Variable:

 > $x$
 ${x}$ (7)

Least Squares Curve:

 > ${\mathrm{CurveFitting}}_{}\left[\mathrm{LeastSquares}\right]\left(,,\mathrm{curve}=\right);$
 ${-}\frac{{6315}}{{5071}}{+}\frac{{5975}}{{10142}}{}{x}{-}\frac{{669}}{{10142}}{}{{x}}^{{2}}$ (8)

Generate Bézier Curves Interactively

The Bézier Curves Task Template is an interactive template.  As you draw points, the curve fitting is calculated and shown.

Bézier Curves

 • Use the slider to select $n$, the degree of the Bézier curve.

n =

 • Press  to initialize/clear the plot window.
 • Click on the plot area and select the Click and Drag Manipulator ( ) from the Plot menu or plotting toolbar.
 • Click to insert $n+1$ control points ${\mathbf{P}}_{k},k=0,\dots ,n.$
 • Drag control points to modify the Bézier curve.
 • Find the Bézier curve

$\mathbf{R}=$

Create an Interpolation with Equispaced Points

The Interpolation with Equispaced Points Task Template is also interactive:

 Interpolation with Equispaced Points: Enter $f$ and the interval $\left[a,b\right]$ $f=$ $a$ =                $b$ = Interpolation Type Number of Points in Addition to $a$ and $b$   $N=$  =

Overview of Packages Related to CurveFitting.

This page provided an introduction to the CurveFitting package, which contains the following command to fit a curve to your input data:

 •
 •
 •
 •
 •
 •
 •
 •

The commands within this package are the same commands that are called by the Curve Fitting Assistant.  After you have determined the command that best suites your data set, you can call the command directly without needing the Curve Fitting Assistant.  More information on each of the commands, including options available with each command, can be found in the help pages.

The Interpolation package provides more interpolation functionality, including a easy-to-use model for inputting the data once and then using it like a normal mathematical function, for example, plotting, integration, or just to evaluate at a given point.  It includes the ability to interpolate over irregularly spaced data in 2 (or more) dimensions, and includes some additional methods of integration, such as Kriging.