% comment: the % symbol is used to start a comment
% matlab is a text-based software package for engineering, math, science to
% easily manipulate matrices, vectors, and variable.
% It is also great for plotting.
% It is of particular use for signals, systems, and control.
% Matlab can be used interactively, command by command or using a file (.m)
% with a series of commands to be run.

% simple variable (Note: matlab is case sensitive.
% Meaning a and A are two different variables
%
a = 1+2
A= 5
b=a*A
%variable can be used recursively
b=b^2

% if we do not want to see the command and result use a semicolon.
pause; % this adds a pause
b=b^2;

%To determine the value of a previously defined quantity,
% type the quantity by itself:
b

% MATLAB utilizes the following arithmetic operators:
% + addition
% - subtraction
% * multiplication
% / division
% ^ power operator
% ' transpose
%Note: others can be found using functions.
pause; % this adds a pause
% If your expression does not fit on one line, use an ellipsis (three or more periods at the end of the line) and continue on the next line.

c = 1+2+3+...
5+6+7

%There are several predefined variables which can be used at any time, in the same manner as user-defined variables:
i % sqrt(-1)
j % sqrt(-1)
pi % 3.14159...

%%For example,

y= 2*(1+4*j)
pause
%%Matlab prefers i to j when it print the result.yields: y = 2.0000 + 8.0000i

%There are also a number of predefined functions that can be used when defining a variable. Some common functions that are used in this text are:

% abs magnitude of a number (absolute value for real numbers)
% angle angle of a complex number, in radians
% cos cosine function, assumes argument is in radians
% sin sine function, assumes argument is in radians
% exp exponential function


c = abs(y)
% the magnitude (SRSS)

c = angle(y)
%the angle expressed is in radians

%With a=3 as defined ,

a=3
c = cos(a)

c = exp(a)

% yields: c = 20.0855

%Note that exp can be used on complex numbers. For example, with y = 2+8i as defined above,

c = exp(y)

% yields: c = -1.0751 + 7.3104i

% which can be verified by using Euler's formula:

c = exp(2)*cos(8) + j*exp(2)*sin(8)
pause
%Definition of Matrices
%MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1 matrices.
%Therefore, vector and matrix operations are as simple as common calculator operations.

%Vectors can be defined in two ways. The first method is used for arbitrary elements:

v = [1 3 5 7] % this is a seen as a row vector. a 1x4 vector with elements 1, 3, 5 and 7.

%Note that commas could have been used in place of spaces to separate the elements.
% Additional elements can be added to the vector:

v(5) = 8

%yields the vector v = [1 3 5 7 8].
%Previously defined vectors can be used to define a new vector.
% For example, with v defined above

a = [9 10]
b = [v a]

%creates the vector b = [1 3 5 7 8 9 10].

%The second method is used for creating vectors with equally spaced elements:

t = 0:.1:10;
%creates a 1x101 vector with the elements 0, .1, .2, .3,...,10.
%Note that the middle number defines the increment.
%If only two numbers are given, then the increment is set to a default of 1:

k = 0:10;

%creates a 1x11 vector with the elements 0, 1, 2, ..., 10.

%Matrices are defined by entering the elements row by row:

M = [1 2 4; 3 6 8];
% or
M = [1 2 4
3 6 8]

%the size command is used to get the size of a matrix or vector.
%the length command is used to get the length of a vector


length(t)

size(t)

size(M)

%There are a number of special matrices that can be defined:

%null matrix: M = [ ];
%nxm matrix of zeros: M = zeros(n,m);
%nxm matrix of ones: M = ones(n,m);
%nxn identity matrix: M = eye(n);


%A particular element of a matrix can be assigned:

M(1,2) = 5;

%places the number 5 in the first row, second column.


%commands who and whos give the names of the variables that have been defined in the workspace.

who
whos

%Functions are applied element by element. For example,

t = 0:10;
x = cos(2*t);
% creates a vector x with elements equal to cos(2t) for t = 0, 1, 2, ..., 10.

% Operations that need to be performed element-by-element can be accomplished
% by preceding the operation by a ".". For example, to obtain a vector x that
% contains the elements of x(t) = tcos(t) at specific points in time,
% you cannot simply multiply the vector t with the vector cos(t).
% Instead you multiply their elements together:

t = 0:10;
x = t.*cos(t);

%help for MATLAB can be reached by typing help for the full menu or
% typing help followed by a particular function name or M-file name.
% For example, help cos gives help on the cosine function.

help cos
%
pause

num=[2 1 ]
den=[1 2 5]

%print a nice looking transfer function
system = tf(num,den)
printsys(num,den,'s')

[R,P,K]=residue(num,den)

%%the roots of the denominator are the poles
roots(den)
pause

%% go from a transfer function to zeros, poles, gain
[Z,P,K]=tf2zp(num,den)

[NUM,DEN] = zp2tf(Z,P,K)

pause

%%Plotting
%%Commands :
% plot
% xlabel
% ylabel
% title
% grid
% axis
% stem
% subplot

%The command most often used for plotting is plot, which creates linear
%plots of vectors and matrices; plot(t,y) plots the vector t on the x-axis
%versus vector y on the y-axis. There are options on the line type and the
%color of the plot which are obtained using plot(t,y,'option'). The linetype
%options are '-' solid line (default), '--' dashed line, '-.' dot dash line,
%':' dotted line. The points in y can be left unconnected and delineated by
%a variety of symbols: + . * o x. The following colors are available options:

% r red
% b blue
% g green
% w white
% k black


%For example, plot(t,y,'--') uses a dashed line, plot(t,y,'*') uses * at
%all the points defined in t and y without connecting the points,
%and plot(t,y,'g') uses a solid green line.
%The options can also be used together, for example, plot(t,y,'g:')
%plots a dotted green line.

%To plot two or more graphs on the same set of axes,
% plot(t1,y1,t2,y2), which plots y1 versus t1 and y2 versus t2.
t1=0:11;
y1=1:12;
t2=0:0.5:7.5;
y2=t2;
%To label your axes and give the plot a title, type

plot(t1,y1,t2,y2),
xlabel('time (sec)')
ylabel('step response')
title('My Plot')
% To a grid to your plot to make it easier to read
pause

grid
pause

%The problem that you will encounter most often when plotting functions is
%that MATLAB will scale the axes in a way that is different than you want
%them to appear. You can easily override the autoscaling of the axes by
%using the axis command after the plotting command:

%%axis([xmin xmax ymin ymax]);
axis([0 5 0 7])
% where xmin, xmax, ymin, and ymax are the limits you desire for the axes.
%To return to the automatic scaling, simply type
pause
axis
pause

%To plot more than one graph on the screen, use the command subplot(mnp)
%which partitions the screen into an m by n grid
%where p determines the position of the particular graph
%counting the upper left corner as p=1. For example,
w=0:0.05:2;
M1=w.^2;
M2=w.*w;
subplot(211),semilogx(w,M1);
subplot(212),semilogx(w,M2);

%plots the bode plot with the log-magnitude plot on top and
%Loading and Saving Data


%save datafile
%saves all workspace variables to the binary "MAT-file" named datafile.mat

%save datafile2 M1 M2 w
%saves all workspace variables to the binary "MAT-file" named datafile2.mat

%To retrieve the data later, type

%load datafile2