” ENG5322M程序 写作、Python编程语言 辅导Course assignmentENG5322M: Introduction to computer programming(Additional Hints Provided)October 26, 2020InstructionsYou should submit the following through the ENG5322M Moodle submission system by 4pm,27th November 2020:1. Python notebook (.ipynb format) used for to solve each of the assignment problems.This can be a Separate notebook for each problem or a single file.You will be marked according to the following criteria: Program correctness (60%): Does your program solve the problem given to you? Program readability (20%): Have you written your program in such a way that it iseasy to follow? Have you included appropriate comments? Testing (20%): Have you done appropriate testing to show your program works asintended?You are given Two problems to solve for your project assignment which will be outlined in moredetail shortly. The two problems are focussed on:1. Numerical integration2. Multivariate least square methodNotebook formatI expect to see you explain your code throughout your notebook both using text and images,much like I have down in the lectures during this course. As a quick guide, the default type of acell is code, but if you change it to markdown (look in the toolbar) and then hit shift and enteryou will see it become text in the notebook.11 PROBLEM 1: NUMERICAL INTEGRATION1. Testing: You should write appropriate code that performs tests on your program.1 Problem 1: Numerical integrationOverview of task: Write a program that calculates the integral of a function using both thetrapezoidal rule and the rectangular rule. That is, we want to calculateI =Z baf(x) dx (1)which is the area under the curve as shown graphically in Fig. 1.Figure 1: Graphical representation of Eq. (1)Specifics: You are required to write a program that first defines variables of the the limits ofthe integral (i.e. the values a and b in Eq. (1) ) and the number of integral divisions (explainedshortly). Please note, it is not Acceptable to simply use a library function to compute theseintegrals – I want to see you implement the algorithms for both the rectangular and trapezoidalrule.Your program should then calculate the integral for f(x) = x2, f(x) = sin x and f(x) = exsin xusing both rules. That is, calculateTo calculate these values, we can approximate them using numerical integration. A key pointto understand is That we almost always approximate the integral using these methods – there isusually an error.Finally, I want you to output a file for each integral and each rule which gives the result for N=2(i.e. 2 integral divisions) up to N=100.21.1 Rectangular rule 1 PROBLEM 1: NUMERICAL INTEGRATIONFigure 2: Rectangular rule for number of divisions N = 21.1 Rectangular ruleThis works by dividing the area under the curve into rectangles as shown in Fig. 2Lets take an example of how to apply this to calculate the integral R 10x3dx for N = 2. We firstcalculate the step size h = (b a)/N = (1 0)/2 = 0.5. We now apply the rectangular rule togetapproximate integral = f(x1)h + f(x2)h= f(0) 0.5 + f(0.5) 0.5= 0 0.5 + 0.125 0.5= 0.0625 (2)We can also do the same for N = 4 (See Fig. 3). h = (1 0)/4 = 0.25.approximate integral = f(x1)h + f(x2)h + f(x3)h + f(x4)h= f(0) 0.25 + f(0.25) 0.25 + f(0.5) 0.25 + f(0.75) 0.25= 0 0.25 + 0.015625 0.25 + 0.125 0.25 + 0.421875 0.25= 0.140625 (3)And for N = 8 we get 0.19140625, N = 50 we get 0.2401, N = 100 we get 0.245025 andN = 1000 we get 0.24950025.If you were to work this integral out by hand, you would see that the exact answer is 0.25. Noticehow we converge to this Answer as we increase N.The general formula for the rectangular rule is as follows:approximate integral = h [f(x1) + f(x2) + + f(xN )] (4)where h = (b a)/N.31.2 Trapezoidal rule 1 PROBLEM 1: NUMERICAL INTEGRATIONFigure 3: Rectangular rule for number of divisions N = 41.2 Trapezoidal ruleThe trapezoidal rule approximates the integral by constructing trapezoids under the function.This is shown in Fig. 4 for N = 2 and Fig. 4 for N = 4. It is clear that this provides a moreaccurate approximation the integral.The general rule for trapezoidal rule is as follows. Letting h = (b a)/N, the integral can beapproximated as:approximate integral = h2[f(x1) + 2f(x2) + 2f(x3) + 2f(xN ) + f(xN+1)] (5)Lets apply this to the previous example of f(x) = x3. For N = 2 with h = (1 0)/2 = 0.5 wehaveapproximate integral = h2[f(x1) + 2f(x2) + f(x3)]=0.52[f(0) + 2f(0.5) + f(1.0)]= 0.25[0 + 0.25 + 1]= 0.3125 (6)and for N = 4 With h = (1 0)/4 = 0.25 we haveapproximate integral = h2[f(x1) + 2f(x2) + 2f(x3) + 2f(x4) + f(x5)]=0.252[f(0) + 2f(0.25) + 2f(0.5) + 2f(0.75) + f(1.0)]= 0.125 [0 + 2 0.015625 + 2 0.125 + 2 0.421875 + 1]= 0.265625 (7)and for N = 8 we have 0.25390625, N = 50 we have 0.2501, N = 100 we have 0.250025 andN = 100 we have 0.25000025.1.3 TestingThe obvious way to test your program is to compare your results against integrals you haveperformed by hand. For example, in the results shown above we know the exact result to be 0.2542 PROBLEM 2: MULTIVARIATE LEAST SQUARES METHODFigure 4: Trapezoidal rule for number of divisions N = 2Figure 5: Trapezoidal rule for number of divisions N = 4and we can see that they are converging to the exact result as N is increased.Another test might be to integrate a function that is a constant. E.g. what is the area under thefunction f(x) = 1?2 Problem 2: Multivariate least squares methodWe Want to find the regression coefficients a0, a1,…,am for the following equation:yi = a0 + a1x1i + a2x2i + a3x3i + … + amxmi (8)To solve this, let us suppose, we take a case for m = 2 and add an error term i to our equation:yi + i = a0 + a1x1i + a2x2i (9)52 PROBLEM 2: MULTIVARIATE LEAST SQUARES METHODWe can then obtain an objective function S to minimize as:(12)Minimizing S with respect to a0, a1, and a2, we get:Sa0= 2X(a0 + a1x1i + a2x2i yi) (13)Sa1= 2X(a0 + a1x1i + a2x2i yi)x1i (14)Sa2= 2X(a0 + a1x1i + a2x2i yi)x2i (15)Equating these to zero, we obtain three equations in three unknowns, a0, a1, a2:a0 + a1 x1 +a2 x2 = y (16)a0 x1 +a1 x1x1 +a2 x1x2 = x1y (17)a0 x2 +a1 x1x2 +a2 x2x2 = x2y (18)where . represents average quantity defined as:We can represent in Matrix form to use matrix methods as:or XA = Y . The matrix of unknown coefficients, A, is obtained by finding the inverse of X,so that A = X1Y .62.1 Hint 2 PROBLEM 2: MULTIVARIATE LEAST SQUARES METHODExtending to m variables, we get:(21)Overview of task: The assignment is to use the above logic to find the coefficients a0,a1,a2for the data given in Table 1 (also provided as a tab-delimited text file). The exact solution isy = 2.145 3.117×1 + 1.486×2.2.1 HintTo program this efficiently in Python, we can make use of a trick. Imagine we get the values of xand y from file, and since we are reading one line at a time, rather than calculating the averages,we can simply maintain the sums by taking 1/n out and cancelling it on both sides: (22)where i is an individual record (row). For example, when i = 1, x1i = 0.408, x2i = 0.58, andyi = 2.39. Similarly, when i = 2, x1i = 0.429, x2i = 0.51, and yi = 2.53, and so on.The summations are as follows:Pni=1 x1i = 0.408 + 0.429 + 0.492 + 0.529 + … + 1.655 + 1.684 + 1.897Pni=1 x1ix2i = (0.408 0.58) + (0.429 0.51) + (0.492 0.53) + … + (1.655 1.2) + (1.684 0.96) + (1.897 1.24)If you can import all the function from numpy library from numpy import *, and if you have avariable m as the number of independent variables, and if you can define x=zeros((m+1,m+1)),y=zeros((m+1,1)), and xi=[0.0]*(m+1), then you can populate both matrices x and y in anested-loop with two equations, x[j,k] += xi[j]*xi[k] and y[j,0] += yi*xi[j], provided ifyou have xi[0]=1, xi[1] the value of first predictor (first column), xi[2] the value of secondpredictor (second column), and so on. yi is dependent variable (last column) in Table 1. Finally,you can simply convert numpy arrays x and y to numpy matrix so that you can get the inverseautomatically: X=mat(x.copy()), Y=mat(y.copy()), and A=X.I*Y.Also, the data file Provided is tab-delimited. Look up how to split a string using .split()function.72.1 Hint 2 PROBLEM 2: MULTIVARIATE LEAST SQUARES METHODTable 1: Data to be fittedx1 x2 y0.408 0.58 -2.390.429 0.51 -2.530.492 0.53 -3.380.529 0.6 -2.720.569 0.58 -2.950.677 0.64 -3.320.703 0.61 -3.450.841 0.66 -3.810.911 0.73 -3.90.936 0.72 -4.110.96 0.57 -4.240.978 0.84 -4.810.993 0.69 -4.11.038 0.75 -4.281.051 0.76 -4.051.119 0.85 -4.231.134 0.75 -4.581.16 0.85 -4.41.209 0.72 -5.051.272 0.82 -4.791.303 0.86 -4.761.353 0.98 -4.651.367 0.9 -5.181.388 0.71 -5.51.425 1.04 -5.011.453 0.9 -5.151.484 0.77 -5.81.503 0.93 -5.331.536 0.81 -5.231.563 0.83 -6.111.655 1.2 -5.341.684 0.96 -6.131.897 1.24 -6.4582.1 Hint 2 PROBLEM 2: MULTIVARIATE LEAST SQUARES METHODFigure 6: Pictorial representation of the overall process如有需要,请加QQ:99515681 或邮箱:99515681@qq.com
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。