% 6.003: Signals and Systems - Spring '97
% S97Less2.M	Lesson 2 of MATLAB MiniCourse Demonstration script 
% 		by Pat Kreidl

%%%%% Basic Operations: Matrix and Element-wise
!clear
type 2.txt
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
echo on

A1 = [1 7; -4 8];	%Generate "workspace" for lesson
A2 = [-1 0; 3 2];
H = [-2 5 -7; 3 1 2];
b2 = [9; 3];
b3 = [b2; 1];
c = [2 0];
alpha = 1;
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

B1 = A1 + A2		%Basic Matrix Operations -- addition
pause	  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
B2 = A2 - A1		%Basic Matrix Operations -- subtraction
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C1 = A1*H		%Basic Matrix Calculations -- multiplication
			%Pay attention to dimensions!!!!
keyboard		%To continue lesson, type:      return
C2 = H'*A1		%Inner dimensions agree now...phew!  8-)
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
v1 = A1*b2		%Example with vector
v2 = alpha*A1*b2		%Example with scalar and vector
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A2inv = inv(A2)		%Basic Matrix Operations -- inverse
x = inv(A1)*b2		%Solve linear equation Ax = b for variable x
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			%Basic Matrix Operations -- ?????division??????
x = A1\b2		%Equivalent to x = inv(A1)*b2
y = A1\A2		%Equivalent to x = inv(A1)*A2
y = A1/A2		%Equivalent to x = A1*inv(A2)
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Asquared = A1^2		%Basic Matric Operations -- exponent
Asquared = A1*A1	%Equivalently, 
Aroot = A1^0.5		%Matrix square root -- via exponent operation
Aroot = sqrtm(A1)	%Matrix square root -- via "sqrtm" function
			%Notice complex numbers! (See lesson 3)
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
				%Can combine operations in many ways
T = alpha*(inv(A1)*v1+c')'*H	%using standard "order-of-operations"
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear			%Clear "workspace" -- take all variables out of memory
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A1 = [1 7; -4 8];	%regenerate "workspace" and continue lesson
A2 = [-1 0; 3 2];
H = [-2 5 -7; 3 1 2];
b2 = [9; 3];
b3 = [b2; 1];
c = [2 0];
alpha = 1;
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

C1 = A1.*A2		%Basic Element-wise Operations -- multiplication
v1 = b2.*c'		%Example with vectors
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			%Basic Element-wise Operations -- division 
M1 = A1./A2 		%Equivalent to each element m = a1 divided by a2 
M1 = A1.\A2		%Equivalent to each element m = a2 divided by a1
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			%Basic Element-wise Operations -- exponent 
Melsqua = A1.^2		%Square each element 
Melroot = A1.^0.5	%Take square root of each element -- via exponent
Melroot = sqrt(A1)	%Take square root of each element -- via "sqrt"
Wow = 10.^A1		%Elements are 10 raised to the power a1 
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			%Note that Wow's elements can be displayed meaningfully
format short e		%Change format of MATLAB output
Wow = 10.^A1		%Elements are 10 raised to the power a1 
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
format short		%Change format back to MATLAB default setting
			%Many familiar element-wise functions
yrad = [-pi/2 0 pi/2]	%Note predefined variable   pi
ydeg = degrees(yrad)	%Readily convert from radians to degrees
yrad = radians(ydeg)	%And vice-versa...use radians for MATLAB trig functions
Sinofy = sin(yrad)	%Trigonometric functions and their inverses
ydeg0 = degrees(atan(tan(radians(ydeg))))	%Nest functions easily!
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			%More element-wise functions
y = [-1 0 1]		%RECALL: Could have used y = [-1:1] = linspace(-1,1,3)
Expofy = exp(y)		%Logarithmic/exponential functions
Logofy = log(Expofy)    %Natural logarithm (base e)
What = log10(10.^y)	%Logarithm base 10
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

echo off

clear



