Computer Problem-Solving in EGR 141

Total Page:16

File Type:pdf, Size:1020Kb

Computer Problem-Solving in EGR 141

COMPUTER PROBLEM-SOLVING IN EGR 141 ENGINEERING AND COMPUTER SCIENCE FALL 2006

LABORATORY EXPERIMENT #9 USING MATLAB TO SOLVE PROBLEMS

BEFORE YOU COME TO LAB

Review your notes from class and the Matlab history files from the lectures.

INTRODUCTION

This term we have learned how to use Visual Basic .NET to solve problems in engineering and computer science. Along the way we have discussed and practiced algorithm design, event- driven programming, conditional statements, loops, variables, arrays, functions, sub routines, files, and databases. Recently, we’ve applied our knowledge to another programming language, Matlab, which contains many toolboxes and functions for solving problems that require more mathematical approaches. This lab requires you to use Matlab to solve a familiar curve fitting problem using data that we collected during the Predicting and Estimating Unknowns problem- solving topic. You will use Matlab’s built-in functions for determining the best least squares curve fit and you will use Matlab’s general matrix operations to determine the same coefficients.

PART A

Table 1: Experimental Data X 5 10 15 25 30 35 45 50 Y 17 25 31 38 37 39 44 46

Table 1 shows the experimental data obtained in problem-solving assignment #3. Recall that we found the coefficients for a least-squares fit to this data for both 1st- and 2nd-order polynomials – by hand and using Visual Basic.

Recall that we derived the formulas for a1 and a0, the two coefficients for a 1st-degree polynomial fit by minimizing the squared error:

n邋 xi y i- x i y i a1 = 2 2 n 邋 x i - ( x i ) and

_ _ _ _ where represents the average of x and represents the average of y a0= y - a 1 x x y The Matlab commands to perform each of these sums is given below in Listing 1.

Listing 1: Matlab commands for a1 and a0

X = [5 10 15 25 30 35 45 50]; Y = [17 25 31 38 37 39 44 46]; Xsum = sum(X,2) Ysum = sum(Y,2) n = size(X,2) Xsquares = X .* X SumOfXsquares = sum(Xsquares,2) XtimesY = X .* Y SumOfXtimesY = sum(XtimesY,2) a1 = (n*SumOfXtimesY - Xsum * Ysum)/(n*SumOfXsquares - Xsum^2) a0 = (Ysum/n - a1 * Xsum/n)

Remember, .* (dot star) is an element-by-element multiplication and the star without the dot (*) is a standard matrix multiplication. Also recall that the sum function will give a vector of column totals, but sum(matrix,2) will give you a row vector of totals which means in this case a single number since there is only one row of data in X and Y.

1. Run the Matlab commands shown in Listing 1 2. Use PolyFit to determine the 1st-order coefficients

If you did this correctly, a1 and a0 obtained from direct computation and using the PolyFit functions should be the same. Print out the Command History and write the values obtained for both coefficients on the printout.

PART B

Recall that using partial differentiation we derived the equations for a2, a1, and a0 for a 2nd-order polynomial:

2 轾 n邋 xi x i轾 a0 轾 y i 犏 2 3 犏 x x x犏 a= x y 犏邋i i 邋 i犏1 犏 i i 犏 x2 x 3 x 4犏 a犏 x 2 y 臌邋i i 邋 i臌2 臌 i i that can be solved by inverting the matrix as shown:

2 -1 轾a0 轾 n邋 xi x i轾 y i 犏 2 3 犏 犏a= x x x x y 犏1 犏邋i i 邋 i犏 i i 犏a犏 x2 x 3 x 4犏 x 2 y 臌2 臌邋i i 邋 i臌 i i In problem-solving assignment #3 we used Microsoft Excel to invert the matrix and solve for the coefficients. We also automated this using Visual Basic .NET. As if that wasn’t enough, we’re now going to use Matlab to determine these coefficients 2 different ways! This will help us to practice using Matlab in general and the built-in curve fitting functions by solving a familiar problem.

Use Matlab to: 1. Enter these data into X and Y matrices (vectors) 2. Compute the XSum and YSum using Matlab’s sum function. Remember to use help sum if you need help using the function. 3. Compute the XSquares using element-by-element multiplication 4. Compute the XCubes using element-by-element multiplication 5. Compute the XFourths using element-by-element multiplication 6. Compute XtimesY using element-by-element multiplication 7. Compute XSquaresTimesY using element-by-element multiplication 8. Compute the SumofXtimesY 9. Compute the SumofXSquaresTimesY 10. Compute the SumofXSquares 11. Compute the SumofXCubes 12. Compute the SumofXFourths 13. Set n to the correct value for this problem 14. Build the Matrices A and B using these computed values according to:

2 轾 n邋 xi x i轾 a0 轾 y i 犏 2 3 犏 x x x犏 a= x y 犏邋i i 邋 i犏1 犏 i i 犏 x2 x 3 x 4犏 a犏 x 2 y 臌邋i i 邋 i臌2 臌 i i

A Coeff B

(hint: A = [ val1,1 val1,2 val1,3 ; val2,1 val2,2 val2,3 ; val3,1 val3,2 val3,3 ]; remember that the semi-colon is used to start a new row)

15. Invert A 16. Multiply matrices A-1B to solve for the coefficients

17. Use PolyFit to check your answers for this 2nd-order polynomial fit.

Copy the Command History related to this Part and Paste it into a Word Document. Skip a few lines and paste in the last 5 lines of your Command Window (where the answers are displayed). Print this Word Document and turn it in with the printout from Part A. PART C

Use the polyfit, polyval, and plot functions to plot X, Y, 1st-order fit, 2nd-order fit, and 3rd-order fits on a single graph. Use different colors for each. Remember to use help plot to view the documentation for the plot function and to review the notes from class.

Take a screenshot of the graph and turn it in with Parts A and B. Also, copy and Paste the commands related to this Part from your Command History window into a Word Document. Print that document and turn it in with Parts A and B.

When you are finished with all three Parts, have your lab mentor scroll back through your Command History and Command Window to check your work in action. Be sure that you have your lab instructor sign and date your lab to receive credit. LAB 9

Turn in the following items:  The Command History from Part A  The Command History and Command Window snippets from Part B as described at the end of Part B  The screen shot of the graph and Command History snippet described at the end of Part C.  This page with the appropriate signatures.

LABORATORY SIGNATURES

PROGRAMMER NAME:

______

LAB INSTRUCTOR SIGNATURE: DATE:

______

THIS SECTION TO BE FILLED IN BY THE GRADER(S). BE SURE TO INCLUDE COMMENTS IF FULL CREDIT IS NOT AWARDED FOR ANY OF THE FOLLOWING PARTS:

 A SCREEN SHOT OF THE PLOT FROM PART C IS INCLUDED (10 POINTS) _____

 THE COMMAND HISTORY AND COMMAND WINDOW WAS CHECKED BY THE LAB MENTOR (40 POINTS) _____

 A COMPLETE LISTING OF THE CODE REQUIRED FOR PARTS A, B, AND C IS INCLUDED (10 POINTS)

_____

 LAB INSTRUCTORS GRADE ASSIGNED BASED ON ORAL EXAMINATION OF THE STUDENTS UNDERSTANDING OF THEIR SOLUTION AND THE OVERALL QUALITY OF THE SOLUTION (40 POINTS)

_____ GRADE: ______OUT OF 100

Recommended publications