Chapter 4: Partial Differentiation
Section 4.8: Unconstrained Optimization
Example 4.8.12
By minimizing the sum of squares of deviations S=∑k=15fxk−yk2 between fx=a x2+b x+c and the points 1,3,2,8,3,15,4,30,5,38, obtain the best least-squares quadratic fit to the data.
Solution
Mathematical Solution
Figure 4.8.12(a) shows the least-squares quadratic in red, and the four given data points in green.
The deviation of the data point xk,yk from the graph of the function fx is measured vertically, and given by the difference fxk−yk. The sum of the squares of deviations of the given data points from the line y=a x+b is given by
use plots in module() local p1,p2,p3: p1:=pointplot([1,2,3,4,5],[3,8,15,30,38],symbol=solidcircle,symbolsize=15,color=green); p2:=plot(-9/5+(16/5)*x+x^2,x=0..6,0..40,color=red); p3:=display(p1,p2,scaling=constrained); print(p3); end module: end use:
Figure 4.8.12(a) Data and least-squares line
S
=∑k=15fxk−yk2
=979⁢a2+450⁢a⁢b+110⁢a⁢c+55⁢b2+30⁢b⁢c+5⁢c2−3200 a−748 b−188 c+2642
To minimize S, solve the equations ∂S∂a=∂S∂b=∂S∂c= for a=1, b=16/5 and c=−9/5.
The least-squares quadratic is then y=x2+16 x/5−9/5.
Maple Solution - Interactive
In Table 4.8.12(a), the required least-squares quadratic 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
X=1,2,3,4,5→assign
Y=3,8,15,30,38→assign
Context Panel: Assign Function
fx=a x2+b x+c→assign as functionf
Define S, the sum of squares of deviations
Expression palette: Summation template
S=∑k=15fXk−Yk2→assign
Minimize S
Calculus palette: Partial-differentiation template
Context Panel: Solve≻Solve
Context Panel: Assign to a Name≻Q
∂∂ a S=0,∂∂ b S=0,∂∂ c S=0
1958⁢a+450⁢b+110⁢c−3200=0,450⁢a+110⁢b+30⁢c−748=0,110⁢a+30⁢b+10⁢c−188=0
→solve
a=1,b=165,c=−95
→assign to a name
Q
Obtain the least-squares quadratic
Expression palette: Evaluation template Press the Enter key.
fxx=a|f(x)Q = −95+165⁢x+x2
Table 4.8.12(a) Construction of least-squares line from first principles.
Table 4.8.12(b) 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.12(b) shows the state of the Assistant after entering fx into the Expression box in the Least Squares section, and pressing the Plot button to its right. The expression for the least-squares quadratic 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
X,Y = 1,2,3,4,5,3,8,15,30,38
Figure 4.8.12(b) Least-squares line by Curve-Fitting Assistant
Table 4.8.12(b) Least-squares quadratic via the Curve-Fitting Assistant
Table 4.8.12(c) illustrates the task template. In addition to returning the least-squares quadratic and the minimum value of S, 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.
Line or PlaneOther
Variables
The default line will use the variables x,y.
The default plane will use the variables x,y,z.
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.
Fitting curve: -1.800+3.200*x+1.000*x^2 Least squares error: 3.795 Maximum error: 3.000
Table 4.8.12(c) The Least-Squares Plot task template
Maple Solution - Coded
Solution from first principles:
Enter the data
Define lists of the abscissas and ordinates.
X,Y≔1,2,3,4,5,3,8,15,30,38:
Define the quadratic fitting function fx
Use the "arrow" notation for defining fx.
f≔x→a x2+b x+c:
Obtain S, the sum of squares of the deviations
Apply the sum command.
S≔sumfXk−Yk2,k=1..5:
Use the diff command to form equations and the solve command to solve them for a,b, and c.
Q≔solvediffS,a=0,diffS,b=0,diffS,c=0,a,b,c:
Insert the optimal values of a,b, and c into fx
Use the eval command.
evalfx,Q
−95+165⁢x+x2
To obtain just the least-squares quadratic:
Apply the LeastSquares command from the CurveFitting package.
CurveFitting:-LeastSquaresX,Y,x,curve=fx
To obtain a figure like the one in Table 4.8.12(c), use the LeastSquaresPlot command from the Student LinearAlgebra package. See Table 4.8.12(d).
Student:-LinearAlgebra:-LeastSquaresPlotX,Y,x,curve=fx,boxoptions=color=magenta,pointoptions=symbol=solidcircle,symbolsize=15
Table 4.8.12(d) Least-squares line via the LeastSquaresPlot command
<< Previous Example Section 4.8 Next Section >>
© 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
Download Help Document