Lab 2 Manipulating Matrices & Programming in MATLAB

Preliminaries

Start MATLAB (from Start Menu). Arrange your windows so you can see MATLAB and this webpage.

Matrix Manipulations

Parts
We can pull parts of matrices by specifying a row, list of rows and all row, and a column, list of columns or all the columns. Try these:
```M = magic(5);
M(2,2)
M(1,[2 3])
M(:,3)
M(1:2,4:5)
```
The colon can be used to specify a range (1:5) of rows or columns or all (:) the rows or columns.
Arithmetic
MATLAB performs matrix addition, subtraction, multiplication and multiplication by a scalar using the usual format: +, -, * and *. If the dimensions don't allow the operation, then there will be an error message. Try these:
```A = rand(3); B = rand(3,4); C = rand(3); D = rand(4);
A*B
B*A
A*C
C*A
A+C
A+B
3*A
C-A
```
You can also do powers of matrices using ^, e.g. D^2 = D*D, D^3 = D*D*D. Try D^10
Functions
MATLAB also allows functions that act on matrices. Some act on the matrix as a matrix, others as a collection of individual elements. For example sum(A) sums each column of A, max(A) finds the maximum of each column of A, while abs(A) takes the absolute value of each element of A, and cos(A) computes the cosine of each element of A.

Scripts and Functions

Programming in MATLAB is pretty straight-forward as all you do is put the commands you want to use together in a file. There are two types of programs that MATLAB uses: Scripts and Functions. Both are called "M-Files" because the file name is whatever.m

To create a M-File, go under the File menu and choose New M-File. This will bring up a new window which you can type in. You might want to move/resize windows so you can see the M-file editor, the Command Window and your web brouser all at once.

First Script
A script is just a list of MATLAB commands and when it runs, it acts just like you had typed the commands. You can control the way it acts either by changing values before you run it, or have it ask for values, like in this example:

```% SCRIPT 1  myname.m  by Dr. Collins for GSS:Math

name = input('Enter your name: ','s');
N = input('How many times do you want to see it? ');
for i = 1:N
disp(name)
end
```

Either type this in, or copy and paste it into the M-file editor window and save as "myname.m". To run it, go into the Command window and type myname

Second Script
Create a new script that does the following:
• Read in a number from the user (call it N)
• Create a N x N matrix (call it A), with entries A(i,j) = i/j
• Display A and the sum of the columns of A
Save the result as "mymatrix.m" and run it. If you get an error message, try to figure out what's wrong and fix it. If you have trouble, try to think about what commands you would type to get the result.
If you get really lost, I've put a version of this at the end of this file.
First Function
The main two differences between a script and a function are that
• all the calculations and variables in a function are only available in the function; while in the script they are available in the main part of MATLAB. This is called the scope of the variables.
• because of the scope, functions start with a line telling which variables will be passed in and which will be passed out of the function
A function starts with a line like
function output = nameoffunction(input)
You can have several inputs and several outputs, but we'll start with one of each. Here's a function that takes as input a value for the size of a matrix and produces a matrix of that size so that A(i,j) = i + j:

```function M = myfun(N)
% function to compute N x N matrix with entries M(i,j) = i + j

for i = 1:N
for j = 1:N
M(i,j) = i + j;
end
end
```
Type or copy & paste this into the M-file editor and save it as "myfun.m". The file and the name of the function should be the same.

To test it, type myfun(4) in the command window.

Challenges
Here are some additional program ideas you might try. You can do all the changes in the "myfun.m" M-file.
• Create a more complicated formula for the elements of A. For example, make A(i,j) = 0 if i<2 or j>4.
• Have the function return A and the sum of the columns of A.
• Pass the function two values N and M. Make the matrix be N x M.
• Re-write "mymatrix.m" so that it calls "myfun.m" to create the matrix.
SCRIPT 2 mymatrix.m
```% SCRIPT 2  mymatrix.m  by Dr. Collins for GSS:Math

N = input('Enter the size of the matrix: ');
for i = 1:N
for j = 1:N
A(i,j) = i/j;
end
end
disp(A)
disp(sum(A))
```

Mail: ccollins@math.utk.edu