Matlab Assignment-01 SEM-II-2016-2017 PDF
Matlab Assignment-01 SEM-II-2016-2017 PDF
Matlab Assignment-01 SEM-II-2016-2017 PDF
Learning Familiarize with the Matlab environment and running some basic Matlab
Objectives: commnds in Matlab
Equipment Matlab
Required:
For example
x=1:2:10
will generate x= 1 3 5 7 9
Try using angles=(0.01:0.01:1.00)*pi;
Transpose Operator():
This operator swaps the rows and columns of any array that it is applied to.
For example try using:
F=[1:4]
And F=[1:4]
Initializing with Built-in functions:
Try using these:
A=zeros(2)
B=zeros(2,3)
C=[1 2; 3 4]
D=zeros(size(C))
E=ones(3)
Ones(3,1)
Eye(3)
Eye(3,2)
Length(C) //Generate the longest dimension of the array.
Zeros can be used to create an all zero array of any desired size. If function has a single
square array; it will generate a square array using the single argument as both the number of
rows and columns. size function returns two values containing the number of rows and
columns in an array.
Initializing variables with keyboard input:
Input function displays a prompt string in the command window and then waits for the user
to type in a response. For example , consider the following statement:
My_val=input(enter an input value);
Multidimensioanl Array
Matlab allows us to create arrays as many dimensions as necessary for any given problem.
These arrays have one subscript for each dimension and an individual element in the array
will be the product of the maximum value of each subscript. For example the following two
statements create a 2X2X3 array C:
C(:,:,1)=[1 2 3; 4 5 6];
C(:,:,2)=[7 8 9; 10 11 12];
Whos C
End Function:
It is very useful for creating array subscripts. When used in an array subscript; end returns
the highest value taken on by that subscript. For example. Suppose that array arr3 is defined
as follows:
Arr3=[ 1 2 3 4 5 6 7 8];
Then Arr1(5:end) would be the array [5 6 7 8] and array(end) would generate 8.
The value returned by end is always the highest value of a given subscript. Is end appears in
different subscripts, it can return different values within the same expression. For example,
suppose that the 3X4 array Arr4 is defined as follows:
Arr4=[1 2 3 4; 5 6 7 8; 9 10 11 12];
Then the expression
Arr4(2:end, 2:end)
would return the array.(Try this one).
TASKS
Question # 01:
This M-file calculates and plot the function sin(x) for 0<=x<=6
X=0:0.1:6;
Y=sin(X);
plot(X,Y);
plot(X,Y)
Question # 02:
The following Matlab statement plot the function y(x)=2e-0.2x for the range 0<=x<=10
X=0:0.1:10;
Y=2*exp(0.2*X);
Plot(X,Y);
Question # 03:
suppose that u=1 and v=3. Evaluate the following expressions using Matlab.
(a) 4u/3v
(b) 2v-2/(u+v)2
(c) v3/v3-u3
(d) 4/3 pi.v2
Question # 04:
(a) Generate a 6X6 Matrix.
(b) Generate a 6X1 Matrix
(c) Generate a 3X4 Matrix
Question # 05
Answer the following questions for the array shown below.
1.1 0.0 2.1 -3.5 6.0
0.0 1.1 -6.6 2.8 3.4
1.1 0.1 0.3 -0.4 1.3
-1.4 5.1 0.0 1.1 0.0
(a) what is the size of the array?
(b) What is the value of the array(4,1)?
(c) What is the size and value of array( : , 1 : 2 )?
(d) What is the size and Value of the (array[1 3],end)?
Question # 06
Are the following Matlab variable names legal or illegal? Why?
(a) dog1
(b) 1dog
(c) Do_you_know_the_way__to_kuala lampur
(d) _help
xp=[0:pi/10:pi]
xp[0:0.1:1]*pi
xp = linspace(0,,11)
If not stated otherwise, all vectors in MATLAB are indexed starting from 1, e.g., x( 1) is the
first element of vector x. Thus, in order to represent a signal with negative or zero time index,
an additional index vector should be defined. The following code will define a discrete-time
signal x[n]=2n, for n = 3,..,3:
x=2*n;<ret>
plot(n,x)<ret> % This command is to show the vector x[n] graphically
In MATLAB both stem and plot can be used to plot a signal. The difference between
these two functions can be seen by typing the following example:
Question # 07
Evaluate the following expressions (remember to include numerical answers in the result section
and executable MATLAB code as attachment of your report):
Question # 08
Exchange the stem and plot functions in the example, and watch the results.
Explain the advantages and disadvantages of each function.
The following example describes the process of creating a sampled sinusoidal signal:
Example 5: Evaluate sin(pi*x) from 0 to 1 using a sampling period of 0.1: create an array for the
argument of sine,
y= sin(xp);<ret>
length(y)< ret>
close
Question # 09
Create and plot the following signals using sampling rate l0 Hz (i.e. 10 samples per second).
Include the plots in your report:
a. y1 = cos(5t);
b. y2 = 2exp(2t)*cos(5t);
c. add a noise scaled by 0.2 on y1.
(use function randn, and type help randn if you dont know how to use it).