Chapter 4: Partial Differentiation
Section 4.8: Unconstrained Optimization
|
Example 4.8.10
|
|
Choose and so that the line minimizes , the sum of squares of the deviations from the points to the line. Such a line is called the "least-squares" line.
|
|
|
|
Solution
|
|
|
Mathematical Solution
|
|
•
|
Figure 4.8.10(a) shows the least-squares line in red, and the four given data points in green.
|
•
|
The deviation of the data point from the graph of the function is measured vertically, and is given by the difference . The sum of the squares of deviations of the given data points from the line is given by
|
|
>
|
use plots in
module()
local p1,p2,p3:
p1:=pointplot([1,3,5,6],[5.3,8.8,12.5,15.4],symbol=solidcircle,symbolsize=15,color=green);
p2:=plot(1.972881356*x+3.101694915,x=0..7,color=red);
p3:=display(p1,p2,scaling=constrained);
print(p3);
end module:
end use:
|
|
Figure 4.8.10(a) Data and least-squares line
|
|
|
|
|
|
=
•
|
To minimize , solve the equations for and .
|
•
|
The least-squares line is then .
|
|
|
Maple Solution - Interactive
|
|
In Table 4.8.10(a), the required least-squares line is constructed from first principles. The first step is to decide an appropriate form for the data, either a list of abscissas and ordinates, or a list of data points.
Define lists of abscissas and ordinates
|
•
|
Context Panel: Assign Name
|
|
|
•
|
Context Panel: Assign Name
|
|
|
Define , the sum of squares of deviations
|
•
|
Expression palette: Summation template
|
•
|
Context Panel: Assign Name
|
|
|
Minimize
|
•
|
Calculus palette: Partial-differentiation template
|
•
|
Context Panel: Solve≻Solve
|
•
|
Context Panel: Assign to a Name≻
|
|
|
Obtain the least-squares line
|
•
|
Expression palette: Evaluation template
Press the Enter key.
|
|
|
Table 4.8.10(a) Construction of least-squares line from first principles.
|
|
|
Table 4.8.10(b) illustrates how the Context Panel provides for the immediate construction of the least-squares line. However, the data must appear as a list-of-lists for this to work.
•
|
Write the generic data point.
Context Panel: Sequence≻ (from 1 to 4)
|
•
|
Context Panel: Conversions≻To List
|
•
|
Context Panel: Curve Fitting≻Least Squares
|
|
|
Table 4.8.10(b) Least-squares line via the Context Panel
|
|
|
There are several other tools in Maple for obtaining a least-squares line. Table 4.8.10(c) illustrates the
Assistant, accessed through the Context Panel launched on a sequence of the lists of abscissas and ordinates for the data points. Figure 4.8.10(b) shows the state of the Assistant after the Plot button in the Least-Squares section, and the Default button to the right of "Vertical range" have been clicked. The expression for the least-squares line appears under the graph, and either this expression or the graph can be chosen as the return when the "Done" button is clicked.
The Curve-Fitting Assistant is no longer listed in Tools≻Assistants. It is now found in Tools≻Tutors≻Statistics≻Curve Fitting.
Launch the Curve-Fitting Assistant from the Context Panel
|
•
|
Context Panel: Evaluate and Display Inline
|
•
|
Context Panel: Curve Fitting≻Interactive Curve Fitting
|
|
=
|
|
Figure 4.8.10(b) Least-squares line by Curve-Fitting Assistant
|
|
|
|
Table 4.8.10(c) Least-squares line via the Curve-Fitting Assistant
|
|
|
Table 4.8.10(d) illustrates the
task template. In addition to returning the least-squares line and the minimum value of , the sum of squares of deviations, the template draws a graph of the data points and the least-squares line annotated with small squares near the data points. The area of a square is in proportion to the square of the deviation at that point.
Tools≻Tasks≻Browse: Linear Algebra≻Visualizations≻Least Squares Plot
|
Least Squares Plot
|
Data Entry
Data can be entered in one of the following formats:
•
|
List of lists (either 2-D or 3-D)
|
•
|
Two lists of coordinates (for 2-D)
|
•
|
Three lists of coordinates (for 3-D)
|
|
|
Fitting Function
•
|
If the fitting function is linear (line in 2-D or plane in 3-D), a function need not be entered.
|
•
|
If a formula for a curve or surface (linear in the unknown parameters) is optionally entered, variables must be explicitly declared, below.
|
|
|
Variables
•
|
The default line will use the variables .
|
•
|
The default plane will use the variables
|
•
|
Optionally, these defaults can be overridden.
|
•
|
If a fitting function has been given explicitly, variables must be declared as a sequence or list.
|
|
|
Click the icon below to generate the Least Squares Plot.
|
|
|
|
|
Table 4.8.10(d) The Least-Squares Plot task template
|
|
|
|
|
Maple Solution - Coded
|
|
Solution from first principles:
Enter the data
|
•
|
Define lists of the abscissas and ordinates.
|
|
|
Obtain , the sum of squares of the deviations
|
|
|
Minimize
|
•
|
Use the diff command to form equations and the solve command to solve them for and .
|
|
|
Insert the optimal values of and into the equation
|
|
|
|
|
To obtain just the least-squares line:
•
|
Apply the LeastSquares command from the CurveFitting package.
|
|
|
|
|
To obtain a figure like the one in Table 4.8.10(d), use the LeastSquaresPlot command from the Student LinearAlgebra package. See Table 4.8.10(e).
|
Table 4.8.10(e) Least-squares line via the LeastSquaresPlot command
|
|
|
|
|
|
<< Previous Example Section 4.8
Next Example >>
© Maplesoft, a division of Waterloo Maple Inc., 2024. All rights reserved. This product is protected by copyright and distributed under licenses restricting its use, copying, distribution, and decompilation.
For more information on Maplesoft products and services, visit www.maplesoft.com
|