LAB1 final

14
SIGNALS AND SYSTEMS LAB Submitted by: Aakanksha Bansal(09103410) Ashima Agrawal(09103408) Batch:B1

Transcript of LAB1 final

Page 1: LAB1 final

SIGNALSAND

SYSTEMS LAB

Submitted by:

Aakanksha Bansal(09103410)

Ashima Agrawal(09103408)

Batch:B1

EXPERIMENT-1

Page 2: LAB1 final

Aim: Introduction to MATLAB and its various applications.

Theory : Z = trapz(Y) computes an approximation of the integral of Y via the trapezoidal method (with unit spacing). To compute the integral for spacing other than one, multiply Z by the spacing increment. Input Y can be complex.

The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct control over the number of points.

y = linspace(a,b) generates a row vector y of 100 points linearly spaced between and including a and b.

y = linspace(a,b,n) generates a row vector y of n points linearly spaced between and including a and b. For n < 2, linspace returns b.

POST LAB - EXCERCISESQ1).Scalar variables. Make the following variablesa. a = 10b. b = 2.5 ×10 23

c. c = 2 + 3i , where i is the imaginary number

Page 3: LAB1 final

d. d = ej 2π /3 , where j is the imaginary number and e is Euler’s number (use exp, pi)Ans:a = 10;b = 2.5*10^23;c = 2+3i;d=exp(j*2*pi/3);

Q2). Vector variables.Make the following variables

Ans:aVec = [3.14 15 9 26];bVec = [2.71;8;28;182];cVec = [5 :-0.2 :-5];dVec = logspace(0,1,100);eVec = ['hello'];

Q3).Matrix variables.Make the following variables

Page 4: LAB1 final

Ans)3(a)

mat1=ones(9,9)mat1 = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

>>amat = amat=2.*mat1amat =

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

Page 5: LAB1 final

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

3(b)v=[1,2,3,4,5,4,3,2,1]bmat=zeros(9,9)bmat=diag(v,0)

b=

1 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1

3(c)incr = 1:1:100;cmat=reshape(incr,10,10)cmat = reshape(x,10,10);>>cmat = reshape(x,10,10)

cmat = 1 11 21 31 41 51 61 71 81 91 2 12 22 32 42 52 62 72 82 92 3 13 23 33 43 53 63 73 83 93 4 14 24 34 44 54 64 74 84 94 5 15 25 35 45 55 65 75 85 95 6 16 26 36 46 56 66 76 86 96 7 17 27 37 47 57 67 77 87 97 8 18 28 38 48 58 68 78 88 98 9 19 29 39 49 59 69 79 89 99 10 20 30 40 50 60 70 80 90 100

3(d)mat3=ones(3,4)dmat=mat3.*NaNdmat=

Page 6: LAB1 final

NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN

3(e)emat = [13 -1 -5; -22 10 -87];emat =

13 -1 -5 -22 10 -87

3(f)fmat = ceil(-3 + 6.*rand(5,3));fmat =

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

Q4).Common functions and indexing.

Ans)4(a)cSum =sum(cmat);

4(b)cMean =mean(cmat);

4(c)

Page 7: LAB1 final

emat(1,:) = [1 1 1];

4(d)cSub = cmat(2:9,2:9)

4(e)lin = 1:1:20;for j=1:20if(mod(lin(j),2)==0)lin(j)=-j;end;end;

4(f)r=rand(1,5)

a=find(r<0.5)r(a)=0

Q5)Plotting multiple lines and colors.Open a script and name it twoLinePlot.mTo plot a sine wave and a cosine wave over one period(a) Make a time vector t from 0 to 2ð with enough samples to get smooth lines, Plot sin t.(b) Use hold on.(c) Plot cos t using a red dashed line.(d) Add labels to the plot and create a legend to describe the two lines you have plotted byusinglegend.(e) Use xlimto set the x axis to be from 0 to 2ð and use ylimto set the y axis to be from -1.4 to 1.4.

Ans)t = linspace (0, 2*pi ,1000);plot (t,sin(t));hold on;plot (t,cos(t),'--r');p =plot (t,sin(t));q =plot (t,cos(t),'--r');

Page 8: LAB1 final

legend (p,'sinwave','location','North');legend (q,'coswave','location','South');xlim([0 2*pi]);ylim([-1.4 1.4]);

Q6)Plot a circle. Write the function [x, y] = getCircle (center,r) to get the x and ycoordinates of a circle. The circle should be centered at center (2-element vector containing the x and y values of the center) and have radius r. Return x and y such that plot(x, y) will plot the circle.

t = linspace(0,2*pi,1000); a = input('enter the x coord of the centre');b = input('enter the y coord of the centre');c= input('enter the radius of the circle');x = r*cos(t)+a; y = r*sin(t)+b; plot(x,y);

Page 9: LAB1 final

7. Loops and flow control. Make function called loopTest(N) that loops through thevalues 1 through N and for each number n it should display ‘n is divisible by 2’, ‘n isdivisible by 3’, ‘n is divisible by 2 AND 3’ or ‘n is NOT divisible by 2 or 3’. Use a forloop, the function mod or rem to figure out if a number is divisible by 2 or 3, and num2strto convert each number to a string for displaying. You can use any combination of if, else,andelseif.

Ans)

p=input('enter max no');

for j=1:p

if (mod(j,3)==0 && mod(j,2)==0)

disp('num div by both');

Page 10: LAB1 final

disp(j);

elseif(mod(j,3)==0)

disp('div by 3');

disp(j);

else if(mod(j,2)==0)

disp('div by 2');

disp(j);

else

disp('div by none');

disp(j);

end;

end;

end;

Q8)

Linear system of equations.Solve the following system of equations:

3a + 6b + 4c = 1. a + 5b = 2. 7b + 7c = 3Ans)A=[3,6,4;1,5,0;0,7,7]b=[1;2;3]x=A\bx = -0.5824 0.5165 -0.0879

Q9)Numerical integration.Use trapzor quad. What is the value of :Compute and display the difference between your numerical answer and the analytical answer: −24 e−5/3+ 9

Ans)x = 0:0.01:5;y=x.*exp(-x*1/3)

Page 11: LAB1 final

z=trapz(x,y)w=4.3736difference=w-zdiff = 0.0934

Q10)Practice with cells.Usually, cells are most useful for storing strings, because thelength of each string can be unique. a. Make a 3x3 cell where the first column contains the names: ‘Joe’, ’Sarah’, and ’Pat’, the second column contains their last names: ‘Smith’,‘Brown’, ‘Jackson’, and the third column contains their salaries: $30,000, $150,000, and $120,000. Display the cell using disp.

Ans)

z=cell(3)z(1,:)={'joe','smith','$30000'}z(2,:)={'sarah','brown','$150000'}z(3,:)={'pat','jackson','$120000'}disp(z)

LEARNING OUTCOME

1. Creating a file and saving to the directory.2. Working with matrix of size of mxn and using the dot operator.3. Using various predefined mathematical operators