Part 1:
These problems all involve typing expressions in the MATLAB command window. To complete this homework assignment, turn in a text (or Word, or PDF) file containing the expression that you type to respond to each problem.
1. Find an efficient way to generate the following matrix:
mat =
7 8 9
12 10 8 10 6
Give expressions that, for the matrix mat,
a. Refer to the scalar value 6 using subscript notation.
b. Refer to the entire second row.
c. Refer to the first two columns
2. Create a vector x that consists of 20 equally spaced values from -π to π. Create a 20-element y vector in which every element is the sine of the corresponding element in x.
3. Create a 4 x 2 matrix of all zeros and store it in a variable. Then, replace the second row in the matrix with a vector consisting of a 3 and a 6.
4. Using the colon operator, create the following row vectors:
a. 3 4 5 6
b. 1.0000 1.5000 2.0000 2.5000 3.0000
c. 5 4 3 2
5. Use the help function to learn about the function rand. Create a 3 x 5 matrix of random real numbers. Delete the third row.
6. Create two 3 x 3 matrices of random real numbers. Create a third matrix in which each element is the product of the corresponding elements in these two matrices. Create a fourth matrix that is the product of matrix multiplication of the first two matrices.
7. Create four 4-element vectors of random real numbers in the range of 1 to 100. Concatenate these vectors together to form a 4 x 4 matrix. Multiply this matrix by itself.
Part 2:
To complete this homework assignment, turn in a text (or Word, or PDF) file containing the MATLAB code that you generate in response to each problem.
1. Write a script that asks for a vector as user input and then prints out the elements of the vector in sentence format. For instance if the vector is [5.5 11.3 4.35], the script will display the following:
Element 1 is 5.5.
Element 2 is 11.3.
Element 3 is 3.45.
The script should work regardless of how many elements are in the vector.
2. The inverse of the mathematical constant e can be approximated as:
1/e ≈ (1 - 1/n)n
as n approaches infinity. Write a script that will loop through increasing values of n until the difference between this approximation and the actual value is less than 0.0001. The script should then display the actual value of e-1 and the approximation to four decimal places, and also display the value of n required to achieve such accuracy.
3. In thermodynamics, the Carnot efficiency is the maximum possible efficiency of a heat engine operating between two reservoirs at different temperatures. The Carnot efficiency is given as:
n = 1 - TC/TH
where TC and TH are the absolute temperatures of the cold and hot reservoirs, respectively. Write a script that will prompt the user for the two reservoir temperatures in Kelvin and print the corresponding Carnot efficiency to three decimal places. The script should errorcheck the user's input since absolute temperature cannot be less than or equal to zero. The script should also swap the temperature values if TH is less than TC.
Part 3:
To complete this homework assignment, turn in a m-file containing the MATLAB code that you generate in response to each problem.
1. Write a script that plots exp(x) for values of x ranging from -2 to 2 in steps of 0.1. The plot should have an appropriate title and labeled axes.
2. If a certain amount of money (called the principal P) is invested in a bank account, earning an interest rate i compounded annually, the total amount of money Tn that will be in the account after n years is given by:
Tn = P(1 + i)n
Write a function that will receive input arguments for P, i, and n, and will return the total amount of money Tn. Write a script that calls this function to plot Tn after 20 years as a function of interest rate, with interest rate ranging from 0 to 10% in increments of 0.1%.
3. Write a script that creates a file called testtan.mat comprised of two lines with three real numbers on each line (some negative, some positive, in the range of -1 to 3). Write a second script that loads the file into a matrix and computes the tangent of every element in the matrix
4. Write a script that will continue prompting the user for positive numbers, and storing them in a vector variable, until the user types in a negative number. Upon the user typing in a negative number, the stored vector should be saved to a file and the program should terminate.