Matlab Assignment JK Institute

499 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
499
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Matlab Assignment JK Institute

  1. 1. SOL 1 : clear clc F=0; format short F = input('Enter temperature in Fahrenheit : '); K = (((F-32)*5/9) + 273); %The expression converts the temperature fprintf('Temperature in Kelvin would be : %fn',K) OUTPUT : Enter temperature in Fahrenheit : 32 Temperature in Kelvin would be : 273.000000 >>
  2. 2. SOL 2 : clear clc format compact format short A(1)=0; A(2)=1; n= input ('Enter the value of n (n>2) : '); i=3; while i<=n A(i)= A(i-1)+ A(i-2); i=i+1; end disp(A) OUTPUT : Enter the value of n (n>2) : 10 0 1 1 2 3 5 8 13 21 34 >>
  3. 3. SOL 3 : clear clc y=input('Enter the year (in YYYY format) : ','s'); m=input('Enter the numeric value for month (in MM format) : ','s'); d=input('Enter the date (in DD format) : ','s'); date=strcat(m,'/',d,'/',y); [N,S]=weekday(date,'long'); fprintf ('The day is %s n',S) OUT PUT : Enter the year (in YYYY format) : 1990 Enter the numeric value for month (in MM format) : 02 Enter the date (in DD format) : 10 The day is Saturday >>
  4. 4. SOL 4 : clear clc x(1)=0; i=1; n=input(' Enter no of elements in the array : '); disp(' Now enter array elements one by one') while i<=n x(i)=input(''); i=i+1; end m=mean(x); s=std(x); disp(' You entered the array as :') disp(x) fprintf('n The mean of the elements is : %fn The Standard Deviation of the elements is : %fn',m,s) OUTPUT : Enter no of elements in the array : 4 Now enter array elements one by one 2 3 5 9 You entered the array as : 2 3 5 9 The mean of the elements is : 4.750000 The Standard Deviation of the elements is : 3.095696 >>
  5. 5. SOL 5 (i) : clear clc format short i=1; for t=-6*pi:pi/10:6*pi x(i)=t; y(i)=sin(t); if y(i)<=0 y(i)=0; end i=i+1; end R(:,1)=x; R(:,2)=y; disp(' t f(t)') disp(R) SOL 5 (ii) : clear clc format short x=-6*pi:pi/10:6*pi; y= sin(x); w= y>0; R(:,1)=x; R(:,2)=w(1,:) .* y(1,:); disp(' t f(t)') disp(R)
  6. 6. OUTPUT : t f(t) -18.8496 0.0000 -18.5354 0.3090 -18.2212 0.5878 -17.9071 0.8090 -17.5929 0.9511 -17.2788 1.0000 -16.9646 0.9511 -16.6504 0.8090 -16.3363 0.5878 -16.0221 0.3090 -15.7080 0 -15.3938 0 -15.0796 0 -14.7655 0 -14.4513 0 -14.1372 0 -13.8230 0 -13.5088 0 -13.1947 0 -12.8805 0 -12.5664 0.0000 -12.2522 0.3090 -11.9381 0.5878 -11.6239 0.8090 -11.3097 0.9511 -10.9956 1.0000 -10.6814 0.9511 -10.3673 0.8090 -10.0531 0.5878 -9.7389 0.3090 -9.4248 0 -9.1106 0 -8.7965 0 -8.4823 0 -8.1681 0 -7.8540 0 -7.5398 0 -7.2257 0 -6.9115 0 -6.5973 0 -6.2832 0.0000 -5.9690 0.3090 -5.6549 0.5878 -5.3407 0.8090 -5.0265 0.9511 -4.7124 1.0000
  7. 7. -4.3982 0.9511 -4.0841 0.8090 -3.7699 0.5878 -3.4558 0.3090 -3.1416 0 -2.8274 0 -2.5133 0 -2.1991 0 -1.8850 0 -1.5708 0 -1.2566 0 -0.9425 0 -0.6283 0 -0.3142 0 0 0 0.3142 0.3090 0.6283 0.5878 0.9425 0.8090 1.2566 0.9511 1.5708 1.0000 1.8850 0.9511 2.1991 0.8090 2.5133 0.5878 2.8274 0.3090 3.1416 0.0000 3.4558 0 3.7699 0 4.0841 0 4.3982 0 4.7124 0 5.0265 0 5.3407 0 5.6549 0 5.9690 0 6.2832 0 6.5973 0.3090 6.9115 0.5878 7.2257 0.8090 7.5398 0.9511 7.8540 1.0000 8.1681 0.9511 8.4823 0.8090 8.7965 0.5878 9.1106 0.3090 9.4248 0.0000 9.7389 0 10.0531 0 10.3673 0 10.6814 0 10.9956 0 11.3097 0
  8. 8. 11.6239 0 11.9381 0 12.2522 0 12.5664 0 12.8805 0.3090 13.1947 0.5878 13.5088 0.8090 13.8230 0.9511 14.1372 1.0000 14.4513 0.9511 14.7655 0.8090 15.0796 0.5878 15.3938 0.3090 15.7080 0.0000 16.0221 0 16.3363 0 16.6504 0 16.9646 0 17.2788 0 17.5929 0 17.9071 0 18.2212 0 18.5354 0 18.8496 0 >>
  9. 9. SOL 6 : clear clc i=1; for k = -9:0.5:9 T(i)=k; if k<0 Y(i) = (-3*(k^2)) + 5; else Y(i) = (3*(k^2)) + 5; end i=i+1; end R(:,1) = T; R(:,2) = Y; disp(' T Y(T)') disp(R) OUTPUT : T Y(T) -9.0000 -238.0000 -8.5000 -211.7500 -8.0000 -187.0000 -7.5000 -163.7500 -7.0000 -142.0000 -6.5000 -121.7500 -6.0000 -103.0000 -5.5000 -85.7500 -5.0000 -70.0000 -4.5000 -55.7500
  10. 10. -4.0000 -43.0000 -3.5000 -31.7500 -3.0000 -22.0000 -2.5000 -13.7500 -2.0000 -7.0000 -1.5000 -1.7500 -1.0000 2.0000 -0.5000 4.2500 0 5.0000 0.5000 5.7500 1.0000 8.0000 1.5000 11.7500 2.0000 17.0000 2.5000 23.7500 3.0000 32.0000 3.5000 41.7500 4.0000 53.0000 4.5000 65.7500 5.0000 80.0000 5.5000 95.7500 6.0000 113.0000 6.5000 131.7500 7.0000 152.0000 7.5000 173.7500 8.0000 197.0000 8.5000 221.7500 9.0000 248.0000 >>
  11. 11. SOL 7(i) : clear clc format short i=1; for x=-1:0.1:3 X(i)=x; Y(i)=(x^2)-(3*x)+2; i=i+1; end R(:,1)=X; R(:,2)=Y; disp(' X Y(x)') disp(R) plot(X,Y,'--r','linewidth',3) xlabel('x-axis'); ylabel('y-axis'); SOL 7(ii) : clear clc format short X=-1:0.1:3; Y= (X.^2)-(3.*X)+2; R(:,1)=X; R(:,2)=Y; disp(' X Y(x)') disp(R) plot(X,Y,'--r','linewidth',3) xlabel('x-axis'); ylabel('y-axis');
  12. 12. OUTPUT : X Y(x) -1.0000 6.0000 -0.9000 5.5100 -0.8000 5.0400 -0.7000 4.5900 -0.6000 4.1600 -0.5000 3.7500 -0.4000 3.3600 -0.3000 2.9900 -0.2000 2.6400 -0.1000 2.3100 0 2.0000 0.1000 1.7100 0.2000 1.4400 0.3000 1.1900 0.4000 0.9600 0.5000 0.7500 0.6000 0.5600 0.7000 0.3900 0.8000 0.2400 0.9000 0.1100 1.0000 0 1.1000 -0.0900 1.2000 -0.1600 1.3000 -0.2100 1.4000 -0.2400 1.5000 -0.2500 1.6000 -0.2400 1.7000 -0.2100 1.8000 -0.1600 1.9000 -0.0900 2.0000 0 2.1000 0.1100 2.2000 0.2400 2.3000 0.3900 2.4000 0.5600 2.5000 0.7500 2.6000 0.9600 2.7000 1.1900 2.8000 1.4400 2.9000 1.7100 3.0000 2.0000 >>
  13. 13. SOL 8 : function f = fact(x) f=1; if x<0 disp('Entered value is negative') return end if x~=ceil(x) | x~=floor(x) disp('Entered value is not an integer') return end for k = x:-1:0 if k==0 k=1; end f = f * k; end OUTPUT : >> fact(-1) Entered value is negative >> fact(12.90) Entered value is not an integer >> fact(7) ans = 5040 >>
  14. 14. SOL 9 : Clear clc x=0; while x < 1 x = input('Enter value for x : '); if x >= 1 disp('The entered value is out of domain of function so returning back . . .') return end y = log(1/(1-x)); disp(y) end OUTPUT : Enter value for x : -1 -0.6931 Enter value for x : 0 0 Enter value for x : -12 -2.5649 Enter value for x : 1 The entered value is out of domain of function so returning back... >>
  15. 15. SOL 10: clear clc F=[75 100 125]; T = (5/9*(F-32))+273; Io=0.000002; q= 1.6 * (10^(-19)); k= 1.38 * (10^(-23)); Vd= -1.0 : 0.1 : 10.6; qvd= q * Vd; KT1= k * T(1); KT2= k * T(2); KT3= k * T(3); I1=Io*(exp(qvd/KT1)-1); I2=Io*(exp(qvd/KT2)-1); I3=Io*(exp(qvd/KT3)-1); plot(Vd,I1,'--b',Vd,I2,'-r',Vd,I3,':k'); legend('75F','100F','125F'); xlabel('Vd (Volts)'); ylabel('I (in Amp)');
  16. 16. OUTPUT : >>
  17. 17. SOL 11 : clear clc ln1=input('Enter the no of elements in the I sequence : '); xn1(1,:)=zeros(1,ln1); disp('Enter the values for the elements ') for r=1:ln1 xn1(r)=input(''); end ln2=input('Enter the no of elements in the II sequence : '); xn2(1,:)=zeros(1,ln2); disp('Enter the values for the elements ') for r=1:ln2 xn2(r)=input(''); end xk2=zeros(1,ln2); xk1=zeros(1,ln1); %To find the DFT of the sequence for k=0:ln1-1 for n=0:ln1-1 xk1(k+1)=xk1(k+1)+(xn1(n+1)*exp((1*2*pi*k*n / ln1)*1i)); end end for k=0:ln2 - 1 for n=0:ln2 - 1 xk2(k+1)=xk2(k+1)+(xn2(n+1)*exp((1*2*pi*k*n / ln2)*1i)); end end disp('DFT of I sequence') disp(xk1) disp('DFT of II sequence') disp(xk2)
  18. 18. OUTPUT : Enter the no of elements in the I sequence : 2 Enter the values for the elements 1 3 Enter the no of elements in the II sequence : 3 Enter the values for the elements 2 4 6 DFT of I sequence 4.0000 -2.0000 + 0.0000i DFT of II sequence 12.0000 -3.0000 - 1.7321i -3.0000 + 1.7321i >>

×