Your SlideShare is downloading. ×

Matlab practical program

3,704

Published on

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

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

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,704
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
514
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. %****************************************************% Title : Basic Arithmatic Operations%****************************************************%MATLAB ProgramX=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 ProgramA = 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
  • 2. %****************************************************% Title : Code Conversion%****************************************************%MATLAB Program% Decimal to Binary ConversionX= 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 conversionbin2dec(1111); % Command for binary to decimal conversionH = 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);%***************************************************** X v/s Y 10 9 8 7 6 Y Axis 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 X Axis
  • 3. %****************************************************% Title : 3D Graph%****************************************************%MATLAB Programt = 0:pi/50:10*pi;plot3(sin(t),cos(t),t)xlabel(sin(t))ylabel(cos(t))zlabel(t)grid on;%***************************************************** 40 30 20 t 10 0 1 0.5 1 0 0.5 0 -0.5 -0.5 cos(t) -1 -1 sin(t)
  • 4. %****************************************************% Title : 2D Pie chart%****************************************************%MATLAB Programt = 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 Programx = [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);%*****************************************************
  • 5. %****************************************************% Title : Generation of Sine Wave%****************************************************%MATLAB ProgramA = 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)%*****************************************************
  • 6. %****************************************************% Title : Generation of Cosine Wave%****************************************************%MATLAB Programa=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);%*****************************************************
  • 7. %****************************************************% Title : Generation of Ramp Signal%****************************************************%MATLAB Programn1=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);%*****************************************************
  • 8. %****************************************************% Title : Generation of Exponential Signal%****************************************************%MATLAB Programn2=input(Enter the No. of sequence N= );t=0:n2;y2=exp(t); % Expressrion for Exponentialsubplot(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);%*****************************************************
  • 9. %****************************************************% Title : Generation of Unit Impulse Signal%****************************************************%MATLAB Programt=-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);%*****************************************************
  • 10. %****************************************************% Title : Generation of Unit Step Signal%****************************************************%MATLAB Programn=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);%*****************************************************
  • 11. %****************************************************% 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); Squarewave Square wave 1 1 0.5 0.5 Amplitude Amplitude 0 0 -0.5 -0.5 -1 -1 0 50 100 0 50 100 Time Time
  • 12. %****************************************************% 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); %*****************************************************
  • 13. %****************************************************% Title : Frequency Modulation & Demodulation%****************************************************%MATLAB Programfc=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);%*****************************************************
  • 14. %****************************************************% Title : Amplitude Shift Keying%****************************************************%MATLAB Program%ASKfc=5; %Carrier FrequencyData=[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%*****************************************************
  • 15. %****************************************************% Title : Frequency Shift Keying%****************************************************%MATLAB Programfc1=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%*****************************************************
  • 16. 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 orequal 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 mustcontain 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 logical0 (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

×