CS 109 - C/C ++ Programming for Engineers w. MatLab- Spring 2016 Assignment-
Evaluating a Function of Two Variables Over Ranges of Inputs Using MATLAB
Overall Assignment-
For this assignment you are to write a simple computer program to evaluate a function of 2 variables over a range of input values, using one- and two-dimensional arrays. This is basically a repeat of HW3, using MATLAB instead of C++, and adding some simple plotting.
The Function-
For this assignment your program is to evaluate the following function for a range of X and Y values:
f(X, Y) = X cos(X)Y sin(Y)
Calculating the X and Y Vectors-
Generating the X and Y vectors will be much simpler using Matlab:
- For the X vector, ask the user to enter the minimum and maximum values and the change between each step, and then use the colon operator, :, to generate the X vector.
- For the Y vector, ask the user to enter the minimum and maximum values and the number of values desired, and then use the linspace( ) function to generate the Y vector.
Matrix Multiplication-
In order to generate the desired final product, you will need to do two types of multiplication:
-Element-by-element multiplication requires two matrices (vectors) of the same size, and is performed by the .* multiplication operator. That will let you calculate X cos(X) and Y sin(Y) as on-dimensional vectors.
Matrix multiplication multiplies out two matrices, and requires that their "inner" dimensions be the same. So if matrix A has rA rows and cA columns (an rA x cA matrix), and matrix B has rB rows and cB columns (rB x cB), then in order to multiply A * B, cA must equal rB, ( and the resulting product will have rA rows and cB columns, i.e. it will be a rA x cB matrix. )
- Transposing a matrix effectively flips it along its diagonal, converting rows into columns and columns into rows, so an M x N matrix becomes an N x M matrix when transposed. The transpose operator in Matlab is a single quote, e.g. A = A'
Plotting Results-
The simplest way to plot in Matlab is to select the desired variable(s) in the workspace window, and right-click. This will bring up a menu of plot types appropriate for the selected data from which you can select one. The command used to generate the plot will be automatically added to the command window, allowing it to be copied and edited for inclusion in a script. (This also tells you what command to look up documentation for to get more information.)
Program Details / PseudoCode-
For this assignment you are to write a simple computer program to calculate X cos(X) Y sin(Y) for a range of X and Y values, and to display the results in a table. Your program should also do some simple analysis on the results.
-Your program should first print out your name and ACCC netID (e.g. jbell), and explain to the user what the program does.
-For the X input, your program should read in the minimum value of X, the maximum value of X, and the change in X between data points.
-For the Y input, your program should read in the minimum value of Y, the maximum value of Y, and the number of data points desired in the Y direction.
-Once you have read in the necessary input and allocated arrays, fill the x and y vectors using the colon operator and linspace() respectively.
-Then calculate X cos( X ) and Y sin( Y ) using element wise multiplication and a final result using matrix multiplication, as discussed above.
-Once both vectors and the array are filled, print out the results in a form resembling that shown in the sample above. (Don't sweat getting the numbers to exactly line up under one another, but do leave some space between the numbers so they don't run together.)
-Finally use Matlab functions to find or calculate the following, and report their values:
- The maximum value of f(x, y) over the defined range.
- The minimum value of f(x, y) over the defined range.
- The average value of f(x, y) over the defined range.
Attachment:- Assignment.rar