% M21-126 Intro to Math Software
% Spring 2000
% Dr. David Handron

% lesson9.m (Introduction to Matlab)

		% This next command causes all the commands below to be shown
echo on		% echo off will only print out the answers.

% To use this file, start up matlab and issue the command
% >> lesson9

% To quit Matlab, issue the command >> quit


% To continue after a pause statment, hit any key to continue, or
% hit (control) C to terminiate the execution of a ".m" file like this.

pause
% CONTRUCTING MATRICIES

% Matlab is designed to work with matricies. There are many ways to
% create a matrix. Here are a few examples:

X = [1,2,3]		% Note the square brackets and comma's
Y = [1 2 3]		% The comma's may be ommitted; but be carefull ....
pause

Z1 = [1 4-2 3]		% No unnecessary spaces ....
Z2 = [1 4 -2 3]		% .... note the gap before the -2

pause

A = [1 2 3; 4 5 6; 7 8 9]	% Semicolon's delimit rows
W = [1; 2; 3]			% A column vector
pause

size(A)		% The size() command gives the size of a matrix
size(X)		% Row vectors ...
size(W)		% ... are not column vectors
pause

zeros(4)	% zeors() makes a square matrix of zeros
zeros(2,3)	% or any other shape
pause

ones(4)		% ones() makes a square matrix of ones
ones(3,2)	% or any other shape
pause

eye(4)		% eye() makes identity matricies
eye(4,2)	% of any shape
pause

diag([1,2,3])		% A diagonal array
diag([1,2,3],1)		% Shift the diagonal by 1
diag([1,2,3],-1)	% Shift the diagonal the other way
pause

A		% Recall A
A(2,3)		% Extracts the entry in the 2nd row, 3rd column
A(2,3) = 17	% Modifies A(2,3)
pause

A(1,4) = 12	% This causes Matlab to expand A to accomodate this element
pause

% CLEARING VARIABLES

who		% who lists all variables used
whos		% whos lists more information
pause

clear Y W	% Clears Y and W
who

clear Z*	% Clear any variable begining with Z
who
pause

% Notice: We've involoked funtions using two different sytaxes:
% >>clear x	(without any brackets) clears the NAME x
% >>sin(x)	(with brackets) computes the sine of the VALUE stored in x
% If x = 5.0, then it is wrong to type clear(x) since this asks Matlab
% to clear 5.0 which it can't do (try it!)

x = 5
sin(x)
clear x
pause

% ARITHMETIC

(3+6*4.5)/(4-1)		% Matlab will do normal arithmetic ...
(3+6*4.5)\(4-1)		% Plus an extra division operation: a/b = b\a
pause

X = [3/7, cos(2.5*pi), sqrt(3)/7]	% Matlab has the usual functions
Y = [1/(3+5*i)^2, cos(2.5 + 3.5*i)]	% Complex arithetic is also supported
pause

pi		% Matlab's name for pi
sin(pi/6)	% All angles are in radians pi/6 is 30 degrees
pause

A		% Recall A
sin(A)		% Most functions will "map" onto arrays
pause

A * pi		% Arrays can also be multiplied by constants ...
A - 7		% ... and constants can be added or subtracted from arrays.
pause

% HELP & SAVING/RETRIEVING WORKSHEETS

help save	% Help is invoked with the help command
pause		
help load	% These help files show you how to save and retrieve your work
pause

% ROW AND COLUM ADDRESSING WITH COLONS

X		% Recall X
X(1:2)		% First two elements of X
X(2:3)		% Last  two elements of X
pause

A		% Recall A
A(1:2, :)	% Rows one and two of A and ALL columns
A(1:2, 2:4)	% Rows one and two of A and columns 2 thru 4
A(1, 2:3)	% Row one, columns two and three
pause

% Colons can also be used to construct arrays

K = 2:4		% integers two thru 4
L = 3:-1:1	% integers 3 down to one
M = 5:-2:1	% integers 5 down to one by increments of -2
pause

% Integer arrays can be used to index other arrays
A		% Recall A
A(L,K)		% Rows [3,2,1] of A and columns [2,4]
pause

A		 % Recall A
A([3,1],[4,1,2]) % Rows 3 and 1 of A and column 4, 1 and 2 in this order
pause







