matlab practical program

19
%**************************************************** % Title : Basic Arithmatic Operations %**************************************************** %MATLAB Program X=input('Enter the first numberX='); Y=input('Enter the second numberY='); disp('Enter "Add" For Output'); disp('Enter "Sub" For Output'); disp('Enter "Mul" For Output'); disp('Enter "Div" For Output'); Add=X+Y; Sub=X-Y; Mul=X*Y; Div=X/Y; %***************************************************** %**************************************************** % Title : Martix Operation %**************************************************** %MATLAB Program A = input('Enter the First Matrix A = [] >>>>>> '); B = input('Enter the Second Matrix B = [] >>>>>> '); disp('Enter ADD to Additioon of two matrix'); disp('Enter SUB to Additioon of two matrix'); disp('Enter MUL to Additioon of two matrix'); disp('Enter DIV to Additioon of two matrix'); ADD=A+B; SUB=A-B; MUL=A.*B; DIV=A./B; %***************************************************** EX: >> Enter the First Matrix A = [] >>>>>> [1 2 3]; >> Enter the Second Matrix B = [] >>>>>> [4 5 6];

Upload: esys-india-malegaon

Post on 12-May-2015

3.484 views

Category:

Education


1 download

DESCRIPTION

Workshop on MATLAB at the Department of Computer Science, Poona College, Pune

TRANSCRIPT

Page 1: Matlab practical program

%****************************************************% Title : Basic Arithmatic Operations%**************************************************** %MATLAB Program X=input('Enter the first numberX=');Y=input('Enter the second numberY=');disp('Enter "Add" For Output');disp('Enter "Sub" For Output');disp('Enter "Mul" For Output');disp('Enter "Div" For Output');Add=X+Y;Sub=X-Y;Mul=X*Y;Div=X/Y; %*****************************************************

%****************************************************% Title : Martix Operation%**************************************************** %MATLAB Program A = input('Enter the First Matrix A = [] >>>>>> ');B = input('Enter the Second Matrix B = [] >>>>>> ');disp('Enter ADD to Additioon of two matrix');disp('Enter SUB to Additioon of two matrix');disp('Enter MUL to Additioon of two matrix');disp('Enter DIV to Additioon of two matrix'); ADD=A+B;SUB=A-B;MUL=A.*B;DIV=A./B; %***************************************************** EX: >> Enter the First Matrix A = [] >>>>>> [1 2 3];

>> Enter the Second Matrix B = [] >>>>>> [4 5 6];>> Enter ADD to Additioon of two matrix>> Enter SUB to Additioon of two matrix>> Enter MUL to Additioon of two matrix>> Enter DIV to Additioon of two matrix

%****************************************************

Page 2: Matlab practical program

% Title : Code Conversion%****************************************************%MATLAB Program% Decimal to Binary Conversion X= input('Enter the any Decimal Number X= ');disp('Enter B for Binary output ');disp('Enter H for Hex output '); B = dec2bin(X); % Command for decimal to binary conversion bin2dec('1111'); % Command for binary to decimal conversion H = dec2hex(X); % Command for decimal to hex conversion %*************************************************************************

%****************************************************% Title : 2D Graph %**************************************************** %MATLAB Program

A=input('Enter the first numberA='); B=input('Enter the first numberB='); C=input('Enter the first numberC='); X= A:B:C; Y= A:B:C; plot (X,Y); grid on; Xlabel('X Axis'); Ylabel('Y Axis'); title('X v/s Y'); %*****************************************************

0 1 2 3 4 5 6 7 8 9 100

1

2

3

4

5

6

7

8

9

10

X Axis

Y A

xis

X v/s Y

Page 3: Matlab practical program

%****************************************************% Title : 3D Graph %**************************************************** %MATLAB Program t = 0:pi/50:10*pi;plot3(sin(t),cos(t),t)xlabel('sin(t)')ylabel('cos(t)')zlabel('t')grid on; %*****************************************************

-1-0.5

00.5

1

-1

-0.5

0

0.5

10

10

20

30

40

sin(t)cos(t)

t

Page 4: Matlab practical program

%****************************************************% Title : 2D Pie chart %**************************************************** %MATLAB Program t = 0:pi/50:10*pi;plot3(sin(t),cos(t),t)xlabel('sin(t)')ylabel('cos(t)')zlabel('t')grid on; %*****************************************************

%****************************************************% Title : 3D Pie chart %**************************************************** %MATLAB Program x = [1 3 0.5 2.5 2]; % Create a 3D pie chart using the pie3 functionfigure;explode = [0 1 0 0 0];pie3(x, explode); %*****************************************************

Page 5: Matlab practical program

%****************************************************% Title : Generation of Sine Wave%**************************************************** %MATLAB Program A = input('Enter the Value of Amplitude A = ');S = input('Enter the Value of Sample S = ');x=linspace(0,(2*pi),S);y=A*sin(x); subplot(2,2,1);plot(x,y,'*-b');grid on;xlabel('Time');ylabel('Amplitude');title('Sine Wave') subplot(2,2,2);stem(x,y,'or');grid on;xlabel('Time');ylabel('Amplitude');title('Sine Wave') %*****************************************************

Page 6: Matlab practical program

%****************************************************% Title : Generation of Cosine Wave%**************************************************** %MATLAB Program a=input('Enter the value of amplitude a= ');S = input('Enter the Value of Sample S = ');x=linspace(0,(2*pi),S); y=a*cos(x);subplot(2,2,1);plot(x,y);grid on;xlabel('x=0:2/pi');Ylabel('cos of x');title('COSINE WAVE'); subplot(2,2,2);stem(x,y, '*r');grid on;xlabel('x=0:2/pi');Ylabel('cos of x');title('COSINE WAVE'); %*****************************************************

Page 7: Matlab practical program

%****************************************************% Title : Generation of Ramp Signal %**************************************************** %MATLAB Program n1=input('Enter the No of sequence = ');t=0:n1;subplot(2,2,1);stem(t,t);Xlabel('sequence');Ylabel('Amplitude');title('Ramp Signal'); subplot(2,2,2);plot(t,t);Xlabel('sequence');Ylabel('Amplitude');title('Ramp Signal');%*****************************************************

Page 8: Matlab practical program

%****************************************************% Title : Generation of Exponential Signal%**************************************************** %MATLAB Program n2=input('Enter the No. of sequence N= ');t=0:n2;y2=exp(t); % Expressrion for Exponential subplot(2,2,1); % Comamd to plot Continuous Signalplot(t,y2,'*-r');grid on;Xlabel('sequence');Ylabel('Amplitude');title(' Exponential Signal'); subplot(2,2,2); %% Comamd to plot Continuous Signalstem(t,y2,'ob');grid on;Xlabel('sequence');Ylabel('Amplitude');title(' Exponential Signal'); %*****************************************************

Page 9: Matlab practical program

%****************************************************% Title : Generation of Unit Impulse Signal%**************************************************** %MATLAB Program t=-2:1:2;y=[zeros(1,2),ones(1,1),zeros(1,2)]; % Expressionsubplot(2,2,1);stem(t,y); xlabel('time');ylabel('Amplitude');title('unit impulse signal'); subplot (2,2,2);plot(t,y);xlabel('time');ylabel('Amplitude');title('unit impulse signal');%*****************************************************

Page 10: Matlab practical program

%****************************************************% Title : Generation of Unit Step Signal%**************************************************** %MATLAB Program n=input('Enter the value of sequence= ');t=0.1:n-1;y1=ones(1,n-1);subplot(2,2,1);plot(t,y1,'*r');xlabel('No of sequence');ylabel('Amplitude');title('Unit step sequence'); subplot(2,2,2);stem(t,y1,'*r');xlabel('No of sequence');ylabel('Amplitude');title('Unit step sequence'); %*****************************************************

Page 11: Matlab practical program

%****************************************************% Title : Generation of Square Wave%****************************************************A=input('Enter the Initial value of time A = ');B=input('Enter the Final value of time B = ');t=A:B;f=input('Enter the value of input frquencyin Hz f= ');d=input('Enter the value of duty cycle d = ');y=square(2*pi*f*t/100,d);subplot(2,2,1);stem(t,y,'*r')xlabel('Time');ylabel('Amplitude');title('Squarewave');subplot(2,2,2);plot(t,y);xlabel('Time');ylabel('Amplitude');title('Square wave');

0 50 100-1

-0.5

0

0.5

1

Time

Am

plitu

de

Squarewave

0 50 100-1

-0.5

0

0.5

1

Time

Am

plitu

de

Square wave

Page 12: Matlab practical program

%****************************************************% Title : Amplitude Modulation & Demodulation%****************************************************t=0:0.01:2;Ec=10*cos(2*pi*50*t); %fc=25Em=(1)*cos(2*pi*5*t); %fm=2subplot(4,1,1);plot(t,Ec);xlabel('carrier frequency');ylabel('amplitude');title('Carrier Signal'); subplot (4,1,2);plot(t,Em);xlabel('modulating frequency');ylabel('amlitude');title('Modulating Signal'); ft=Em.*Ec;Eam=ft+Em;subplot(4,1,3);plot(t,Eam);xlabel('AM modulating Signal');ylabel('amlitude');title('AM Signal'); hold on;Ed=Eam-ft;subplot(4,1,4);plot(t,Ed);xlabel('modulating frequency');ylabel('amlitude');title('AM Demodulating Signal'); %*****************************************************

Page 13: Matlab practical program

%****************************************************% Title : Frequency Modulation & Demodulation%**************************************************** %MATLAB Program fc=10000; %carrier frequencyfs=100000; %sampling frequencyf=600; %tone modulationt=0:1/fs:((2/f)-(1/fs));x=cos(2*pi*f*t);kf=2*pi*(fc/fs)*(1/max(x));kf=kf*(f/fc);opt=10*kf;y=modulate(x,fc,fs,'fm',opt);subplot(4,1,1);plot(x); title('modulating signal'); subplot(4,1,2);plot(y); title('Frequency modulated signal')subplot(4,1,3);plot(x); title('Demodulated signal'); %*****************************************************

Page 14: Matlab practical program

%****************************************************% Title : Amplitude Shift Keying%****************************************************%MATLAB Program %ASK

fc=5; %Carrier Frequency Data=[1 1 0 1 1 0 1 1];n=1;while n<= length(Data)if Data(n)==0 %if bit to be transmitted is '0' ,then send 1volt amplitude carrier t=(n-1)*0.2:0.2/100:n*0.2; ASK=(1)*sin(2*pi*fc*t); plot(t,ASK); grid on; hold on;else %if bit to be transmitted is '0' ,then send 1volt amplitude carrier t=(n-1)*0.2:0.2/100:n*0.2; ASK=(2)*sin(2*pi*fc*t); plot(t,ASK); grid on; hold on; end n=n+1;end%*****************************************************

Page 15: Matlab practical program

%****************************************************% Title : Frequency Shift Keying

%**************************************************** %MATLAB Program fc1=100;fc=5;g=[1 1 0 1 0 0 1 1 1 0];n=1;while n<=length(g) if g(n)==0 t=(n-1)*0.2:0.2/100:n*0.2; carrier=sin(2*pi*fc*t); plot(t,carrier); grid on; hold on; else t=(n-1)*0.2:0.2/100:n*0.2; carrier=(1)*sin(2*pi*fc1*t); plot(t,carrier); grid on; hold on; end n=n+1;end %*****************************************************

Page 16: Matlab practical program

1. Prime Number in MATLAB: Syntax: p = primes(n) …………Generate list of prime numbersDescription: p = primes(n) returns a row vector of the prime numbers less than or equal to n. A prime number is one that has no factors other than 1 and itself.Examples:>> p = primes(37)

p = 2 3 5 7 11 13 17 19 23 29 31 37

2. factor: Syntax: f = factor(n) …. Prime factorsDescription: f = factor(n) returns a row vector containing the prime factors of n.Examples:>> f = factor(123)

f = 3 41

3. Isprime:Syntax: TF = isprime(A) ……. Array elements that are prime numbersDescription: TF = isprime(A) returns an array the same size as A containing logical 1 (true) for the elements of A which are prime, and logical 0 (false) otherwise. A must contain only positive integers.Examples:>> c = [2 3 0 6 10]

c = 2 3 0 6 10>> isprime(c)

ans = 1 1 0 0 0

4. Ischar: Syntax: tf = ischar(A) ….Determine whether item is character arrayDescription: tf = ischar(A) returns logical 1 (true) if A is a character array and logical 0 (false) otherwise.Examples: Given the following cell array,>> C{1,1} = magic(3); % double array>> C{1,2} = 'John Doe'; % char array>> C{1,3} = 2 + 4i % complex double>> C =

[3x3 double] 'John Doe' [2.0000+ 4.0000i]ischar shows that only C{1,2} is a character array.>> for k = 1:3>> x(k) = ischar(C{1,k});>> end

x = 0 1 0