It is assumed  that you already have your user name and passwd and know how to login to math department computers(point, round or line) and start MATLAB. If you need help on login please click here Login.

This introductory lab is a MATLAB Tutorial. Please follow directions and type appropriate commands to learn how to work with MATLAB.

If you have not visited the page A few NOTES on MATLAB please visit it by clicking on it.

Please Login  to one of math dept computers (tangent or point), then open a terminal window and follow the instruction below.



Lab #1



Type
textmatlab
You will see the Matlab prompt : >>
Type :  diary LAB1.text  WHAT is diary? 


Type:
% your last name

Type:
% your first name

Type:
% date

Type:
%Your MATH 22AL User name 

Ways to enter a vector  in  matlab

a)  Direct Method:      b) Using command


Type:
u = [4   1.5    3.9]   to create a 3-element row vector 3-element .
Type: v = [1   2    3.1    5]' to creates a 4-element column vector.
In entering a large vector  if you have to continue onto the next line first type .... then press enter and  continue onto the next line try this with w in the next input

Type: w = [2   2.6    5   1    ...
Press Enter then Type
11.6   9   -8] to creates a 7-element row vector
You may use brackets to combine two vectors to obtain a larger one
Type: t = [u   v'] 
to create a 7-element vector.

Why did we use v' instead of v?


Type:   s= [ u' v ]    You will  see an error message, because the size of the vectors does not  match.


Ways to enter a matrix in MATLAB

Type:  A = [1 2 3 ; 3 4 5 ; 4 5 6 ]          click here to check to create a 3 by 3 matrix.

( Note : You need a space between the row entries and a semicolon between the rows.)

Type:  B = eye(4) to create a 4 by 4 identity matrix.
Type:  C = rand(4) to create a 4 by 4 matrix with randomly generated entries distributed uniformly between 0 and 1. 
Type:  D= rand(4,3)  to get a 4 by 3 random matrix .
Type:  who  to see a list of your variables.
Type:  clear D  to clear the variable D
Type:  who  to see if D is cleared or not 
Type:  D=rand(5) to create a 5 by 5 random matrix. 
Type:  D=10*D  to multiply every entry of D by 10. 
Type:  D=round(D)  to round the entries of D to integers. Now you have a random matrix with integer entries.

You can do all three operation with a single command :

Type:  D=round (10*rand(5))  to get a random matrix with integer entries. 
Type:  E= magic(3)  to get a 3 by 3 magic matrix. Why it is called magic? 
Type:  E= magic(4)  to get a 4 by 4 magic matrix. Why it is called magic? 

Working with entries, rows, and columns of a matrix:

You can access or redefine any sub-matrix of a given matrix.

Type:  A(2,2:3)=[3,4]  to change the entries (2,2) and (2,3) to 3 and 4 respectively. 
Type:  A to see the matrix A. 
Type:  A23=A(2,3)  to see the entry (2,3) of A . 
Type:  row2=A(2,:)  to see the second row of A. In A(2,:) the symbol colon ":" in second component means select entries  from all columns. 
Type:  col3=A(:, 3)  to see the third column of A. In A(:,3) the symbol ":" means select entries from all rows of A. 
Type:  subD1=D(2:3, 1:3) to see a sub-matrix of D consisting of the entries on the second-third row and on the first through third columns.
Type:  subD2=D([1 3], 1:3) to see a sub-matrix of D consisting of the entries on the first and third row and on the first through third columns.
Type: subD3=D(1:2:5, 1:3)  Note that 1:2:5 selects the the first, increments by 2 and then selects 3, increments by 2, selects 5.
Type: subD4=D([1 3 5], 1:3)  This should give you the same submatrix obtained by the previous command subD3=D(1:2:5, 1:3).

Now type the following and explain what happened( By typing on a new line to percentage sign % followed by your comments you may avoid receiving an error message).
 You may check your answer by clicking the appropriate box.


Type:  A(2,3) = -2 check your answer. 
Type:  A(2,:) = [0 1 0] check your answer. 
Type:  A(3,2:3) = [3 4] check your answer. 


Sometimes you want Matlab to do computation but not to show the answer on the screen. In this case to avoid a display, put a semicolon " ; " at the end of the line. See the following example:
 

Type:  B=[ 6 4; 3 0]; To create another matrix called B, but not display it 
Type:  B To see the matrix B 

Working with diagonal matrices:

Type:  diag(A) to see a vector of diagonal entries of A 
Type:  diag(ans)  to see a matrix of zeroes with the same diagonal entries as A
Type:  diag(diag(A))  to see a matrix of zeroes with the same diagonal entries as A.

Creating zero matrices and matrices of ones:
 
Type:  ones(3)  to create a matrix of entries 1. 
Type  zeros(4) 
Type zeros(3, 4)
Type:  ones(3,4)  to create a 3 by 4 matrix of entries 1. 
 


Use  commands, diag, ones, eye,  to construct a 4 by 4 matrix of ones , with 2, 3, 4, and 5 on the main diagonal.

 note that you can Add matrices of the same size.  


Type:   diag([ 1 2 3 4]) To create  a matrix with diagonal 1, 2, 3, 4
Type:   ones( ?) + diag([ 1 2 3 4])  You need to decide what number to  put for "?".

Constructing large matrices using small matrices.

Type:  row1=[ 2 2 2 ]  to create a 1 by 3 matrix ( row vector). 
Type:  col1=row1'  to create the transpose of r , a 3 by 1 matrix ( column vector). 
Type:  A=[1 2 3; 3 4 5; 4 5 6]  to see the matrix A
Type:  A'  to see the transpose of the matrix A
Type:  M=[A ; row1]  to add a new row to A to create a 4 by 3 matrix.
Type:  N=[A col1]  to add a new column to A to create a 3 by 4 matrix.
Type:  B1=[A N]  to create a 3 by 7 matrix. Note the space between two matrices.
Type:  B2=[A ; M]  to create a 7 by 3 matrix. 
Type:  B3=[A ;N]   Why are you getting an error message?
Type:  B4=[A M]  Why are you getting an error message?

 

b.) Creating vectors with equispaced elements

Example:

a=[ 1 3 5 7]  is a vector with equispaced elements, in which neighboring elements are separated from each other by a fixed amount (the step size).

To create such a vector we  use colon operation.
 For example,

Type    a = 1:2:7          to create the vector a= [ 1 3 5 7] ,  above.  Step size is two=2.
Type   b = 3:7	 to produces the vector with integer elements : 3, 4, 5, 6 and 7.  Step size is one.

Type c = 13:2:25         To obtain odd integers between, 13 and 25
Note: The step-size (the separation between the values of neighboring elements) must be put between two colon operation.

Example:  In the following example 15 is the first element the others obtained by adding 4 each time the last element must be smaller than or equal to  the third number.
 Type 	c = 15:4:35
Type c = 15:4:36
Type c = 15:4:37
Type c = 15:4:38
Type c = 15:4:39
Type c = 15:4:40

The syntax for the colon operator is    x = start value : step : stop value

  Integer values  and decimal numbers bothe can be used.  
 

Type x = 0.2:0.25:1
 to created [0.2 0.45 0.7 0.95].

If the  start and stop values and number of the elements are known we can use the function linspace.

Type : x = linspace(1.2,  2, 5)
will create a 5-element vector  with start  value of  1.2 and stop value of 2  x =[ 1.2000    1.4000    1.6000    1.8000    2.0000]
 

This is the general form of the command if you type this, you  will get an error, Why?
v= linspace(start,stop,nv)
 

 You may use linspace to create a table like 

  Type:  x = 0:0.5:6
Type: y = sin(x)

to plot the function.

 

A general set of commands that will accomplish  plotting is (

   xi = linspace(min(x),max(x),100);    % 100 x-values
yi = spline(x,y,xi); % interpolated y-values
plot(x,y,'o',xi,yi)
You can use more or less than 100 values but the point is that the combination of linspace and max/min minimizes the need for excessive thinking about creating the vectors and so allows you to think about what the results (the plot in this case) looks like and mean.

logspace plays the role of linspace but on a logarithmic scale. Logarithmic spacing of elements in a vector is useful when dealing with exponential functions (log-log and semilog plots, in particular. The only difference between logspace and linspace to be aware of is in the definition of the starting and ending values.

logspace uses the logarithm of the starting and ending values!

Being aware of this feature can eliminate a number of errors and frustrations in using this function.



Sample commands for elementary row operations

Let N be the 3 by 4 matrix that you built in the last step. Suppose first you want to interchange the rows 2 and 3.
 

Type:  TEMP= N(2,:)  to store the second row of N as TEMP. 
Type:  N(2,:) =N(3,:)  to replace the second row of N by the third row of N.
Type:  N(3,:) =TEMP  to replace the third row of N by TEMP which was the second row of N . 

Now suppose that you want to multiply the second row of the new matrix N by 5 .
 

Type:  N(2,:)=5*N(2,:)  to multiply the second row of N by 5 

Now suppose that you want to add -7 times the second row to the third row.
 

Type:  N(3,:)=-7*N(2,:)+N(3,:)  to add a multiple of the second row to the third row. 
Here are two useful commands:

You will learn about the following two  MATLAB commands later in your Linear algebra class, type the following commands and see how they work.
 

Type:  rref (C)  to see C after row reduction. 

MATLAB commands for matrix operation

A few helpful MATLAB commands

Number Display Formats


To Finish your LAB


Type:
save
to stop copying in the file " LAB1.text and saving and closing it
Type: diary off to   exit MATLAB
Type: exit to   exit MATLAB




Use a local editor (pico) to edit your diary file, then in command line of  POINT submit it by typing the following command:

     submitm22al LAB1.text