%Tan Nguyen, Trang Pham, Nhan Phan
%ECE 460 Lab
%WINTER 2003
%Lab #1
%Example 1- Scalars, vectors and matrices
a=2.5 %Assigns a scalar value to the variable "a"
a=[1 2 3] % "[]" signify a vector or a matrix
a=[1, 2, 3] % both "," and "bland space" act as delimiters; extra spaces are ignored
a=[1 2 3;4 5 6;7 8 10] % a ";" signifies end of a row of the matrix
f=a(2,1) %The element in Row 2 and Column 1 of matrix "a"
g=a(2:3,2) %Will assign elements in row2 to 3 and column 2 of "a" to the variable "g"
h=a(2,2:3) %Will assign elements in row 2 and columns 2 to 3 of "a" to the variable "h"
b=[1 2;3 4]; % The semicolon after } suppresses printing on screen of the value of "b"
d=inv(b) %Matlab will compute the inverse of matrix "b" and assign it to "d" and print it.
%==========================================================================
%Example 2- Plotting graphs and repetive computations.
t=[0: 0.1:20]'; % "t" is a column vector with elements 0,.1,.2,...,20 - a total of 201 elements
for i= 1:1:201
x(i)=sin(t(i))*(t(i)^2);
end
y=sin(t).*(t.^2); % alternate approach. Note that x=y=t^2sin(t). Second method is more efficient
% The column vectors x and y are identical vectors of dimension 201. y is generated by using
%array operators, an efficient way of performing repetive computations.
plot(t,x)
%This command plots a x vs. t graph. With the graph window active, the plot can be printed
%(just use the print command in the File menu). Next plot y vs. t for comparison.
plot(t,y)
%=========================================================================
%Example 3-Complex Arithmetic
z=1+2i  %Assigns a complex value to z;
r=abs(z)  %absolute value or magnitude of z
theta=angle(z)   %angle of z in radians; multiply by (180/pi) for angle in degrees
fz=(z+1)/(z*(z^2-4*z+5)*(z-1));  %Evaluating complex expression; 
                                 %F(z)=(z+1)/z(z^2-4z+5)(z-1)
fz
%=======================================================================
%Example 4-Factoring Polynomials
%F(s)=s^3+6s^2+11s+6. We want to factor this polynomial.
%In Matlab polynomials are defined as row vectors of the polynomial coefficients.
pol1=[1 6 11 6];
rt1=roots(pol1);  % computes the roots and store the result in rl1 without
%printing on screen.
rt1
pol2=[1 4 4]; rt2=roots(pol2)  % We may have more than one command
%(statement) in a line.
%Note: no ";' at the end of rt2 statement. If there is no ';' 
%Matlab prints the value of the variable
pol3=[1 1 1]; rt3=roots(pol3)
f1=poly(rt1); %Given the roots, this will compute the polynomial coeffs.
f1
%======================================================================
%Example 5- Partial Fraction Expansion.
%Given a ratio of polynomials, G(s)=(3s+7)/(s^3+6s^2+11s+6), determine the 
%corresponding PFE.
%This is done by first defining the numerator and denominator polynomials
num1=[3 7];
den1=[1 6 11 6];
[r,p,k]=residue(num1,den1);
%r is the vector of PFE coefficients; p is the vector of roots of den1;
%and k=0 if the degree of num1 < the degree of den1.
r
p
k
%======================================================================
%Example 6 - Plotting functions revisited.
%Generate and plot the function; f(t)=1-2e^(-0.24t)cos(2t)
t=[0:0.1:20];
f=1-2*exp(-.24*t).*(cos(2*t));
% Array operator ".*" is needed to multiply two vectors element by element
plot(t,f)
xlabel('The t axis')
ylabel('The f axis')
title('The plot of f versus t')
grid on
diary off % the file lab1.txt is closed

%=========================================================================
%Report Item #3
% Rework example 6 Generate and plot the function : f(t)=1-2e^(-.24t) cos(2t)

 t=[0:0.1:20];
 f=1-2*exp(-.24*t).*(cos(2*t));  % .* multiply 2 vectors element by element
 plot(t,f)
 hold on                         % holds the graph for superimposed plots
 plot(t,(1+2*exp(-0.24*t)),'--')  % gives a dashed line plot
 plot(t,(1-2*exp(-0.24*t)),'--')  %  ""
 xlabel(' The t axis')
 ylabel(' The f axis')
 title('The plot of f versus t by second method')
 grid on;
 hold off;                       % removes the hold for future plots
%============================================================================
%  Report Item #4. Write function m-file prompts user for a positive
% integer input, and return the sum of integer from 1 to n.

%function m-file int_sum.m
function y=int_sum(x) %first line of a function m-file must be the 
                      %function statement
n=input('Type an integer and hit return: ') % prompting for an input
if ((n-abs(round(n)))~=0)   %check if input is not a positive integer
    error('Input is not a positive integer') %if so, give error message
else s=sum(1:n)  % evaluate and print the sum
end
%=============================================================================
%Report Item #5
%Determine the roots of the following polynomials using MATLAB roots function:
%a) P1(s)=s^3+6s^2+11s+6
pol1=[1 6 11 6];
rt1=roots(pol1);
rt1
f1=poly(rt1); %Reconstruct the polynomials using the poly function
f1
%b) P2(s)=s^4+s^3-3s^2-s+2
pol2=[1 1 -3 -1 2];
rt2=roots(pol2);
rt2
f2=poly(rt2); %Reconstruct the polynomials using the poly function
f2
%c)P3(s)=s^6+s^5-2s^4-3s^3-7s^2-4s-4
pol3=[1 1 -2 -3 -7 -4 -4 ];
rt3=roots(pol3);
rt3
f3=poly(rt3); %Reconstruct the polynomials using the poly function
f3
%d)P4(s)=2s^4+4s^3+8s^2+8s+4
pol4=[2 4 8 8 4];
rt4=roots(pol4);
rt4
f4=poly(rt4); %Reconstruct the polynomials using the poly function
f4
%======================================================================
%Report Item #6
%Find partial fraction expansion for the following ratios of 
%polynomials with pencil and paper, and verify the results using MATLAB.
%a) F1(s)=(s+3)/(s+1)(s+2)
num1=[1 3];
den1=[1 3 2];
[r,p,k]=residue(num1,den1);
%r is the vector of PFE coefficients; P is the vector of roots of den1;
% and k=0 if the degree of num1< the degree of den1;
r
p
k
%b) F2(s)=(5s+3)/(s^3+6s^2+11s+6)
num2=[5 3];
den2=[1 6 11 6];
[r,p,k]=residue(num2,den2);
%r is the vector of PFE coefficients; P is the vector of roots of den1;
% and k=0 if the degree of num1< the degree of den1;
r
p
k
%c) F3(s)=(2s+12)/(s^2+2s+5)
num3=[2 12];
den3=[1 2 5];
[r,p,k]=residue(num3,den3);
%r is the vector of PFE coefficients; P is the vector of roots of den1;
% and k=0 if the degree of num1< the degree of den1;
r
p
k
%d) F4(s)=(s^2+2s+3)/(s+1)^3
num4=[1 2 3];
den4=[1 3 3 1];
[r,p,k]=residue(num4,den4);
%r is the vector of PFE coefficients; P is the vector of roots of den1;
% and k=0 if the degree of num1< the degree of den1;
r
p
k
%===============================================================================
%Report Item #7
% Compute and plot the following functions on the same page using subplot command.
%Be sure to include labels, tile, and grid for each plot. Use t2=[0:0.1:99]'. 
%Use a script m-file to do this exercise.
%a) x(t2)=(12t2^2+2.3t2+5)e^(-.1t2) vs. t2
t2=[0:0.1:99]';
x=(12*(t2.^2)+2.3*t2+5).*exp(-0.1*t2);
subplot(2,2,1); plot(t2,x)
xlabel(' The t2 axis')
ylabel(' The x(t2) axis')
title('The plot of x(t2) versus t2 ')
grid on

%b)y(t2)=(12t2^2+2.3t2+5)e^(-0.1t2 vs. t2
y=1-exp(-0.04*t2).*(cos(0.2*t2)+(0.19*sin(0.2*t2)));
subplot(2,2,2); plot(t2,y)
xlabel('The t2 axis')
ylabel('The y(t2) axis')
title(' The plot of y(t2) versus t2')
grid on

%c)h(t2) vs. g(t2) where g(t2) =2t2cos(0.2t2) and h(t2)= t2sin(0.2t2).
h=t2.*(sin(0.2*t2));
g=2*t2.*(cos(0.2*t2));
grid on
subplot(2,2,3); plot(g,h)
xlabel('The g(t2) axis')
ylabel('The h(t2) axis')
title('The plot of h(t2) versus g(t2)')
grid on




