Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 15
Kernel: SageMath (stable)

Part 1.

Consider the data set (1,0),(2,1),(4,2),(5,4)(1,0),(2,1),(4,2),(5,4)

A = matrix(QQ,[[1, 1],[1,2],[1,4],[1,5]]) AT=A.transpose() A1 = AT*A b = matrix(QQ,[[0],[1],[2],[3]]) b1 = AT*b M = A1.augment(b1) f(x)=M.rref()[0][2]+M.rref()[1][2]*x P = point([(1,0),(2,1),(4,2),(5,3)], size=40) P += plot(f,[-1,6],gridlines='true', legend_label='Least Squares Line') show( M, M.rref(), f(x)) show(P) P.save('plot1lab3_part1.png') show(A, b) latex(b)
Image in a Jupyter notebook
\left(\begin{array}{r} 0 \\ 1 \\ 2 \\ 3 \end{array}\right)
show(A1.column(0).dot_product(A1.column(1)))

Part 2.

Consider the data set (1,5),(2,1),(4,2),(5,3)(1, 5),(2, 1),(4, 2),(5, 3). How does the line of best change compared to Part 1?

# reset() A = matrix(QQ,[[1, 1],[1, 2],[1,4],[1,5]]) AT=A.transpose() A1 = AT*A b = matrix(QQ,[[5],[1],[2],[3]]) b1 = AT*b M = A1.augment(b1) show(M, M.rref()) P1 = point([(1, 5),(2, 1),(4, 2),(5, 3)], size=40) f(x)=M.rref()[0][2]+M.rref()[1][2]*x P = plot(f,[-1,6],gridlines='true', legend_label='Least Squares Line') Q = P+P1 show(Q) Q.save('plot1lab3_part2.png') show("y = ", f(x)) latex(f(x))
Image in a Jupyter notebook
-\frac{3}{10} \, x + \frac{73}{20}

Part 3

Find the line of best fit for the points (4,1.58),(6,2.08),(8,2.5),(10,2.8),(12,3.1),(14,3.4),(16,3.8),(18,4.32)(4, 1.58), (6, 2.08), (8, 2.5), (10, 2.8), (12, 3.1), (14, 3.4), (16, 3.8), (18, 4.32).

reset() A = matrix(QQ,[[1, 4],[1, 6],[1, 8],[1, 10],[1, 12],[1, 14],[1, 16],[1, 18]]) AT=A.transpose() A1 = AT*A b = matrix(QQ,[[1.58],[2.08],[2.5],[2.8],[3.1],[3.4],[3.8],[4.32]]) b1 = AT*b M = A1.augment(b1) f(x)=M.rref()[0][2]+M.rref()[1][2]*x show(M, " RREF ", M.rref(), " from which we obtain y = ", f(x)) P1 = point([(4, 1.58), (6, 2.08), (8, 2.5), (10, 2.8), (12, 3.1), (14, 3.4), (16, 3.8), (18, 4.32)], size=40) P = plot(f,[0,20],gridlines='true', legend_label='Least Squares Line') Q = P+P1 show(Q) Q.save('plot1lab3_part3.png') latex(f(x))
Image in a Jupyter notebook
\frac{513}{2800} \, x + \frac{261}{280}

Part 4

An experiment produces the data (1,7.9),(2,5.4),(3,0.9)(1, 7.9), (2, 5.4), (3, -0.9). Solve the equation ATAβ^=ATbA^T A \hat{\beta} = A^T\textbf{b} to find the least-squares curve to fit the data, where the curve has the form y=β0cosx+β1sinxy = \beta_0 \cos{x} + \beta_1 \sin{x}.

reset() A = matrix(RDF, [[cos(1), sin(1)],[cos(2),sin(2)],[cos(3), sin(3)]]) b = matrix(RDF, [[7.9],[5.4], [-0.9]]) AT=A.transpose() A1 = AT*A b1 = AT*b M = A1.augment(b1) f(x)=M.rref()[0][2]*cos(x)+M.rref()[1][2]*sin(x) show(M.n(digits = 3), "RREF ", M.rref().n(digits = 3), " from which we obtain y = ", f(x)) P = point([(1, 7.9), (2, 5.4), (3, -0.9)], size=40) P += plot(f,[0,5],gridlines='true', legend_label='Least Squares Line') show(P) P.save('plot1lab3_part4.png')
Image in a Jupyter notebook

Part 5

(10,21.34),(11,20.68),(12,20.05),(14,18.87),(15,18.30)(10,21.34), (11,20.68), (12,20.05), (14,18.87), (15,18.30)

y=βAe(0.02t)+βBe(0.07t)y = \beta_A e^{(-0.02t)} + \beta_B e^{(-0.07t)}

A = matrix([[exp(-0.02*10), exp(-0.07*10)], [exp(-0.02*11), exp(-0.07*11)], [exp(-0.02*12), exp(-0.07*12)], [exp(-0.02*14), exp(-0.07*14)], [exp(-0.02*15), exp(-0.07*15)]]) b = matrix([[21.34],[20.68],[20.05],[18.87],[18.30]]) AT=A.transpose() A1 = AT*A b1 = AT*b M = A1.augment(b1) f(x)=M.rref()[0][2]*exp(-0.02*x) + M.rref()[1][2]*exp(-0.07*x) show(M.n(digits = 3), "RREF ", M.rref().n(digits = 3), " from which we obtain y = ", f(x)) show(f(x)) P = point([(10,21.34), (11,20.68), (12,20.05), (14,18.87), (15,18.30)], size=40) P += plot(f,[0,25],gridlines='true', legend_label='Least Squares Line') show(P) P.save('plot1lab3_part5.png')
Image in a Jupyter notebook

Part 6

Data: (44,91),(61,98),(81,103),(113,110),(131,112)(44,91), (61, 98), (81, 103), (113, 110), (131, 112)

Curve: p=β0+β1lnwp = \beta_0 + \beta_1 \ln{w}

A = matrix(RDF,[[1, log(44)], [1, log(61)], [1, log(81)], [1, log(113)], [1, log(131)]]) b = matrix([[91],[98],[103],[110],[112]]) AT=A.transpose() A1 = AT*A b1 = AT*b M = A1.augment(b1) f(x)=M.rref()[0][2] + M.rref()[1][2]*log(x) show(M.n(digits = 3), "RREF ", M.rref().n(digits = 3), " from which we obtain y = ", f(x)) show(f(x)) show(f(100, hold=True), " = ", f(100).numerical_approx(digits = 3)) P = point([(44,91), (61, 98), (81, 103), (113, 110), (131, 112)], size=40) P += plot(f,[0,140], gridlines='true', legend_label='Least Squares Line') show(P) P.save('plot1lab3_part6.png')
Image in a Jupyter notebook

Part 7

Data: (0,0),(1,8.8),(2,29.9),(3,62.0),(4,104.7),(5,159.1),(6,222.0),(7,294.5),(8,380.4),(9,471.1),(10,571.7),(11,686.8),(12,809.2)(0,0), (1, 8.8), (2, 29.9), (3, 62.0), (4, 104.7), (5, 159.1), (6, 222.0), (7, 294.5), (8, 380.4), (9, 471.1), (10, 571.7), (11, 686.8), (12, 809.2)

Curve: y=β0+β1t+β2t2+β3t3y = \beta_0 + \beta_1 t + \beta_2 t^2 + \beta_3 t^3

reset() var('t') A = matrix(QQ, 13, 4, lambda i, j: i^j) b = matrix([[0], [8.8], [29.9], [62.0], [104.7], [159.1], [222.0], [294.5], [380.4], [471.1], [571.7], [686.8], [809.2]]) AT=A.transpose() A1 = AT*A b1 = AT*b show(A1.augment(b1).n(digits = 3)) M = A1.augment(b1).n(digits = 3).rref() show(M) f(t) = M[0][4] + M[1][4]*t + M[2][4]*t^2 + M[3][4]*t^3 show(f(t)) P = point([(0,0), (1, 8.8), (2, 29.9), (3, 62.0), (4, 104.7), (5, 159.1), (6, 222.0), (7, 294.5), (8, 380.4), (9, 471.1), (10, 571.7), (11, 686.8), (12, 809.2)], size=20) P += plot(f,[0,15], gridlines='true', legend_label='Least Squares Line') show(P) velocity = f.diff() show("At time t = 4.5 seconds, the plane's velocity is ", velocity(4.5), " feet per second") show(A) P.save('plot1lab3_part7.png')
Image in a Jupyter notebook

Part 8

Write a 1-2 paragraph summary of what you learned in this lab. Make sure to include both mathematics and coding (Sage and Latex), and be specific.

The main idea we explored was how to associate a data set to a curve using a matrix. We then translated the matrix equation ATAβ^=ATbA^T A\hat{\beta} = A^T\textbf{b} into Python code:

A = matrix(RDF, [[x-values and constants]]) b = matrix([[y-values]]) A1 = A.transpose()*A # the left side of the equation b1 = A.transpose()*b # the right side of the equation A1.augment(b1).rref() # the entries of the last column are the solutions to the equation

We also learned about several new utility functions for this lab. First, we learned that defining a symbolic function is just like defining a normal python function. We explored plotted functions and data points with the plot() function. We also learned about Pythons anonymous lambda functions which we used to generate the large Matrix in part 7 with the command

matrix(QQ, 13, 4, lambda i, j: i^j)