Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Matlab practical program

4,019

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
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
4,019
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
573
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Transcript

• 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