DERIVE for Windows version 5.05 DfW file saved on 30 Sep 2002
eqns:=VECTOR(ptsi2 = aptsi1^3 + bptsi1^2 + cptsi1 + d, i, 1, 10)
hCross:=APPROX(17903225806451613/10000000000000000)
loc:=[3, 1; -3, -4; 1, -3; 1, 3; -1, -2; -3, 3; -1, -4]
pts2:=[-2, 1; -4, 0; 5, 5; 4, 0; 4, -1; 3, 0; 5, -6; 5, 2; -4, -6]
result:=[5c^2 - 10c + 10 = r^2, 5c^2 + 22c + 25 = r^2, 5c^2 + 10c + 10 = r^2, 5c^2 - 14c + 10 = r^2, 5(c + 1)^2 = r^2, 5c^2 - 6c + 18 = r^2, 5c^2 + 18c + 17 = r^2]
vCross:=APPROX(5862068965517241/2500000000000000)
CTextObj{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset0 Times New Roman;}{\f2\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;\red0\green0\blue160;}
\viewkind4\uc1\pard\qc\cf1\b\f0\fs24 Trying to Find a 'Best' Fit
\par A Heuristic Approach
\par \pard\b0
\par \cf2 NOTE: While working with this lesson, we recommend that you open a \f1 2D-plot\f0 \f1 W\f0 indow and then choose the option Window > Tile Vertically. That way you will be able to follow the lesson and display your graphical results on the same page.
\par \cf1
\par \b Example:\b0
\par
\par Suppose that a department store chain has stores scattered about a certain region. The store executives would like to build a warehouse that will service the stores. The optimal location for the warehouse is to place it where it is close to the center of a circle that passes through all of the stores in the region. Placing a coordinate system over the region, each of the stores corressponds to a point, (\i x\i0 , \i y\i0 ), on this coordinate system. Now if each of the stores lay on some circle with center at (\i c\i0 1, \i c\i0 2) and radius, \i r\i0 , then each of the coordinates for the store would satisfy the equation:\f2
\par }
CExpnObj8User(x-c1)^2+(y-c2)^2=r^2 Y{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset0 Times New Roman;}{\f2\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;\red255\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 However, when we substitute the coordinates of a point into this equation there are only three unknown values: \i c\i0 1, \i c\i0 2, and \i r\i0 . That means that if there are not exactly three stores, we would be extremely lucky to have all of the stores lie on the same circle. \cf2 Convince yourself of this by generating seven random points using the following expression and plot\f1 t\f0 ing them. Save these points as the values for a variable called \i loc\i0 .\cf1\f2
\par }
8eqUser'VECTOR([RANDOM(8)-4,RANDOM(8)-4],i,1,7)}{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset0 Times New Roman;}{\f2\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 Now using the equation of a circle in expression #1 try to find a circle that comes "close" to all of the points. How did you do?\cf2 Well, that depends on the definition of "close" that you use. The generally accepted mathematical definition is that the sum of the squares of the distances of each point from the circle is a minimum. To implement this definition requires the use of calculus. We will take a different approach, based on a \ul heuristic\ulnone , or a seemingly common sense approach. It turns out that this approach also yi\f1 e\f0 lds a result that agrees with the mathematical definition. First we generate all of the equations that can result from the equation of the circle (expression #1) and the random points, \i loc\i0 , that we generated. \f2
\par }
8hUserNresult:=VECTOR((loc SUB i SUB 1-c1)^2+(loc SUB i SUB 2-c2)^2=r^2,i,1,DIM(loc))Q{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 This is a set of seven equations, four more than we need. As we mentioned earlier, it is highly unlikely that values for \i c\i0 1, \i c\i0 2, and \i r\i0 that satisfy one subset of three of these equations that will satisfy all of the others. We need some strategy to reduce this set of equations to three equations in a sensible way. Suppose we take an "average" equation representing the set \i result\i0 . When you evaluate the following expression, you have this "average" equation. \f1
\par }
8]User)1/DIM(loc)*SUM(result SUB i,i,1,DIM(loc)){\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 But, there is more than one way to take an average. We might consider weighting the average by the \i x\i0 - and \i y\i0 - coordinates of the points that are under consideration. This makes sense since the location of the center is going to be influenced by the location of these points. If more lie to the left of origin than the right, the center will be pulled in that direction. Likewise, if more lie above the origin than below it, the center of the circle will be pulled in that direction. Expressions #5 and #6 give these weighted averages.\f1
\par }
8H User91/DIM(loc)*SUM(loc SUB i SUB 1*result SUB i,i,1,DIM(loc))8H9User91/DIM(loc)*SUM(loc SUB i SUB 2*result SUB i,i,1,DIM(loc))Ek{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset0 Times New Roman;}{\f2\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 Evaluate the expressions in #4, #5, and #6. Save these results in a vector called \i eqns\i0 .\cf2 Now we have a system \f1 of \f0 exactly 3 equations in 3 unknowns. Furthermore, this system uses all of the information that we have from our orig\f1 i\f0 nal set of points. \cf1 Now solve this set of equations for \i c\i0 1, \i c\i0 2\f1 ,\f0 and \i r \i0 using the expression:\f2
\par }
8wUserSOLVE(eqns,{c1,c2,r}){\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset0 Times New Roman;}{\f2\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;\red255\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 This expression generates more than one set of values for \i c\i0 1, \i c\i0 2, and \i r\i0 . \cf2 Choose the correct set and substitute these values into expression #1. Plot the result. How does this circle compare with your earlier attempt\f1 ?\cf1\f0 As was mentioned earlier, when you take calculus, you will be able to "prove" that this is the correct solution to the stores problem.
\par \b
\par \ul\b0 Exercise\ulnone
\par \cf2 1) Another region contains 9 department stores. Generate a set of 9 random points and find the "best" location for the distribution center in this situation.
\par \cf1
\par \b Other Examples:\b0
\par
\par Real world data generally are going to have some "noise" and not conform to a nicely stated law. Measurement errors, incorrect reporting, and inaccurate answers all lead to data that may appear not to conform to a given relationship between the independent and dependent variables. If we have confidence in the relationship, we may want to find a curve that conforms to that relationship and yields a "best fit" to the data.
\par
\par Suppose that \i x\i0 and \i y\i0 satisfy a cubic equation relationship as illustrated below:\f2
\par }
8Usery=a*x^3+b*x^2+c*x+d{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 Here we have four parameters, \i a\i0 . \i b, c\i0 , and \i d\i0 that determine the exact curve. This means that we need to come up with four equations in four unknowns. We start as before and generate 10 points at random.\f1
\par }
8User *VECTOR([RANDOM(12)-6,RANDOM(12)-6],i,1,10)L{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 Evaluate this expression and save the result as the variable \i pts\cf2\i0 . We obviously have too many points to generate one cubic curve that will pass through all of them. This is what is called an \ul over determined system\ulnone . We proceed as before. First generate all possible cubic relationships between the value for \i y\i0 and the values for \i x\i0 , \i x^\i0 2, and \i x\i0 ^3 given our values in \i pts\i0 .\f1
\par }
8Xp|User
`eqns:=VECTOR(pts SUB i SUB 2=a*pts SUB i SUB 1^3+b*pts SUB i SUB 1^2+c*pts SUB i SUB 1+d,i,1,10)@{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 These are equations in \i a\i0 , \i b\i0 , \i c\i0 , and \i d.\i0 We find the "average" equation as above.\f1
\par }
8User!1/DIM(pts)*SUM(eqns SUB i,i,1,10){\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 We leave it to you to write the expressions for the weighted averages for \i x\i0 , \i x\super\i0 2\nosupersub , and \i x\super\i0 3\nosupersub and evaluate them. Follow the example given in the location for the distribution center.
\par \cf2
\par \ul Exercises\ulnone
\par \cf1 2) Following the example given above, create a vector called \i eqns\i0 containing the average equation and the three weighted average equations.
\par 3) Solve this equation for the values \i a\i0 ,\i b\i0 ,\i c\i0 , and \i d\i0 . Plot the resulting cubic on the same graph as the 10 points.
\par \cf2
\par We conclude with a slightly more challenging exercise, but not much different than we have done before. Most routines to do cubic regression find an expression of the form given in expression #8. In this example we are going to insist that the resulting equation does not have a constant term, i.e. that it be of the form:\f1
\par }
8Usery=a*x^3+b*x^2+c*xpf{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset0 Times New Roman;}{\f2\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;\red255\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 Since we are showing the method in general, we do not have a set of data points. You will need to generate them. Following the previous examples for generating \i loc\i0 and \i pts\i0 generate a set of random points to display on the graph. If any points are generated that lie on the \i y\i0 -axis, discard them. Just be sure that your system is over determined (in this case contains more than three points). Now, it\f1 '\f0 s up to you to decide what we\f1 i\f0 ghted average equations you need to choose to generate the "best fitting" cubic.
\par
\par \ul Exercises\ulnone
\par \cf2 4) After you choose three weighted average equations, create them and solve the resulting system of three equations for \i a\i0 , \i b\i0 , and \i c\i0 . HINT: Since \i x\i0 is not zero for any of the data points, you can divide expression #12 by \i x\i0 and the result is a quadratic.
\par 5)Plot the resulting regression equation.
\par 6)Check your result using the built\f1 -\f0 in Derive 5 function, FIT. (Yes, there is a built\f1 -\f0 in function, but we wanted you to understand how it works.) \f2
\par }
8|@User
FIT([x,a*x^3+b*x^2+c*x],pts2)5{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern\fprq1 Times New Roman;}{\f1\fmodern\fprq1\fcharset2 DfW5 Printer;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs24 The first vector lists the independent variable(s) followed by the form of the dependent variable. The variable \i pts\i0 2 is the array of points, (\i x\i0 , \i y\i0 ) that you generated at the beginning of this section and used to create your weighted average equations in Exercise 4.
\par
\par \fs20 Carl Leinbach: leinbach@cs.gettysburg.edu\f1\fs24
\par }