Test 2 - Python Edition

Similar documents
Test 2 Solutions - Python Edition

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 103L Fall Test 2. Michael R. Gustafson II

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 103L Fall Test 2 Solutions. Michael R. Gustafson II

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 224 Spring Test II. Michael R. Gustafson II

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 224 Spring Test II. Michael R. Gustafson II

Test II Michael R. Gustafson II

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. ECE 110 Fall Test II. Michael R. Gustafson II

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. ECE 110 Fall Test I. Michael R. Gustafson II

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 53L Fall Test I. Rebecca A. Simmons & Michael R. Gustafson II

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 53L Fall Test III. Rebecca A. Simmons & Michael R. Gustafson II

Exercise_set7_programming_exercises

Optimization with Scipy (2)

Complex Numbers. Visualize complex functions to estimate their zeros and poles.

Lynch, October 2016 Page 1 of 5. Math 150, Fall 2016 Exam 2 Form A Multiple Choice Sections 3A-5A

MthSc 107 Test 1 Spring 2013 Version A Student s Printed Name: CUID:

Complex Numbers. A complex number z = x + iy can be written in polar coordinates as re i where

MA 262, Fall 2017, Final Version 01(Green)

MATH 1040 Test 2 Spring 2016 Version A QP 16, 17, 20, 25, Calc 1.5, 1.6, , App D. Student s Printed Name:

Math 19 Practice Exam 2B, Winter 2011

Lynch 2017 Page 1 of 5. Math 150, Fall 2017 Exam 2 Form A Multiple Choice

IMPORTANT Read these directions carefully:

Lynch 2017 Page 1 of 5. Math 150, Fall 2017 Exam 1 Form A Multiple Choice

Student s Printed Name:

Lab 6: Linear Algebra

Multiple Choice Answers. MA 110 Precalculus Spring 2016 Exam 1 9 February Question

MATH 099 Name (please print) FINAL EXAM - FORM A Winter 2015 Instructor Score

MthSc 107 Test 1 Spring 2013 Version A Student s Printed Name: CUID:

Math 2114 Common Final Exam May 13, 2015 Form A

Math 1: Calculus with Algebra Midterm 2 Thursday, October 29. Circle your section number: 1 Freund 2 DeFord

MA 110 Algebra and Trigonometry for Calculus Spring 2017 Exam 1 Tuesday, 7 February Multiple Choice Answers EXAMPLE A B C D E.

DM534 - Introduction to Computer Science

Practice Problems. 1. The age and weights of six cats are given in the following table:

Math 41 Second Exam November 4, 2010

MAS212 Scientific Computing and Simulation

MAT 145 Test #4: 100 points

Math 51 Second Exam May 18, 2017


Conjugate-Gradient. Learn about the Conjugate-Gradient Algorithm and its Uses. Descent Algorithms and the Conjugate-Gradient Method. Qx = b.

Conditioning and Stability

Test 3 Version A. On my honor, I have neither given nor received inappropriate or unauthorized information at any time before or during this test.

Ordinary Differential Equations II: Runge-Kutta and Advanced Methods

ECE 220 Laboratory 4 Volt Meter, Comparators, and Timer

Math 160 Calculus for Physical Scientists I Exam 1 - Version 1 February 9, 2017, 5:00-6:50 pm

IMPORTANT. Read these directions carefully: You do not need to show work for the Multiple Choice questions.

Math 41 Final Exam December 9, 2013

MA 113 Calculus I Fall 2017 Exam 1 Tuesday, 19 September Multiple Choice Answers. Question

MA Exam 1 Fall 2015 VERSION 01

By providing my signature below I acknowledge that this is my work, and I did not get any help from anyone else:

Core Mathematics 2 Algebra

Gradient Descent Methods

Math 41 First Exam October 12, 2010

Math 160 Calculus for Physical Scientists I Exam 1 February 11, 2016, 5:00-6:50 pm

Math 111 Exam 1. Instructions

Multiple Choice Answers. MA 113 Calculus I Spring 2018 Exam 2 Tuesday, 6 March Question

Math 41 First Exam October 15, 2013

Student s Printed Name: _Key_& Grading Guidelines CUID:

Math 148. Polynomial Graphs

Physics 102 Exam 2 Spring Last Name: First Name Network-ID

MthSc 103 Test 3 Spring 2009 Version A UC , 3.1, 3.2. Student s Printed Name:

Chapter 1: January 26 January 30

Lorenz Equations. Lab 1. The Lorenz System

Math 1050 Exam 2 Name. D) no vertical asymptotes

MAT Calculus for Engineers I EXAM #3

STUDENT NAME: STUDENT SIGNATURE: STUDENT ID NUMBER: SECTION NUMBER RECITATION INSTRUCTOR:

Student s Printed Name: _Key

Page Points Score Total: 100

Inverse Problems. Lab 19

1 Introduction & Objective

Temperature measurement

CMSC Discrete Mathematics FINAL EXAM Tuesday, December 5, 2017, 10:30-12:30

Calculus is Cool. Math 160 Calculus for Physical Scientists I Exam 1 September 18, 2014, 5:00-6:50 pm. NAME: Instructor: Time your class meets:

Without fully opening the exam, check that you have pages 1 through 11.

MA 262, Spring 2018, Midterm 1 Version 01 (Green)

Spring 2018 Exam 2 MARK BOX HAND IN PART NAME: PIN: INSTRUCTIONS

Math 41: Calculus First Exam October 13, 2009

A Add, subtract, multiply, and simplify polynomials and rational expressions.

MA 113 Calculus I Fall 2016 Exam Final Wednesday, December 14, True/False 1 T F 2 T F 3 T F 4 T F 5 T F. Name: Section:

MATH 112 Final Exam, Spring Honor Statement

Student s Printed Name:

MA162 EXAM III SPRING 2017 APRIL 11, 2017 TEST NUMBER 01 INSTRUCTIONS:

Homework 2 Computational Chemistry (CBE 60553)

Foundations for Functions

STUDENT NAME: STUDENT SIGNATURE: STUDENT ID NUMBER: SECTION NUMBER RECITATION INSTRUCTOR:

MATH 152 FINAL EXAMINATION Spring Semester 2014

1.) Suppose the graph of f(x) looks like this (each tick mark denotes 1 unit). x y

Lesson 5 Practice Problems

Math 41 Final Exam December 6, 2010

MATH 1070 Test 1 Spring 2014 Version A Calc Student s Printed Name: Key & Grading Guidelines CUID:

Total 100

GES 554 PDE MEMO. Memo: GES554-Project-2. REF: Ext:

University of Georgia Department of Mathematics. Math 2250 Final Exam Spring 2017

Without fully opening the exam, check that you have pages 1 through 11.

EE3210 Lab 3: Periodic Signal Representation by Fourier Series

2. To receive credit on any problem, you must show work that explains how you obtained your answer or you must explain how you obtained your answer.

2D Plotting with Matlab

Version B QP1-14,18-24, Calc ,App B-D

STUDENT NAME: STUDENT SIGNATURE: STUDENT ID NUMBER: SECTION NUMBER AND RECITATION INSTRUCTOR:

University of Maryland Department of Physics. Spring 2009 Final Exam 20. May (175 points) Post grades on web? (Initial, please) Yes No

Designing Information Devices and Systems II Spring 2016 Anant Sahai and Michel Maharbiz Midterm 2

Without fully opening the exam, check that you have pages 1 through 11.

Transcription:

'XNH8QLYHUVLW\ (GPXQG7UDWW-U6FKRRORI(QJLQHHULQJ EGR 10L Spring 2018 Test 2 - Python Edition Shaundra B. Daily & Michael R. Gustafson II Name (please print): NetID (please print): In keeping with the Community Standard, I have neither provided nor received any assistance on this test. I understand if it is later determined that I gave or received assistance, I will be brought before the Undergraduate Conduct Board and, if found responsible for academic dishonesty or academic contempt, fail the class. I also understand that I am not allowed to speak to anyone except the instructor about any aspect of this test until the instructor announces it is allowed. I understand if it is later determined that I did speak to another person about the test before the instructor said it was allowed, I will be brought before the Undergraduate Conduct Board and, if found responsible for academic dishonesty or academic contempt, fail the class. Signature: Notes You will be turning in each problem in a separate pile. Most of these problems will require working on additional pieces of paper - Make sure that you do not put work for more than any one problem on any one piece of paper. For this test, you will be turning in four different sets of work. Again, Please do not work on multiple problems on the same sheet of paper. Also - please do not put work for one problem on the back of another problem. Be sure your name and NetID show up on every page of the test. If you are including work on extra sheets of paper, put your name and NetID on each and be sure to staple them to the appropriate problem. Problems without names will incur at least a 25% penalty for the problem. Work must be down in dark ink and on only one side of the page. This first page should have your name, NetID, and signature on it. It should be stapled on top of and turned in with your submission for Problem I. Every other pile should have your test page on top followed by any previously blank paper used for that problem. You will not need and can not use a calculator on this test. For hand calculations you will instead show the set-up of the calculation you need to perform but will not reduce it. You can leave powers, roots, products, sums, differences, and quotients unevaluated - for example, ()(2) + (8)(11) (6)(24) A = (8) 2 (4) 2 + (2) 2 (7) 2 should be left just like that. You will be asked to write several lines of code on this test. Make sure what you write is code and not mathematics. Be very careful with any symbols you use. You do not need to put the honor code statement in your codes. The honor code statement on this page and your NetID on each problem stands in for that.

Name (please print): Community Standard (print NetID): Problem I: [20 pts.] The Basics (1) Assume that someone has already run the following code: 1 T = np.array([[1, 2],[, 4]]) 2 U = np.array([[-5, 6],[7, -8]]) which has created the following matrices: [ ] 1 2 T = 4 U = [ ] 5 6 7 8 For each of the following sections, determine the resulting matrix or matrices created by Python. Put your answers near the block of code from which they came and be sure to indicate which matrix is which; your work can go on a separate sheet that you will staple to this page. Your answers can look like Python output or like mathematical expressions. (a) A = T**2 (f) F = (-10<U) & (U<0) (b) B = T@T (g) G = U>T (c) C = T*U (h) H = np.where(u<0) (d) D = T@U (i) I = T[np.where(U<0)] (e) E = np.mean(t) (j) J = np.block([[t], [U]]) (2) Determine the values of the matrices K, L, M, and N when the following loop is finished 1 K=0; L=[0]; M=0; N=np.ones(6) 2 for K in np.arange(1, 6, 2): L += [K]; 4 M = M+K; 5 N[K] = K;

(1) 1 A: 2 array([[ 1, 4], [ 9, 16]], dtype=int2) 4 5 B: 6 array([[ 7, 10], 7 [15, 22]]) 8 9 C: 10 array([[ -5, 12], 11 [ 21, -2]]) 12 1 D: 14 array([[ 9, -10], 15 [ 1, -14]]) 16 17 E: 18 2.5 19 20 F: 21 array([[ True, False], 22 [False, True]]) 2 24 G: 25 array([[false, True], 26 [ True, False]]) 27 28 H: (Not part of Fall 2018 test) 29 (array([0, 1], dtype=int64), array([0, 1], dtype=int64)) 0 1 I: 2 array([1, 4]) 4 J: 5 array([[ 1, 2], 6 [, 4], 7 [-5, 6], 8 [ 7, -8]]) (2) 1 K: 2 5 4 L: 5 [0, 1,, 5] 6 7 M: 8 9 9 10 N: 11 array([1., 1., 1.,., 1., 5.]) 12

Name (please print): Community Standard (print NetID): Problem II: [0 pts.] Roots and Extrema (1) This part of the problem refers to: f(x) = e x + e x x + 5cos(2x) The function and the sign of the function are presented on the next page which should be turned in with this problem. Assuming the following code is already in place: 1 f = lambda x: np.exp(x)+np.exp(-x)-x+5*np.cos(2*x) (a) Determine the location of the two roots of f(x). At the end of your code, the location of the left root should be stored in RootLeft and the location of the right root should be stored in RootRight. (b) Determine the value and location of the overall minimum value of f(x). At the end of your code, the value should be stored in MinVal1 and the location should be stored in MinLoc1. (c) Determine the value and location of the local minimum value of f(x) when x < 0. At the end of your code, the value should be stored in MinVal2 and the location should be stored in MinLoc2. (d) Determine the value and location of the local maximum value of f(x) when 1 < x < 1. At the end of your code, the value should be stored in MaxVal and the location should be stored in MaxLoc. (e) Determine the two values of x where f(x) = 10. At the end of your code, these values should be stored in f10x1 and f10x2. (f) Write the code that produces the top figure on the next page. Note that 1000 points were used. (2) This part of the problem refers to: g(x,y) = cos(x) sin(y) x 2 + y 2 + 1 The function and the contour plot of the function are presented on the next page which should be turned in with this problem. Assuming the following code is already in place: 1 g = lambda x,y: np.cos(x)*np.sin(y)/(x**2+y**2+1) (a) Determine the value and location of the overall minimum value of g(x,y). At the end of your code, the value should be stored in gmin and the x and y coordinates should be stored in xgmin and ygmin, respectively. (b) Determine the value and location of the overall maximum value of g(x,y). At the end of your code, the value should be stored in gmax and the x and y coordinates should be stored in xgmax and xgmax, respectively. (c) Read this carefully: determine the value and y coordinate of the minimum value of g(x,y) when x is -. At the end of your code, the value should be stored in ghmm and the y coordinate should be stored in yhmm. (d) Write the code that produced the bottom left plot of g(x,y). Note that 0 points were used in each direction and that the copper color map was used.

20 f(x) 10 0 2 1 0 1 2 x 1.0 sign(f(x)) 0.5 0.0 0.5 1.0 2 1 0 1 2 x 0.4 0. 4 0.2-0.000-0.000 0.1 0.0 0.1 0.2 g(x,y) 2 1-0.050 0.00 0.100 0.150 0.200 0.250 0.50 0.050-0.050 0. 0.4 y 0 1 0.050-0.050-0.100-0.150-0.250-0.00-0.50-0.200 0.050 4 2 1 0 x 1 2 4 0 1 2 4 4 2 1 y 2-0.000-0.000 4 4 2 1 0 1 2 4 x -0.000

1 import numpy as np 2 import scipy.optimize as opt import matplotlib.pyplot as plt 4 from matplotlib import cm 5 6 def f(x): return np.exp(x) + np.exp(-x) - x + 5 * np.cos(2 * x) 7 8 # a - any valid bracket accepted for each root 9 RootLeft = opt.brentq(f, 0.0, 1.5) 10 RootRight = opt.brentq(f, 1.5, 2.0) 11 # b - any valid boundaries accepted 12 MinLoc1 = opt.fminbound(f, 1, 2) 1 MinVal1 = f(minloc1) 14 # c - any valid boundaries accepted 15 MinLoc2 = opt.fminbound(f, -2, -1) 16 MinVal2 = f(minloc2) 17 # d - any valid boundaries accepted 18 MaxLoc = opt.fminbound(lambda x: -f(x), -0.5, 0.5) 19 MaxVal = f(maxloc) 20 # e - note re-write for root-finding 21 f10x1 = opt.brentq(lambda x: f(x) - 10, -2.5, -2.0) 22 f10x2 = opt.brentq(lambda x: f(x) - 10, 2.0,.0) 2 # f - 24 fig1, (ax1, ax2) = plt.subplots(2, 1, num=1, clear=true) 25 x = np.linspace(-,, 1000) 26 ax1.plot(x, f(x), ³k-³) 27 ax1.grid(1) 28 ax1.set(xlabel= ³x ³, ylabel= ³f(x) ³) 29 ax2.plot(x, np.sign(f(x)), ³k-³) 0 ax2.grid(1) 1 ax2.set(xlabel= ³x ³, ylabel= ³sign(f(x)) ³) 2 fig1.tight_layout() fig1.savefig( ³fplot.eps ³) 4 5 def g(x, y): return np.cos(x) * np.sin(y) / (x**2 + y**2 + 1) 6 7 # a - any close initial guess accepted 8 rmin = opt.fmin(lambda r: g(r[0], r[1]), [0, -1]) 9 xmin = rmin[0] 40 ymin = rmin[1] 41 gmin = g(xmin, ymin) # or gmin = g(*rmin) 42 # b - any close initial guess accepted 4 rmax = opt.fmin(lambda r: -g(r[0], r[1]), [0, 1]) 44 xmax = rmax[0] 45 ymax = rmax[1] 46 gmax = g(xmax, ymax) # or gmax = g(*rmax) 47 # c - any valid boundaries accepted 48 yhmm = opt.fminbound(lambda y: g(-, y), 0, ) 49 ghmm = g(, yhmm) 50 # d 51 x, y = np.meshgrid(np.linspace(-4, 4, 0), 52 np.linspace(-4, 4, 0)) 5 fig2 = plt.figure(2) 54 fig2.clf() 55 newax = fig2.add_subplot(111, projection= ³d ³) 56 newax.plot_surface(x, y, g(x, y), cmap=cm.copper) 57 newax.set(xlabel= ³x ³, ylabel= ³y ³, zlabel= ³g(x,y) ³) 58 fig2.tight_layout() 59 fig2.savefig( ³gplot1.eps ³)

60 # %% Not part of test - just showing how to make contour plots 61 fig = plt.figure() 62 fig.clf() 6 lvls = np.round(np.arange(-0.4, 0.4,.05), decimals=2) 64 cplot = plt.contour(x, y, g(x, y), lvls, cmap=cm.copper) 65 plt.clabel(cplot, lvls, inline=true, fontsize=8) 66 plt.xlabel( ³x ³) 67 plt.ylabel( ³y ³) 68 fig.tight_layout() 69 fig.savefig( ³gplot2.eps ³)

Name (please print): Community Standard (print NetID): Problem III: [0 pts.] Linear Algebra (1) Given the following expression: [ ] [ ] 1 2 x a y = [ ] 5 6 where a is some known, but variable, value and x and y represent your unknowns: (a) Find an expression for the determinant of the coefficient matrix - that is, 1 2 a (b) Find an expression for the inverse of the coefficient matrix - that is, ([ ]) 1 1 2 a (c) Are there values of a for which there is no solution to this problem? If not, state why you believe that. If so, state what value or values of a mean there is no solution. (d) For a particular value of a, the condition number of the system is given as 10 5. If the measurements for the system were taken with 8 significant figures, what does this condition number mean about the accuracy of the solution? (e) Clearly use linear algebra to solve for x and y as functions of a. (2) Given the electric circuit below: R 1 R v s + v a R 2 v b R x where R 1 through R are resistors with set values, v s is a known voltage source with a set value, R x is a variable resistor (called a potentiometer), and v 1 and v 2 are unknown voltages, a student taking a class requiring the ability to solve for circuits has come up with the following equations: v a v s R 1 + v a + v a v b = 0 R 2 R v b v a + v b = 0 R R x (a) Assuming that the values of v 1 and v 2 are unknown, re-write the equations above in linear algebra form; that is, fill in the following: v a v b = (b) Now assume that R 1 =1000 Ω, R 2 =2000 Ω, R =000 Ω, and v s =10 V. The potentiometer R x can have values ranging from 0 Ω to 5000 Ω. Write code that will clearly use linear algebra techniques to solve for the voltages v a and v b as functions of R x for 500 values of R x linearly spaced across the possible range of values, then write code to make a graph containing curves for both v a and v b as a function of R x. Be sure to include appropriate axis labels, a title, a grid, and a legend. v a should be plotted as a solid red line and v b should be plotted as a dashed blue line.

(1) (a) (1)(a) ()(2) = a 6 2 4 a 2 5 1 (b) a 6 (c) If a = 6 the determinant is 0 and therefore there either is no solution or there are infinite solutions, In this case, there is no solution. (d) log 10 (10 5 )=5, so the solution has 5 fewer digit of precision than the system. The solution is only accurate to 8-5= significant figures. (e) [ ] a 2 1 a 6 [ ] 5 6 = [ ] (a)(5) + ( 2)(6) ( )(5) + (1)(6) a 6 = [ 5a 12 ] a 6 9 a 6 (2) (a) [ 1 + 1 + 1 1 R1 R2 R R 1 1 R R + 1 R x ] [va ] v b = [ vs ] R 2 0 (b) 1 import numpy as np 2 import matplotlib.pyplot as plt 4 R1 = 1000 5 R2 = 2000 6 R = 000 7 vs = 10 8 N = 500 9 Rx = np.linspace(0, 5000, N) 10 va = np.zeros(n) 11 vb = np.zeros(n) 12 for k in range(n): 1 A = np.array([[1 / R1 + 1 / R2 + 1 / R, -1 / R], 14 [-1 / R, 1 / R + 1 / Rx[k]]]) 15 b = np.array([[vs / R1], [0]]) 16 Voltages = np.linalg.solve(a, b) 17 va[k] = Voltages[0] 18 vb[k] = Voltages[1] 19 20 fig, ax = plt.subplots(1, 1, num=1, clear=true) 21 ax.plot(rx, va, ³r-³, label= ³ v_a ³) 22 ax.plot(rx, vb, ³b-- ³, label= ³ v_b ³) 2 ax.set(xlabel= ³ R_x, \Omega ³, 24 ylabel= ³Voltage, V ³, 25 title= ³Voltages in a Circuit ³) 26 ax.legend() 27 ax.grid(1) 28 fig.tight_layout() # Not required

Name (please print): Community Standard (print NetID): Problem IV: [20 pts.] Stats and Fits You are given a data file called MyInfo.dat containing eleven pairs of independent (left) and dependent (right) values. For example, the file might look like: 1-5 4.879 2-4 6.7059-7.5885 4-2 8.175 5-1 7.5565 6 0 7.0117 7 1 6.2292 8 2 5.60 9 5.1179 10 4 4.5048 11 5 4.9485 Write a program called FitRand that performs the following tasks: (1) Loads the data. (2) Calculates and displays the sum of the squares of the data residuals. () Efficiently calculates the sums of the squares of the estimate residuals and the coefficients of determination for polynomial fits of order 1 through 6 and displays them in an aligned table (see below) where decimal values are in exponential format with four significant digits. (4) Determines the lowest order of those six fits to have a coefficient of determination at or above 0.95 and displays that order. If none of the fits has a coefficient of determination of at least 0.95, state, Fit order is higher than 6. Here are two example runs: 1 St is 1.209e+01 2 Order Sr r2 1 8.454e+00.006e-01 4 2 4.777e+00 6.048e-01 5 7.05e-02 9.942e-01 6 4 6.710e-02 9.944e-01 7 5 6.494e-02 9.946e-01 8 6 5.4e-02 9.956e-01 9 Fit order is likely: 1 St is.409e+01 2 Order Sr r2 1 2.521e+01 2.607e-01 4 2 1.41e+01 5.80e-01 5 1.086e+01 6.815e-01 6 4 1.069e+01 6.865e-01 7 5 1.062e+01 6.886e-01 8 6 9.975e+00 7.074e-01 9 Fit order is higher than 6

1 import numpy as np 2 # %% 4 data = np.loadtxt( ³MyInfo.dat ³) 5 X = data[:, 0].copy() 6 Y = data[:, 1].copy() 7 8 # %% 9 St = np.sum((y - np.mean(y))**2) 10 print( ³St is {:0.e} ³.format(St)) 11 print( ³Order Sr r2 ³) 12 r2 = np.zeros(6) 1 14 # %% 15 GE = 0 16 for Order in range(1, 7): 17 P = np.polyfit(x, Y, Order) 18 YHAT = np.polyval(p, X) 19 Sr = np.sum((y - YHAT)**2) 20 r2[order - 1] = (St - Sr) / St 21 print( ³{:5d} {:0.e} {:0.e} ³.format(Order, Sr, r2[order - 1])) 22 if GE == 0 and r2[order - 1] > 0.95: 2 GE = Order 24 25 # %% 26 if GE!= 0: 27 print( ³Fit order is likely: {:d} ³.format(GE)) 28 else: 29 print( ³Fit order is higher than 6 ³)