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

%%%%% Working with Complex Numbers and Polynomials
!clear
type 3.txt
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
echo on

%%%%%%%%%%%%%%%% Complex numbers
		%MATLAB interprets the letter i to represent sqrt(-1)
sqrt(-1)	%PROVIDED THE USER HASN"T DEFINED IT AS SOMETHING ELSE
s1 = 3 + 4i	%Defining complex numbers
s2 = 5 - 12j	%Can use j also -- MATLAB caters to electrical engineers 8-)
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		%MATLAB does "everything" with complex numbers as with real!!!
A = [s1 j; j s2]	%Generate matrix of complex entries
ca = s1 + s2		%Addition
cm = s1*s2		%Multiplication
A2 = A.^2		%Element-wise Exponent -- each a2 = a*a
Ainv = inv(A)		%Matrix inverse 
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		%MATLAB also provides special functions for complex numbers
a = real([s1 s2])		%extract real part -- element-wise
b = imag([s1 s2])		%extract imaginary part -- element-wise
r = abs([s1 s2])		%calculate magnitude -- element-wise
alpha = angle([s1; s2])		%calculate phase angle in rad -- element-wise
beta = degrees(angle([s1; s2]))	%nest functions if want in degrees!!
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

!clear
%%%%%%%%%%%%%%% Polynomials
		%MATLAB represents polynomials by a vector of its coefficients
		%in order of descending powers
p1 = [1 4 4]		%Represents the polynomial          s^2 + 4s + 4
p2 = [2 0 0 -1]		%Represents the polynomial   2s^3 +0s^2 + 0s - 1
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		%MATLAB also provides special functions for polynomials
r1 = roots(p1)		%two roots of second-order polynomial -- via "roots"
r2 = roots(p2)		%three roots of third-order polynomial -- via "roots"
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p3 = poly(r2)		%Coefficients of polynomial with given set of roots
a = polyval(p3,5)	%Evaluate polynomial  p3  at s = 5
c = polyval(p1,r1(1,1))	%Evaluating at root gives zero...whew!
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			%Coefficients of product of two polynomials
p4 = conv([1 1],[1 1])	%(s+1)*(s+1) = s^2 + 2s + 1
poly2str(p4,'s')	%display polynomial as equation in variable s
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
poly2str(conv([1 1],poly([j; -j])),'z')	%Can combine operations in many ways

pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		%We use polynomials primarily to express system functions
num = [1 0];
den = [1 1 5];
printsys(num,den,'s')		%Show system function in "pretty" form
pause     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


echo off
clear



