Top Banner
EXPERIMENT NO.:1 Aim : To study the basic MATLAB Arithmetic Commands. Tools : MATLAB 7.0.4 Syntax used N/A Code : a=16 b=27 cube_a=a^3 sum=a+b diff=a-b mult=a*b div=a/b c=[1 4 16;25 49 -1] sqrt(c) Output : a =
76
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: matlab (2)

EXPERIMENT NO.:1

Aim:To study the basic MATLAB Arithmetic Commands.

Tools : MATLAB 7.0.4

Syntax usedN/ACode:a=16

b=27

cube_a=a^3

sum=a+b

diff=a-b

mult=a*b

div=a/b

c=[1 4 16;25 49 -1]

sqrt(c)

Output :

a =

16

b =

Page 2: matlab (2)

27

cube_a =

4096

sum =

43

diff =

-11

mult =

432

div =

0.5926

c =

1 4 16 25 49 -1

ans =

1.0000 2.0000 4.0000 5.0000 7.0000 0 + 1.0000i

>>

Page 3: matlab (2)

EXPERIMENT NO.:2

Aim:For an electric circuit with inductance L=.01mH andresistance R=100 ohm, the damped natural frequency ofoscillaton is

f=[1/LC-R2/4C2]1/2.Write a program to calculatethe frequency for different values of C varying from 0.1 to1 in steps of 0.1.

Tools : MATLAB 7.0.4

Syntax used :for variable = expression

statementsend

Code:%variation of frequency with capacitance%L=.00001;R=100;

for C=.1:.1:1 f=sqrt((1/(L*C)-((R^2)/(4*(C^2)))))end

Output :

f =

Page 4: matlab (2)

866.0254f =

661.4378

f =

552.7708

f =

484.1229

f =

435.8899

f =

399.6526

f =

371.1537

f =

347.9853

f =

328.6711

f =

Page 5: matlab (2)

312.2499

EXPERIMENT NO.:3Aim : Write a MATLAB program to define the vectors :

u = <1 2 3 4> v = <2 3 4 5>

Now determine the following using matlab.

a.) Multiply the vector v with a scalar quantity ,i.e., 5v.b.) Take the power of 5 of a vector, i.e., u5 and v5.c.) Multiply the vectors element by element(u.*v).d.) First take the transpose of a vector and then

multiply the vector element to element(u’*v).

Tool : MATLAB 7.0.4

Syntax used :Transpose of A=A’Multiplying AandB(element by element): A.*B

Code:

%matrix properties%u=[1 2 3 4]v=[2 3 4 5]for row=1:1for col=1:4uv(row,col)=u(row,col)*v(row,col);endendmult=5*vu5=u.^5v5=v.^5

Page 6: matlab (2)

x=u'y=v'uvuv'

Output :u =

1 2 3 4

v =

2 3 4 5

mult =

10 15 20 25

u5 =

1 32 243 1024

v5 =

32 243 1024 3125

x =

1

Page 7: matlab (2)

2 3 4

y =

2 3 4 5

uv =

2 6 12 20

ans =

2 6 12 20

>>

Page 8: matlab (2)

Experiment no.:4

Aim : Write a matlab program to calculate:

a.) Summation of a number k from 0 to 100.b.) Summation of a number k2from 0 to 100.c.) Summation of a number 1/k from 1 to 100.d.) Summation of a number 1/k2from 1 to 100.

Tool: MATLAB 7.0.4

Syntax:

for variable = expressionstatementsend

DescriptionThe columns of the expression are stored one at a time in the variable while the following statements, up to the end, are executed.

Code:%series sum%sum=0;sqr_sum=0;inv_sqr_sum=0;inv_sum=0;

fork=0:100sum=sum+k;sqr_sum=sqr_sum+(k^2);

ifk>1

Page 9: matlab (2)

inv_sum=inv_sum+(1/k);inv_sqr_sum=inv_sqr_sum+(1/(k^2));

endend

sumsqr_suminv_sqr_sum

Output :

sum =

5050

sqr_sum =

338350

inv_sqr_sum =

0.6350

>>

Page 10: matlab (2)

EXPERIMENT NO.:5Aim :Write a MATLAB program to break a function(saysin(x)), into odd and even part.One can compute the two functions as :

fodd= f (x )−f (−x )2

feven=f ( x )+ f (−x)

2

Tool: MATLAB 7.0.4

Syntax:

for variable = expressionstatementsend

DescriptionThe columns of the expression are stored one at a time in the variable while the following statements, up to the end, are executed.

Code:

%odd and even function%for x=1:60

fodd(x)=(sin(x)-sin(-x))/2; %odd partfeven(x)=(sin(x)+sin(-x))/2; %even part

endfoddfeven

Output:

fodd =

Columns 1 through 11

Page 11: matlab (2)

0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121 -0.5440 -1.0000

Columns 12 through 22

-0.5366 0.4202 0.9906 0.6503 -0.2879 -0.9614 -0.7510 0.1499 0.9129 0.8367 -0.0089

Columns 23 through 33

-0.8462 -0.9056 -0.1324 0.7626 0.9564 0.2709 -0.6636 -0.9880 -0.4040 0.5514 0.9999

Columns 34 through 44

0.5291 -0.4282 -0.9918 -0.6435 0.2964 0.9638 0.7451 -0.1586 -0.9165 -0.8318 0.0177

Columns 45 through 55

0.8509 0.9018 0.1236 -0.7683 -0.9538 -0.2624 0.6702 0.9866 0.3959 -0.5588 -0.9998

Columns 56 through 60

-0.5216 0.4362 0.9929 0.6367 -0.3048

feven =

Columns 1 through 19

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 12: matlab (2)

Columns 20 through 38

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Columns 39 through 57

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Columns 58 through 60

0 0 0

>>

Page 13: matlab (2)

EXPERIMENT NO.:6Aim :Calculate following using MATLAB:

a. Define the symbolic variable x=sin(x).b. Define the symbolic function y=ex.c. Define the symbolic function y=e-x.d. Using the result of previous two sections, calculatefodd(x) and feven(x) for the function y=ex.

Tool: MATLAB 7.0.4

Syntax:

Y = sin(X)Y = sin(X) returns the circular sine of the

elements of XY = exp(X)

Y = exp(X) returns the exponential for each element of X.Code:

%even and odd function%x=1:20;x=sin(x)y1=exp(x)y2=exp(-x)fodd=(y1-y2)/2feven=(y1+y2)/2

Output:x =

Columns 1 through 11

0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121 -0.5440 -1.0000

Columns 12 through 20

Page 14: matlab (2)

-0.5366 0.4202 0.9906 0.6503 -0.2879 -0.9614 -0.7510 0.1499 0.9129

y1 =

Columns 1 through 11

2.3198 2.4826 1.1516 0.4692 0.3833 0.7562 1.9290 2.6895 1.5100 0.5804 0.3679

Columns 12 through 20

0.5847 1.5222 2.6929 1.9161 0.7498 0.3824 0.4719 1.1617 2.4917

y2 =

Columns 1 through 11

0.4311 0.4028 0.8684 2.1314 2.6089 1.3224 0.5184 0.3718 0.6622 1.7229 2.7183

Columns 12 through 20

1.7101 0.6569 0.3714 0.5219 1.3336 2.6153 2.1191 0.8608 0.4013

fodd =

Columns 1 through 11

0.9444 1.0399 0.1416 -0.8311 -1.1128 -0.2831 0.7053 1.1588 0.4239 -0.5713 -1.1752

Columns 12 through 20

Page 15: matlab (2)

-0.5627 0.4326 1.1608 0.6971 -0.2919 -1.1165 -0.8236 0.1504 1.0452

feven =

Columns 1 through 11

1.3754 1.4427 1.0100 1.3003 1.4961 1.0393 1.2237 1.5307 1.0861 1.1517 1.5431

Columns 12 through 20

1.1474 1.0896 1.5321 1.2190 1.0417 1.4989 1.2955 1.0113 1.4465

>>

Page 16: matlab (2)

EXPERIMENT NO.:7Aim:Write a MATLAB program to generate different waveforms and signals:

a) Square wave e)Unit step function u(t)b) Sine wave f )Unit impulse

functionδ(t)c) Triangle wave g)A rentangular pulse of width

2 d) Exponential wave h)Unit ramp function r(t)

Tool: MATLAB 7.0.4

Syntax:

x = square(t),generates a square wave with period 2π for the elements of time vector t.Y = sin(X),returns the circular sine of the elements of X.X=sawtooth(t),generates a sawtooth wave with period 2 for the elements of time vector t.

Code:

%SQUARE WAVE%f1=10;a=1;dc=20;f=f1*2*pi;t=0:.0001:1;y=a*square(f*t,dc);plot(t,y),grid on ,xlabel('t--->'),ylabel('x(t)--->'),title('SQUARE WAVE')axis([0 1 -2.2 2.2]);--------------------------------------------------

%SINE WAVE%x=-2*pi:pi/10:2*pi;

Page 17: matlab (2)

y=sin(x)

plot(x,y),grid on, xlabel('x'),ylabel('sin(x)'),title('Sine Wave')--------------------------------------------------%TRIANGLE WAVE%t=-4*pi:pi/2:4*pi;x = sawtooth(t,.5);plot(t,x),grid on,xlabel('t--->'),ylabel('x(t)--->'),title('TRIANLGE WAVE')

--------------------------------------------------

%exponential curve%x=-2:.01:2;y=exp(x);plot(x,y),grid on,xlabel('x--->'),ylabel('e^x--->'),title('Exponential wave')

--------------------------------------------------%Unit step function%

t = -5 : .01 : 5

u = (t >= 0)

plot(t,u),axis([-6 6 -0.2 1.2]),grid on,xlabel('t--->'),ylabel('u(t)--->'),title('Unit step function')--------------------------------------------------

%unit impulse function%

t=-8:.01:8;y=(t==0);plot(t,y),xlabel('t--->'),ylabel('d(t)--->'),title('Unit impulse function')--------------------------------------------------

%unit ramp function%

Page 18: matlab (2)

t=0:0.1:5;u=2*pi*t;plot(t,u),grid on,xlabel('t--->'),ylabel('tu(t)--->'),title('Unit ramp Function')

Output:

Page 19: matlab (2)
Page 20: matlab (2)
Page 21: matlab (2)
Page 22: matlab (2)

EXPERIMENT NO.:8Aim:Draw graphs of following function using matlab:

i)sin xx ii)

1

(x−1)2+x iii) x

2+1x2−4

iv)(10−x)1 /3−2

(4−x2)1/2

Tool: MATLAB 7.0.4

Syntax:

plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. If only Xn or Yn is a matrix, the vector is plotted versus the rows or columns of the matrix, depending on whether the vector's row or column dimension matches the matrix.

fplot(fun,limits)

Description

fplot plots a function between specified limits. The function must be of the form y = f(x), where x is a vector whose range specifies the limits, and y is a vector the same size as x and contains the function's value at the points in x

Code:

%8(i)Graph for the function "sine x/x"%

x=-30*pi:pi/10:30*pi;y=(sin(x)./x);

Page 23: matlab (2)

plot(x,y),grid on,xlabel('x'),ylabel('sin(x)/x'),title('Fig.8(i)..')--------------------------------------------------

%8(ii)%

x=-40:.001:40;

y=1./(((x-1).^2)+x);

plot(x,y),grid on,xlabel('x--->'),ylabel('1/(x-1)^2+x--->'),title('fig 8.(ii)')

--------------------------------------------------%8(iii)%x=-3.25:.1:3.25;nr=(x.^2+1);dr=(x.^2-4);y=nr./dr;plot(x,y),grid on,xlabel('x'),ylabel('x^2+1/x^2-4'),title('8(iii)')

--------------------------------------------------%8(iv)%clcfplot(@(x)(((10-x).^1/3)-2)/((4-x.^2).^1/2),[-3 3]),grid on,xlabel('x--->'),ylabel('((10-x)^1^/^3-2)/(4-x^2)^1^/^2');

Page 24: matlab (2)

Output:

Page 25: matlab (2)
Page 26: matlab (2)

EXPERIMENT NO.:9Aim:Draw a circle of radius r=5 using Matlab

Tool: MATLAB 7.0.4

Syntax:Y= cos(X) returns the circular cosine for each element of X.

Y = sin(X) returns the circular sine of theelements of X.

plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. If only Xn or Yn is a matrix, the vector is plotted versus the rows or columns of the matrix, depending on whether the vector's row or column dimension matches the matrix.

Code:%Circle%t=-2*pi:pi/100:2*pi;x=5*cos(t);y=5*sin(t);plot(x,y),axis equal,grid on,xlabel('x--->'),ylabel('y--->'),title('Circle')

Page 27: matlab (2)

Output:

Page 28: matlab (2)

EXPERIMENT NO.:10Aim:Write a program in MATLAB for factorial to compute factorial n! , for n=50.

Tool: MATLAB 7.0.4

Syntax:

factorial(N)

Description

factorial(N), for scalar N, is the product of all the integers from 1 to N

Code:%factorial of 50%A=50;y=factorial(A)

Output:

y =

3.0414e+064

>>

Page 29: matlab (2)

EXPERIMENT NO.:11Aim:Write a MATLAB program to calculate sum of all powers of 2 below 50 using while command.

Tool: MATLAB 7.0.4

Syntax:

whileexpressionstatementsend

Description

while repeats statements an indefinite number of times. The statements are executed while the real part of expression has all nonzero elements. expression is usually of the form expressionrel_op expression

whererel_op is ==, <, >, <=, >=, or ~=.

The scope of a while statement is always terminated with a matching end.

Code:

%sum of powers of 2%

i=0;sum=0;

Page 30: matlab (2)

whilei<50sum=sum+(2^i);i=i+1;end

sum

Output:

sum =

1.1259e+015

>>

Page 31: matlab (2)

EXPERIMENT NO.:12Aim:Write a MATLAB program to solve the equations:

-6x-2y+2z=15-3x+4y-3z=13 2x+4y-7z=-9

Tool: MATLAB 7.0.4

Syntax:

Y = inv(X)

Description

Y = inv(X) returns the inverse of the square matrix X.

Code:

%EQUATION SOLVING%A=[-6 -2 2;-3 4 -3;2 4 -7]B=[15;13;-9]a=inv(A);X=a*B

Output:

A =

-6 -2 2 -3 4 -3 2 4 -7

Page 32: matlab (2)

B =

15 13 -9

X =

-2.7273 2.7727 2.0909

>>

Page 33: matlab (2)

EXPERIMENT NO.:13Aim:Write a MATLAB program for fibonnaci series.

Tool: MATLAB 7.0.4

Syntax:

user_entry = input('prompt')

Description

The response to the input prompt can be any MATLAB expression, which is evaluated using the variables in the current workspace.

fprintf(format, var)

Description

The fprintf command displays formatted text centered on the icon and can display format along with the contents of var.

Code:

%fibonnaci series%clcx=0;y=1;i=1;lim=input('Enter number of terms in the series:');fprintf('\nTheFibonnaci Series is:\n%d\n%d\n',x,y);

Page 34: matlab (2)

whilei<=(lim-2) y=x+y; x=y-x;fprintf('%d\n',y);i=i+1;end

Output:

Enter number of terms in the series:10

The Fibonnaci Series is:0112358132134>>

Page 35: matlab (2)

EXPERIMENT NO.:14Aim:Write a MATLAB program for the calculation of following :

1-1/2+1/3-1/4+1/5…..

Tool: MATLAB 7.0.4

Syntax:user_entry = input('prompt')

Description

The response to the input prompt can be any MATLAB expression, which is evaluated using the variables in the current workspace.

fprintf(format, var)

Description

The fprintf command displays formatted text centered on the icon and can display format along with the contents of var.

Code:

%q14%clcsum=0;

Page 36: matlab (2)

k=-1;i=1;lim=input('Enter the series limit');whilei<=limsum=sum+(-k)*1/(i^2);i=i+1;endfprintf('\nSum of series is:\n%.3f\n',sum);

Output:

Enter the series limit5

Sum of series is:1.464>>

Page 37: matlab (2)

EXPERIMENT NO.:15Aim:Write a MATLAB program for generalized following:

1+ 122

+ 132

…..n terms

Tool: MATLAB 7.0.4

Syntax:fprintf(format, var)

Description

The fprintf command displays formatted text centered on the icon and can display format along with the contents of var.

Code:%generalised series%clcn=input('ENTER THE NUMBER OF TERMS IN THE SERIES');S=0;

fori=1:n S=S+1/(i^2);end

fprintf('Sum of the series is:\n%.4f\n ',S);

Output:

ENTER THE NUMBER OF TERMS IN THE SERIES6Sum of the series is:1.4914

Page 38: matlab (2)

>>

Page 39: matlab (2)

EXPERIMENT NO.:16Aim:Write a MATLAB program that accepts sides of triangle a,b,c as input and give area

A=√ (s−a ) (s−b ) ( s−c ), where s=(a+b+c)/2as output.

Tool: MATLAB 7.0.4

Syntax:fprintf(format, var)

Description

The fprintf command displays formatted text centered on the icon and can display format along with the contents of var.

Code:

%triangle area%clc

a=input('enter side a');b=input('enter side b');c=input('enter side c');

s=(a+b+c)/2;A=sqrt((s-a)*(s-b)*(s-c));fprintf('Area of triangle with sides \na=%.2f\nb=%.2f\nc=%.2f\nis %.2f\n',a,b,c,A);

Page 40: matlab (2)

Output:

enter side a3enter side b4enter side c5Area of triangle with sides a=3.00b=4.00c=5.00is 2.45>>

Page 41: matlab (2)

EXPERIMENT NO.:17Aim:Write a MATLAB program to compute and display even powers of 2 less than or equal to a positive number.

Tool: MATLAB 7.0.4

Syntax:

fprintf(obj,'cmd')

DESCRIPTION

fprintf(obj,'cmd') writes the string cmd to the device connected to obj. The default format is %s\n. The write operation is synchronous and blocks the command line until execution is complete.

Code:%even power%clcnum= input('Enter the limiting number');

i=0;sum=0;

while (2^i<=num) sum=sum+2^i; i=i+2;endi=i-1;if sum>num sum=sum-2^i;end

fprintf('Sum of powers even 2 below %d is: %d\n',num,sum);

Output:

Page 42: matlab (2)

Enter the limiting number 23Sum of powers even 2 below 23 is: 21>>

Page 43: matlab (2)

EXPERIMENT NO.:18Aim:Use the command plot3(x,y,z) to plot the circular helix

x(t)=sin t, y(t)=cos t, z(t)=t, 0 ≤ t ≤ 20

Tool: MATLAB 7.0.4

Syntax :

plot3(X1,Y1,Z1,...)

Description:

plot3(X1,Y1,Z1,...), where X1, Y1, Z1 are vectors or matrices, plots one or more lines in three-dimensional space through the points whose coordinates are the elements of X1, Y1, and Z1.

Code:

%Circular Helix%clct=0:.1:20;x=sin(t);y=cos(t);z=t;plot3(x,y,z),grid on,xlabel('sin(t)---->'),ylabel('cos(t)---->'),zlabel('t---->'),title('Circular helix')

Page 44: matlab (2)

Output:

Page 45: matlab (2)

EXPERIMENT NO.:19Aim:Plot the surface defined by the function using MATLAB.

f(x,y)=(x-3)2+(y-2)2 for 2≤ x≤4, 1≤y≤3

Tool: MATLAB 7.0.4

Syntax :

[X,Y] = meshgrid(x,y)

Description:

[X,Y] = meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables and three-dimensional mesh/surface plots. The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.

Code:%Parabolloid Plot%[x,y]=meshgrid(2:.1:4,1:.1:3);

z=(x-3).^2+(y-2).^2;surf(x,y,z),grid on, xlabel('x---->'),ylabel('y---->'),zlabel('f(x,y)---->'),title('PARABOLLOID');

Page 46: matlab (2)

Output:

Page 47: matlab (2)

EXPERIMENT NO.:20Aim:Find the solution of first order ODE:

y′=2x+5sin x, y(0)=1

Tool: MATLAB 7.0.4

Syntax :

r = dsolve('eq1,eq2,...', 'cond1,cond2,...', 'v')

Description:

dsolve('eq1,eq2,...', 'cond1,cond2,...', 'v') symbolically solves the ordinary differential equation(s) specified by eq1, eq2,... using v as the independent variable and the boundary and/or initial condition(s) specified by cond1,cond2,....

Code:

%Ordinary Differential equation %clc

y=dsolve('Dy=2*x+5*sin(x)','y(0)=1','x')

Output:

y = x^2-5*cos(x)+6 >>

Page 48: matlab (2)

EXPERIMENT NO.:21

Aim:Plot r2=2sin 5t, for 0 ≤ t ≤2π using polar command.

Tool: MATLAB 7.0.4

Syntax :

polar(theta,rho)

Description:

polar(theta,rho) creates a polar coordinate plot of the angle theta versus the radius rho. theta is the angle from the x-axis to the radius vector specified in radians; rho is the length of the radius vector specified in dataspace units.

Code:

%polar function%clct=linspace(0,2*pi,1000);

r=sqrt(2*sin(5*t));r=r.^2;polar(t,r),title('r^2=2sin5t')

Page 49: matlab (2)

Output:

Page 50: matlab (2)

EXPERIMENT NO.:22

Aim:Plot

z=−5

1+ x2+ y2, |x|≤3,|y|≤3 using contour3 command.

Tool: MATLAB 7.0.4

Syntax :

contour3(X,Y,Z,n)

Description:contour3(X,Y,Z,n), uses X and Y to define the x- and y-axis limits. If X is a matrix, X(1,:) defines the x-axis. If Y is a matrix, Y(:,1) defines the y-axis. When X and Y are matrices, they must be the same size as Z, in which case they specify a surface as surf does.

Code:

%plot using contour3 command%clc[x,y]=meshgrid([-3:0.25:3]);z=(-5)./(1+x.^2+y.^2);contour3(x,y,z,30)surface(x,y,z,'Edgecolor',[.001 1 1],'Facecolor',[1 1 1])grid on, xlabel('x--->'),ylabel('y--->'),zlabel('f(x,y)')colormap hot

Page 51: matlab (2)

Output:

Page 52: matlab (2)

EXPERIMENT NO.:23

Aim:Compute the following integral:

∫−1

1

∫0

2

(1−6 x2 y )dxdy

Tool: MATLAB 7.0.4

Syntax :q = dblquad(fun,xmin,xmax,ymin,ymax)

Description

q = dblquad(fun,xmin,xmax,ymin,ymax) calls the quad function to evaluate the double integral fun(x,y) over the rectangle xmin <= x <= xmax, ymin <= y <= ymax. fun is a function handle.

Code:

%Integration%f=@(x,y)(1-6*x.^2*y);

I=dblquad(f,0,2,-1,1)

Output:

I =

4.0000

>>

Page 53: matlab (2)

EXPERIMENT NO.:24

Aim:Compute the first order differential equation:

dydx

=x+ t, with initial condition x(0)=0.

Tool: MATLAB 7.0.4

Syntax :

r = dsolve('eq1,eq2,...', 'cond1,cond2,...', 'v')

Description:

dsolve('eq1,eq2,...', 'cond1,cond2,...', 'v') symbolically solves the ordinary differential equation(s) specified by eq1, eq2,... using v as the independent variable and the boundary and/or initial condition(s) specified by cond1,cond2,....

Code:

%first order differential equation%clc

y=dsolve('Dy=x+t','y(0)=0','x')

Output:

y = 1/2*x^2+t*x >>

Page 54: matlab (2)

EXPERIMENT NO.:25

Aim:Compute the following transcendental equation .

sin x=ex-5

Tool: MATLAB 7.0.4

Syntax :

g = solve(eq)

Description

Single Equation/Expression. The input to solve can be either symbolic expressions or strings. If eq is a symbolic expression (x^2-2*x+1) or a string that does not contain an equal sign ('x^2-2*x+1'), then solve(eq) solves the equation eq=0 for its default variable

Code:

%transcedental equation%clcf=solve('sin(x)-exp(x)+5')

Output:

f = 1.7878414746209143250134641264441 >>

Page 55: matlab (2)

EXPERIMENT NO.:26

Aim:

Compute the convolution x(t)=1

1+ t2 with itself.

Tool: MATLAB 7.0.4

Syntax :

w = conv(u,v)

Description

w = conv(u,v) convolves vectors u and v. Algebraically, convolution is the same operation as multiplying the polynomials whose coefficients are the elements of u and v

Code:

%Convolution%clc

t=0:10u=1./(1+t.^2);c=conv(u,u)

Output:

t =

0 1 2 3 4 5 6 7 8 9 10

Page 56: matlab (2)

c =

Columns 1 through 11

1.0000 1.0000 0.6500 0.4000 0.2576 0.1757 0.1260 0.0942 0.0727 0.0577 0.0468

Columns 12 through 21

0.0223 0.0105 0.0057 0.0033 0.0020 0.0013 0.0008 0.0005 0.0002 0.0001

>>

Page 57: matlab (2)

EXPERIMENT NO.:27

Aim: Compute the Cross correlation between the following two sequences.x(n)={1,2,-1,3}, h(n)={3,-2,1,4}

Tool: MATLAB 7.0.4

Syntax :

Code:Output:

Page 58: matlab (2)

EXPERIMENT NO.:28

Aim: Let x(n)=[1,2,-1,3]=u(n)-u(n-10). Decompose x(n) into even and odd components.

Tool: MATLAB 7.0.4

Syntax :

Code:

Output:

Page 59: matlab (2)

EXPERIMENT NO.:29

Aim: Let x(t)= e-1000|t|

a) Sample x(t) at fs=5000 samples/sec to obtain x(n)b) Fom the sample x(n), reconstruct x(t).

Tool: MATLAB 7.0.4

Syntax :Code:Output:

Page 60: matlab (2)

EXPERIMENT NO.:30

Aim: Determine the laplace transform of the following:

a) x(t) = te-at u(t)b) x(t) = [sin(at)+cos(bt)](u(t))

Tool: MATLAB 7.0.4

Syntax :syms arg1 arg2 ...

Description

syms arg1 arg2 ... is short-hand notation for arg1 = sym('arg1');arg2 = sym('arg2'); ...

laplace(F)

Description

L = laplace(F) is the Laplace transform of the scalar symbol F with default independent variable t. The default return is a function of s. The Laplace transform is applied to a function of t and returns a function of s

Code:

%Laplace 1%clcsyms t a s;

Page 61: matlab (2)

xt=t.*exp(-a.*t)xs=laplace(x,s)--------------------------------------------------%laplace 2%clcsyms t a b s;x=(sin(a*t)+cos(b*t));laplace(x,s)

Output:

a) xt =

t*exp(-a*t)

xs =

1/(s+a)^2 b)xt =

sin(a*t)+cos(b*t)

xs =

a/(s^2+a^2)+s/(s^2+b^2)

Page 62: matlab (2)

MATLABNational Institute of Technology

Kurukshetra

Anshuman111405

EC-5