Вы находитесь на странице: 1из 15

Tutorial 1 (Day 1)

Matlab has many types of matrices which are built into the system. A 7 by 7 matrix
with random entries is produced by typing

rand(7)
You can generate random matrices of other sizes and get help on
the rand command within matlab:
rand(2,5)

Initialization of variables:

Problem 1: Variables a, b, c and d are initialized as follows:
a=3; b=2; c=5; d=3;
Evaluate the following MATLAB assignment statements:
(a) Output=a*b+c*d;
(b) Output=a*(b+c)*d;
(c) Output=(a*b)+(c*d);
(d) Output=a^(b^d);

To find more about rand in-built command, type help rand

Problem2: Generate a matrix of 57 order with random entries from 1 to 10.
You can generate your own matrix

A=[1 2 3;2 3 4;4 5 2] or [1,2,3;2,3,4;4,5,2];

>> eye(4)

ans =

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

>> [eye(4) randi(3,4)]

ans =

1 0 0 0 1 1 3 1
0 1 0 0 1 1 3 2
0 0 1 0 2 3 2 3
0 0 0 1 2 1 2 2

>> [eye(4) randi(3,4)] press Enter

Do you get an error. What does it say ?

Reading out matrix

>> A=randi(3,5)

A =

3 3 3 2 2
3 1 3 2 2
2 1 1 3 2
2 1 3 3 3
1 1 1 1 3

(a) Read all the elements of Ist column
(b) Read all the elements of 3rd column
(c) Read all the elements of fourth row
(d) Size of matrix
(e) Read out a particular element of matrix

Problem3: Run the following:

[eye(2);zeros(2)]

[eye(2);zeros(3)]

[eye(2),ones(2,3)]
Did any of the last three examples produce error messages? What is the problem?


Matrix Addition, subtraction, multiplication, division



>> A=[1 2 3];
>> B=[3 4 5];
>> A.*B

ans =

3 8 15

>> A+B

ans =

4 6 8
>> A-B


ans =

-2 -2 -2
>> A=[1 1 1;2 2 2]

A =

1 1 1
2 2 2

>> B=[2 2 2;2 2 2]

B =

2 2 2
2 2 2

>> A./B

ans =

0.5000 0.5000 0.5000
1.0000 1.0000 1.0000

Problem4: Generate two different 45 matrices with random entries and do the
operations such as (i) Addition (ii) Subtraction (iii) Multiplication (iv) Division. Check
the results.


Plotting facility

>> A=[1 2 3 4];
>> B=[4 2 5 7];
>> plot(A,B)


1 1.5 2 2.5 3 3.5 4
2
2.5
3
3.5
4
4.5
5
5.5
6
6.5
7

Setting the plot such as
(i) X and Y axis (ii) Font size of the plot (iii) Colour (iv) setting X-axis and Y
axis range (v) Plotting more no. of plots simulataneously ( vi) Writing text
on the plot (vii) reading points on the plot
Problem5: Generate two arrays of 110 size each and considering one array on the
X axis and other on the Y axis, plot it. Check for the above properties on your plot.
Multiple Plots
>> A=[1 2 3 4 5];
>> B=[3 5 1 3 2];
>> C=[5 2 1 3 6];
>> plot(A,B);
>> hold on
>> plot(A,C)
Change the different properties of these two plots such as color, font, line style etc.
Alternative way to multiple plots:
>> A=[1 2 3 4];
>> B=[3 4 2 6];
>> C=[2 3 3 1];
>> plot(A,B,A,C)
Plotting on Logarithmic Scale
Functions used are
Semilogx, semilogy
>> A=[1 2 3 4 5];
>> B=[1 10 100 1000 10000];
>> semilogy(A,B)




Problem 6:


Problem 7:








Problem 8:


Importance of clc and clear all (to be shown). Start the program with
clc; clear all;
CLEAR Clear variables and functions from memory.
CLEAR removes all variables from the workspace.
CLEAR VARIABLES does the same thing.
CLEAR GLOBAL removes all global variables.
CLEAR FUNCTIONS removes all compiled M- and MEX-functions.
CLEAR ALL removes all variables, globals, functions and MEX links.
CLEAR ALL at the command prompt also removes the Java packages import
list.
So always start the program with clc; clear all;
Note: All the work done above can also be done in M-file
Different ways of Getting help from MATLAB
Using help
Lookfor
Q: Search for following functions using lookfor command:
(i) integration (ii) differentiation (iii) interpolation (iv) plot
# Logical buil-in function
If logical functions are true, it returns 1 or else it returns 0
Isinf, isempty

>> a=20;b=-2;c=0;
>> isinf(a/b)
ans =
0
>> isinf(b/c)
ans =
1
>> isempty(c)
ans =
0



If construction
if control_expression1
Statement1

else if control_expression1
Statement1

end
Problem 9: Write a program in M-file to display the conditions of roots of quadratic
equation using if condition.
ax2+bx+c=0
roots are
a
ac b b
x
2
4
2

=
Sol:
clc
clear all

a=5;b=4;c=7;
x1=(-b+sqrt(b^2-4*a*c))/2/a;
x2=(-b-sqrt(b^2-4*a*c))/2/a;
if (b^2-4*a*c)>0
disp('Roots are real and distinct ');
elseif (b^2-4*a*c)==0
disp('Root is real and single root ');
else
disp('Roots are imaginary and distinct ');
end
disp(x1);disp(x2)

Problem 10: Write a MATLAB program using if condition to evaluate a function
f(x,y) for any two users defined values x and y:


0 y , 0 x y x
0 y , 0 y x x
0 y , 0 x y x
0 y , 0 x , ) , (
2 2
2
2
< < +
> < +
< > +
> > + = y x y x f

FOR loop structure
for i=1:n
statement
..
end
Problem 11: Develop a program for modified Bessel function given as follows using
for loop

=
+
+
=
0
) 2 (
)! ( !
) 2 / (
) (
k
n k
n
n k k
z
z I
Sol:
clc
clear all;
n=3;b=[0:0.1:4];II=[];
for i=1:length(b)
x=b(i);
A=[];
for s=1:100

k=s-1;
M=(x/2)^(2*k+n)/(factorial(k)*factorial(n+k));
A=[A M];
end
I=sum(A);II=[II I];
end
plot(b,II);

Problem 12: Using Simpson rule, obtain the Fourier Transform of the following pulse
and plot both the results.
( ) ) / exp( / 2 1
2 / 3
1
) (
2 2 2 2
t t
t
t
t
t t t s =
Sol:
% simpson formula
%I=(hu/3)*(f(1)*1+f(2)*4+f(3)*2+f(4)*4+.....+f(N-1)*4 +f(N)*1)
% where f(1), f(2) .. are the value of function at sample points
% N= total number of sample points
clc
clear all
f1=[1:1:10]*1e9;G=[];
for a=1:length(f1)
f=f1(a);w=2*pi*f;h=1;t1=0:1:200;L=length(t1);
M(2:2:L-1)=4;M(3:2:L-2)=2;M([1 L])=1;
FUNC=[];
for b=1:length(t1)
t=t1(b)*1e-10;tou=0.1e-9;
x1=1/tou*sqrt(tou/(3*sqrt(pi/2)));x2=(1-2*t^2/tou^2);x3=exp(-t^2/tou^2-j*w*t);
func=x1*x2*x3;FUNC=[FUNC func];
end
% fun1=(1/tau)*sqrt(tau./3./sqrt(pi/2)).*(1-(2.*t.^2)./tau.^2).*exp(-(t.^2)./tau.^2-j*w.*t);
fun1=FUNC.*M;I=(h/3)*sum(fun1);G=[G I];
end
%=====time domain pulse======
t1=-10:0.1:10;FUNC1=[];
for b=1:length(t1)
t=t1(b)*1e-10;tou=0.1e-9;
x1=1/tou*sqrt(tou/(3*sqrt(pi/2)));x2=(1-2*t^2/tou^2);x3=exp(-t^2/tou^2);
func=x1*x2*x3;FUNC1=[FUNC1 func];
end
subplot(2,1,2);plot(t1,FUNC1);
subplot(2,2,2);semilogy(f1,abs(G)/max(abs(G)));





Basic Signals Program
Problem1. Generation of Impulse function
0 ) ( = n o
-n -1 0 1 n
clc; clear all; close all;
n=-10:1:10;
y=[zeros(1,10) ones(1, 1) zeros(1,10);
plot(n,y);
ylabel(amplitude); xlabel(time);

Problem2. Generate the sequence for unit step sequence
U(n)

1 { ) ( = n u n>=0
clc; clear all; close all;
x=input('enter the sequence of length');
n=0:x;
y=ones(1,length(n));
stem(n,y);
ylabel('amplitude');
xlabel('time');
axis([0 length(n)+5 0 5]);

n
0 1 2 3 4 5 6 7 8
Problem3. Generation Ramp Signal
U(n)
)
`

>=
=
otherwise
n for n
n u
r
... .......... 0
0 __ ... ..........
) (

.n

0 1 2 3

clc; clear all; close all;
x=input('enter the sequence of length');
n=0:x;
y=n;
stem(n,y);
ylabel('amplitude');
xlabel('time');
% axis([0 length(n)+5 0 5]);

Problem4: Generate the Sine and Cosine functions and plot them simultaneously.
>> t=[0:0.01:1];
>> fc=1;
>> A=5*sin(2*pi*fc*t);
>> B=5*cos(2*pi*fc*t);
>> plot(t,A)
>> hold on
>> plot(t,B)
Change the properties of the plots.
The above can be plotted using discrete sequence called stem
Using stem(x,y) function, plot the above results.
Problem5: Generate the following functions
A=5sin(2*pi*fc*t+)+5cos
2
(2*pi*fc*t+ )
B=5sin
3
(2*pi*fc*t+)+5cos(2*pi*fc*t+)
Plot them simultaneously (i) =0 (ii) =pi/3
To plot the same (i) Use Command window (ii) M file
Problem6: Program for Linear convolution
(a) ) ( * ) ( ) (
2 1
n m x n x m y
n
=

=

But in matlab
% x1=[1 2 3];
% x2=[2 4 3 2];

For linear convolution Length of the sequence is N1+N2-1

) ( ) ( ) (
1 / 2
2 / 1
1
2 / 1
j x n x m y
N N
n
=

=


J=m+1-n;
1<=m<=(N1+N2-1)

clc; clear all; all clear;
x1=input('enter the 1st sequence');
x2=input('enter the 2nd sequence');
N1=length(x1);
N2=length(x2);
N=N2-N1;
n=0:(N1+N2-2);
for i=1:(N1+N2-1)
y(i)=0;
for k=1:N2
j=i+1-k;
% y(i)=y(i);
if j>0&&j<=N2-N %N(2/1)-() depend on the length of the x(2/1) if
it is greater than x(1/2) there should be the reduction in N(2/1) otherwise
addition in N(2/1) for equal length nothing will put woith N(2/1)
y1(k)=x2(k)*x1(j);
else
y1(k)=0;
end
% p=y1(k);
y(i)=y1(k)+y(i);

end
end
disp('the value of result'); disp(y);
stem(n,y);
xlabel('time');ylabel('amplitude');

(b) Linear convolution using circular convolution

When finding the linear using circular convolution we
have to make the sequence of the same length N=N1+N2-1
With zero padding.
) ( ) ( ) (
1 / 2
1
2 / 1
j x n x m y
N
n
=

=


J=m+1-n;
1<=m<=(N1+N2-1)


clc; clear all; all clear;
x1=input('enter the 1st sequence');
x2=input('enter the 2nd sequence');
N1=length(x1);
N2=length(x2);
x1=[x1, zeros(1,N2-1)];
x2=[x2, zeros(1,N1-1)];
N=length(x1);
n=0:(N-1);

for i=1:N
y(i)=0;
for k=1:N
j=i+1-k;
if j>0
j=j;
y1(k)=x2(k)*x1(j);

else
j=N+j;
y1(k)=x2(k)*x1(j);
end
y(i)=y1(k)+y(i);
end
end
disp('the value of result'); disp(y);
stem(n,y);
xlabel('time');ylabel('amplitude');

Program7: Program for circular convolution

=
=
1
0
1 / 2 2 / 1
)) (( ) ( ) (
N
n
N
n m x n x m y 1 ... .......... 4 , 3 , 2 , 1 , 0 = N m
If the sequences are of same length then N=N1=N2

=
=
N
n
N
j x n x m y
1
1 / 2 2 / 1
) ( ) ( ) ( N m ... .......... 4 , 3 , 2 , 1 =
J=m+1-n;
If sequences are not of same length then N=max(N1,N2)

=
=
N
n
N
j x n x m y
1
1 / 2 2 / 1
) ( ) ( ) ( N m ... .......... 4 , 3 , 2 , 1 =
J=m+1-n;
clc; clear all; all clear;
x1=input('enter the 1st sequence');
x2=input('enter the 2nd sequence');
N1=length(x1);
N2=length(x2);
if N1==N2
N=N1;
n=0:(N-1);
for i=1:N
y(i)=0;
for k=1:N
j=i+1-k;
if j>0
j=j;
y1(k)=x1(k)*x2(j);

else
j=N+j

y1(k)=x1(k)*x2(j);
end
y(i)=y1(k)+y(i);
end
end
else
N=max(N1,N2);
x1e=[x1, zeros(1,N-N1)];
x2e=[x2, zeros(1,N-N2)];
N=length(x1e);
n=0:(N-1);
for i=1:N
y(i)=0;
for k=1:N
j=i+1-k;
if j>0
j=j;
y1(k)=x2e(k)*x1e(j);

else
j=N+j;
y1(k)=x2e(k)*x1e(j);
end
y(i)=y1(k)+y(i);
end
end
end
disp('the value of result'); disp(y);
stem(n,y);
xlabel('time');ylabel('amplitude');

Program8: Discrete Fourier transform of given sequence

=
1
0
) ( ) (
N
n
n k
N
w n x k X 1 .... .......... 3 , 2 , 1 , 0 = N k
N
j
N
e w
t 2
=
In Matlab

=

=
N
n
n k
N
w n x k X
1
) 1 ( ) 1 (
) ( ) ( N k .... .......... 3 , 2 , 1 =
clc; clear all; all clear;
x=input('enter the sequence');
N=length(x);
n=0:N-1;
for K=1:N
Y(K)=0;
for i=1:N
X(i)=x(i)*exp(-j*2*pi*(K-1)*(i-1)/N);
Y(K)=X(i)+Y(K);
end
Y1(K)=angle(Y(K));
Y2(K)=abs(Y(K));

end
disp('the value of result'); disp(Y);
disp('the value of result'); disp(Y1);
disp('the value of result'); disp(Y2);
subplot(2,1,1);
stem(n,Y2);
xlabel('time');ylabel('amplitude');
subplot(2,1,2);
plot(n,Y1);
xlabel('time');ylabel('angle');

Program9: Program for Inverse Discrete Fourier transforms (IDFT)

=

=
1
0
) (
1
) (
N
k
n k
N
w k X
N
n x 1 .... .......... 3 , 2 , 1 , 0 = N n
In Mat lab

=

=
N
k
n k
N
w k X
N
n x
1
) 1 ( ) 1 (
) (
1
) ( N n .... .......... 3 , 2 , 1 =
clc; clear all; all clear;
x=input('enter the sequence');
N=length(x);
n=0:N-1;
for K=1:N
Y(K)=0;
for i=1:N
X(i)=(1/N)*x(i)*exp(j*2*pi*(K-1)*(i-1)/N);
Y(K)=X(i)+Y(K);
end
Y1(K)=angle(Y(K));
Y2(K)=abs(Y(K));
end
disp('the value of result'); disp(Y);
disp('the value of result'); disp(Y1);
disp('the value of result'); disp(Y2);
subplot(2,1,1);
stem(n,Y2);
xlabel('time');ylabel('amplitude');
subplot(2,1,2);
plot(n,Y1);
xlabel('time');ylabel('angle');

Вам также может понравиться