Question: Write a simple computer program to evaluate a function of 2 variables over a range of input values, using one- and two- dimensional arrays using
Write a simple computer program to evaluate a function of 2 variables over a range of input values, using one- and two- dimensional arrays using MATLAB, and adding some simple plotting.
Evaluate the following function for a range of X and Y values: (X,Y) = Xcos(X)(Y)sin(Y)
- For the X vector, ask the user to enter the min and max 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 min and max values and the number of values desired, and then use the linspace() function to generate the Y vector.
In order to generate the desired final product, 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 calculate X cos(X) and Y sin(Y) as one-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 wll 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'
These MATLAB commands will be useful:
- disp
- input
- colon, :
- linspace
- cos
- sin
- * versus. *
- transpose
- surfc
- title
- xlabel
- ylabel
- fprintf
- min
- max
- mean
- Also: ops, elfun, datafun, elmat, matfun, polyfun, and specfun
Note: Calculate Xcos(X)Ysin(Y) for a range of X and Y values to display the results in a table
- For the X input, the program should read in the min value of X, the max value of X, and the change in X between data points
-For the Y input, the program should read in the min value of Y, the max value of Y, and the number of data points in the Y direction.
- Once the program has read in the necessary input and allocated arrays, fill the x and y vectors using the colon operator and linspace() respectively
- Then calculate Xcos(X) and Ysin(Y) using elementwise multiplication and a final result using matrix multiplication.
- Once both vectors and the array are filled, printout the results in a table and plot
- Finally use MATLAB functions to find or calculate the following, and report their values:
- The max value of f(x,y) over the defined range
- The min value of f(x,y) over the defined range
- The average value of f(x,y) over the defined range
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
