MAT 275 Lab 2
MAT 275 Laboratory 2
Matrix Computations and Programming in MATLAB
In this laboratory session we will learn how to
1.Create and manipulate matrices and vectors.
2.Write simple programs in MATLAB
NOTE: For your lab write-up, follow the instructions of LAB1.
EXERCISE 1
Enter the following matrices and vectors in MATLAB
(a)Perform the following operations: AB, BA, cB and Ad (use standard linear algebra multiplica- tion).
(b) Construct a 3 × 6 matrix C = [A B ] and a 4 × 3 matrix D = [ | B | ]. |
c |
(c)Use the “backslash” command to solve the system Ax = b.
(d)Replace A(2; 3) with 0.
(e)Extract the 3rd row of the matrix A.
(f)A row or a column of a matrix can be deleted by assigning the empty vector [] to the row or the column. For instance A(2,:)=[] deletes the second row of the matrix A.
Delete the third row of the matrix B.
EXERCISE 2
Recall that a geometric sum is a sum of the form a + ar + ar2 + ar3 + : : :.
(a)Write a function file that accepts the values of r, a and n as arguments and uses a for loop to return the sum of the first n terms of the geometric series. Test your function for a = 3, r = 1=2 and n = 10.
(b)Write a function file that accepts the values of r, a and n as arguments and uses the built in command sum to find the sum of the first n terms of the geometric series. Test your function for a = 3, r = 1=2 and n = 10.
Hint: Start by defining the vector e=0:n-1 and then evaluate the vector R = r.^e. It should be easy to figure out how to find the sum from there.
EXERCISE 3
The counter in a for or while loop can be given explicit increment: for i =m:k:n to advance the counter i by k each time. In this problem we will evaluate the product of the first 10 odd numbers 1 · 3 · 5 · : : : · 19 in two ways:
(a)Write a script file that evaluates the product of the first 10 odd numbers using a for loop.
(b)Evaluate the product of the first 10 odd numbers using a single MATLAB command. Use the MATLAB command prod.
EXERCISE 4
Write a script file that creates a row vector v containing all the powers of 2 below 1000. The output vector should have the form: v = [ 2; 4; 8; 16 : : : ]. Use a while loop.
EXERCISE 5
Write a function file that creates the following piecewise function:
Assume x is a scalar. The function file should
contain an if statement to distinguish between the
different cases. The function should also display “the function is undefined at x = 10” if the input is x = 10. Test your function by evaluating f(1), f(4), f(7) and f(10).
11 years ago
Purchase the answer to view it

- lab_2_m_files.zip
- lab_2.docx