{VERSION 2 3 "IBM INTEL NT" "2.3" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 }{CSTYLE "2D Math" -1 2 "Times" 0 1 107 110 111 0 0 0 2 0 0 0 0 0 0 }{CSTYLE "Hyperlink" -1 17 "" 0 1 0 128 128 1 0 0 1 0 0 0 0 0 0 }{CSTYLE "2D Output" 2 20 "" 0 1 0 0 255 1 0 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 256 "" 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 }{CSTYLE "" -1 257 "" 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 }{CSTYLE "" -1 258 "" 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 }{PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "Times" 0 12 0 0 0 1 2 2 2 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 } {PSTYLE "Heading 1" 0 3 1 {CSTYLE "" -1 -1 "" 1 18 158 0 132 0 0 1 0 0 0 0 0 0 0 }1 0 0 0 6 6 0 0 0 0 0 0 -1 0 }{PSTYLE "Maple Output" 0 11 1 {CSTYLE "" -1 -1 "" 0 1 10 0 0 0 0 0 0 0 0 0 0 0 0 }3 3 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Maple Plot" 0 13 1 {CSTYLE "" -1 -1 "" 0 1 182 0 99 0 0 0 0 0 0 0 0 0 0 }3 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 } {PSTYLE "Bullet Item" 0 15 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 3 3 0 0 0 0 0 0 15 2 }{PSTYLE "R3 Font 0" -1 256 1 {CSTYLE "" -1 -1 "Helvetica" 0 12 255 0 0 1 2 1 2 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "R3 Font 2" -1 257 1 {CSTYLE "" -1 -1 "Courier" 0 12 0 0 0 1 2 2 2 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "" 3 258 1 {CSTYLE "" -1 -1 "" 0 1 245 0 0 0 0 0 0 0 0 0 0 0 0 }3 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }} {SECT 0 {SECT 0 {PARA 258 "" 0 "" {TEXT -1 29 "Least Squares Approxima tion. " }}{PARA 3 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 1 " " }{TEXT 256 9 "Problem: " }{TEXT -1 335 "Given some data [xi,yi] i = 1..m , that a function f(x) = a1 u1(x) + a2 u2(x) + .. . + an un(x) is supposed to give the values yi = f(xi) for each i, \+ find the values of a1 through an which come the nearest to satisfyin g the equations in the senses that the sum of the squares of the err ors (f(xi)-yi) is as small as possible. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 1 " " }{TEXT 258 18 "Calculus solution " }{TEXT -1 70 "for a sample set of data that we want to do a linear \+ regression on. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 299 " Here the idea is to set up the linear function f with u nknown coefficients a1 and a2. So here u1(x) = x and u2(x) = 1. Then \+ set up the error function e(a1,a2) and find the critical point by l ocating where both partial derivatives vanish. The minimum error is attained at that critical point." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 42 "data := [[.3,1],[2,4],[4,2],[7,9],[10,3]];" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%%dataG7'7$$\"\"$!\"\"\"\"\"7$\"\"#\"\"%7$F-F,7$\"\"( \"\"*7$\"#5F(" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "f := x -> \+ a1*x + a2;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"fG:6#%\"xG6\"6$%)ope ratorG%&arrowGF(,&*&%#a1G\"\"\"9$F/F/%#a2GF/F(F(" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 80 "i := 'i' : e := (a1,a2) -> sum((f(data[i][1]) -data[i][2])^2, i =1..nops(data));" }}{PARA 11 "" 1 "" {XPPMATH 20 "6 #>%\"eG:6$%#a1G%#a2G6\"6$%)operatorG%&arrowGF)-%$sumG6$*$,&-%\"fG6#&&% %dataG6#%\"iG6#\"\"\"F;&F66#\"\"#!\"\"F>/F9;F;-%%nopsG6#F7F)F)" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "e(a,b);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#,,*$,(%#a1G$\"\"$!\"\"%#a2G\"\"\"F)F+\"\"#F+*$,(F&F,F*F +!\"%F+F,F+*$,(F&\"\"%F*F+!\"#F+F,F+*$,(F&\"\"(F*F+!\"*F+F,F+*$,(F&\"# 5F*F+!\"$F+F,F+" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "eqn1 := \+ diff(e(a1,a2),a1);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%%eqn1G,(%#a1G$ \"&=Q$!\"#%#a2G$\"$m%!\"\"$!%'=#F-\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "eqn2 := diff(e(a1,a2),a2);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%%eqn2G,(%#a1G$\"$m%!\"\"%#a2G\"#5!#Q\"\"\"" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 35 "sol1 := solve(\{eqn1,eqn2\}, \{a1,a2\});" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%%sol1G<$/%#a2G$\"+!R# G,A!\"*/%#a1G$\"+%[C2V$!#5" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 71 "pl1 :=plot(\{[0,0],seq(d,d=data )\} ,style=point,symbol=box,color= black):" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 68 "plots[display]([ plot(\{ subs(sol1,f(x))\},x=-2..10),pl1],color=black);" }}{PARA 13 "" 1 "" {INLPLOT "6'-%'CURVESG6$7S7$$!\"#\"\"!$\"1++?$\\P^^\"!#:7$$!1+++] TVQr%>F-7$$!1,++DM5J]F@$\"1iJ)H4z'G?F-7$$!1,++D!G&pDF@$\"1$3BH'* GJ6#F-7$$!1s*****\\AjP#!#=$\"1P#G!G)>FF-7$$\"1++]xq!*Qf -s6h%\\F-7$$\"1+++0\"Q_D)F-$\"1Et !)QrUL]F-7$$\"1,+]x2k2&)F-$\"10YkT&>+7&F-7$$\"1,++?EdR()F-$\"1.Irm*)e* >&F-7$$\"1+++&o#R0!*F-$\"1+kC2Xy!H&F-7$$\"1+++?`9V#*F-$\"1-%\\e)3Ns`F- 7$$\"1++]<#Rm\\*F-$\"1%=>zl<$faF-7$$\"1++]A_ER(*F-$\"1X+]bfbUbF-7$$\"# 5F*$\"1+++uo+KcF--%'COLOURG6&%$RGBG$Fgz!\"\"F*F*-F$6&7(7$F*F*7$$\"\"%F *$\"\"#F*7$$\"\"(F*$\"\"*F*7$Ffz$\"\"$F*7$$\"1+++++++IF@$\"\"\"F*7$Fg[ lFe[l-F[[l6&F][lF*F*F*-%'SYMBOLG6#%$BOXG-%&STYLEG6#%&POINTG-%+AXESLABE LSG6$%\"xG%!GFg\\l-%%VIEWG6$;F(Ffz%(DEFAULTG" 2 397 397 397 2 0 1 0 2 6 0 4 2 1.000000 45.000000 45.000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 103 "Th e average error in the data would be obtained by taking the square ro ot of the average square error." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 37 " sqrt(subs(sol1,e(a1,a2))/nops(data));" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#$\"+oW0 " 0 "" {MPLTEXT 1 0 13 "with(linalg):" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 70 "A : = transpose( matrix(2,nops(data),[seq(data[i][1],i=1..nops(data))," } }{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " seq(1,i=1..nops(data))]));" } }{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"AG-%'MATRIXG6#7'7$$\"\"$!\"\"\"\" \"7$\"\"#F-7$\"\"%F-7$\"\"(F-7$\"#5F-" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 " A := evalf(evalm(A));" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"AG-%'MATRIXG6#7'7$$\"\"$!\"\"$\"\"\"\"\"!7$$\"\"#F/F-7$$\"\" %F/F-7$$\"\"(F/F-7$$\"#5F/F-" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 39 "b := [seq(data[i][ 2],i=1..nops(data))]:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 59 "xb ar := evalm(inverse( transpose(A)&*A)&*transpose(A)&*b );" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%%xbarG-%'VECTORG6#7$$\"+$\\C2V$!#5$\"+\"R#G,A !\"*" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 64 "Note: linalg has \+ a word to do leastsquare approximation already." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "leastsqrs(A,b);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#-% 'VECTORG6#7$$\"+zWsIM!#5$\"+$R#G,A!\"*" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 1 " " }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 1 " " }}{PARA 0 "" 0 "" {TEXT -1 11 "Problems: " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 105 " 1. In the data above, replace the da ta point [2,4] with [2,t]. Then find the smallest positive t " }} {PARA 0 "" 0 "" {TEXT -1 63 " so that the average error in the data i s 5 or greater. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 398 "2. Using the original data above, find the q uadratic function f(x) = a1 x^2 + a2 x + a3 and the function f(x) \+ = a1 exp(x) + a2 x + a3 which come nearest fittting the data in th e sense of least squares. Plot both of these functions together wi th the data on the same graph and decide which fits the data better. \+ Does either of these fit the data better than the linear function?" } }{PARA 0 "" 0 "" {TEXT -1 1 " " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 300 "QR factorization. The sticky place in \+ the calculation of either a projection matrix or a least squares appro ximation is in finding the inverse of transpose(A)&* A. This can b e improved by replacing the colums of A with an orthonormal set. This is called the QR factorization (or Gramm Schmidt)" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 57 "1 First make A into Q', a matrix with orthogonal columns." }}{PARA 0 "" 0 "" {TEXT -1 71 "2. Th en make Q' into an orthogonal matrix Q by normalizing its columns." }} {PARA 0 "" 0 "" {TEXT -1 40 "3. Then get R = transpose(Q)&*A. done. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 78 "The w ord proj gives the projection of a vector onto a line through the orig in." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "proj := proc(b,a)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 49 " evalm(dotprod(b,a)/dotprod(a,a) *a) end;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%%projG:6$%\"bG%\"aG6\"F) F)-%&evalmG6#*(-%(dotprodG6$9$9%\"\"\"-F/6$F2F2!\"\"F2F3F)F)" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 4 " " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "proj([3,4,5],[3,4,2]);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#-%'VECTORG6#7%#\"$0\"\"#H#\"$S\"F)#\"#qF)" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 67 "Now to get the QR factorization of a rand omly chosen 4 by 3 matrix." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "A := randmatrix(4,3);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"AG-%'MATRIXG6#7&7%!#b!#P!#N7%\"#(*\"#]\"#z7%\"#c\"# \\\"#j7%\"#d!#f\"#X" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 1 " " }} }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 " seq(col(A,i),i=1..3);" }} {PARA 11 "" 1 "" {XPPMATH 20 "6%-%'VECTORG6#7&!#b\"#(*\"#c\"#d-F$6#7&! #P\"#]\"#\\!#f-F$6#7&!#N\"#z\"#j\"#X" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 " q1 := \+ col(A,1);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#q1G-%'VECTORG6#7&!#b\" #(*\"#c\"#d" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 42 "q2 := evalm( col(A,2)-proj(col(A,2), q1 ));" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%#q 2G-%'VECTORG6#7&#!'t;N\"&>)=#\"'[JLF+#\"'N7dF+#!'h\"*[\"%ti" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 59 "q3 := evalm(col(A,3)-proj(co l(A,3), q1)-proj(col(A,3),q2));" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%# q3G-%'VECTORG6#7&#\"+pfGA<\"*88CW\"#!*wp=:%F+#\"+Uw>'4#F+#\"*&*)y*3$F+ " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 69 " for i from 1 to 3 do \+ q.i := evalm(1/sqrt(dotprod(q.i,q.i))*q.i) od:" }}}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 33 "Q := transpose(stack (q1,q2,q3));" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"QG-%'MATRIXG6#7&7%,$*$\"%\"4##\"\"\"\"\"##! #b\"%ti,$*$\"-$[&e3;IF-#!'t;N\"-\\kvD[!*,$*$\"4EZ2Ofph\"GwF-#\"+pfGAF,,$F4#!'h\"*[F5,$F:#\"*&*)y*3$F; " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "R :=evalm(transpose(Q)& *A);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"RG-%'MATRIXG6#7%7%,$*$\"% \"4##\"\"\"\"\"#\"\"$,$F+#\"%mi\"%ti,$F+#\"%F_F,7%\"\"!,$*$\"-$[&e3;IF -#F.F4,$F;#\"(R%fGF<7%F9F9,$*$\"4EZ2Ofph\"GwF-#F.\"*88CW\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 2 " " }}{PARA 0 "" 0 "" {TEXT -1 223 "Problem. Define a word \+ getqr(A,`Q`,`R`) which takes an m by n matrix A of rank n <= m and \+ returns an orthogonal matrix Q and an upper triangular matrix R such that A = Q R. Test your word out on several matrices." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 15 "" 0 "" {HYPERLNK 17 "Back" 1 "s yll322.mws" "" }}}}}{MARK "0 35 1 0" 0 }{VIEWOPTS 1 1 0 1 1 1803 }