Sns Lab 4
Sns Lab 4
Task : 01
ii) Now make a universal function which prompts user to enter matrix A & B
of 2x2 and 2x1 respectively from user and returns A1 and A2 using both
“disp” & “ fprintf ” command. (Hint: A1 is the determinant of A but 1st column
replaced by matrix “b”). what is the difference b/w these two commands?
Part(i)
CODE:
function my_cramerRule
A=[4 -2;3 -5];
B=[10;11];
Ax=[B A(:,2)];
Ay=[A(:,1) B];
dA=det(A);
dAx=det(Ax);
dAy=det(Ay);
x1=dAx/dA;
x2=dAy/dA;
disp(['The value of x1 is ',num2str(x1)]);
disp(['The value of x2 is ',num2str(x2)]);
end
OUTPUT:
1
Part(ii)
CODE:
function my_func
A=input('Enter 2*2 Matric ');
B=input('Enter 2*1 Matric ');
A2=[B A(:,2)];
A1=[A(:,1) B];
a1=det(A1);
a2=det(A2);
disp(['A1 is ',num2str(a1),' A2 is ',num2str(a2)]);
fprintf('A1 using fprintf is: %s\n',num2str(a1));
fprintf('A2 using fprintf is: %s\n',num2str(a2));
end
OUTPUT:
Difference:
The difference to disp is that it doesn't display the value of variable unless you specify
format string if you tend to display the value of a variable, we get an error .we need to specify
the format string. For example, the format string is 'The value of a is %d\n'. If you are talking about
writing data to text file, the format is:
fprintf(fileID,formatSpec,A1,...,An) applies the formatSpec to all elements of arrays A1,...An in
column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the
call to fopen.
2
Task : 02
Example : 1 10 25 1 2 3
123 233 255 => 5 8 9
172 201 54 6 7 4
Part(i)
CODE:
x = -1: 0.1: 1;
y = -1: 0.1: 1;
[X Y]=meshgrid(x,y);
Z=Y+X*exp(-3*abs(Y));
surface(X,Y,Z);
xlabel('X');
ylabel('Y');
zlabel('Z');
figure
contour(X,Y,Z);
xlabel('X');
ylabel('Y');
zlabel('Z');
mygraph(1);
figure;
mygraph(2);
M=[1 10 15;123 233 255;172 201 54];
m=reshape(M',[1,9])
[S I]=sort(m);
[S i]=sort(S);
S(I)=i;
S=reshape(S',[3,3]);
S=S';
3
OUTPUT:
Contour Plot:
4
Part(ii)
CODE:
function mygraph
disp('Option 1 For 2D')
disp('Option 2 For 3D')
disp('Option 3 For meshplot')
X=input('Enter your choice: ');
if (X==1)
A=input('Enter X ');
B=input('Enter Y ');
figure(1);
plot(A,B,'*');
xlabel('X');
ylabel('Y');
end
if (X==2)
A=input('Enter X ');
B=input('Enter Y ');
C=input('Enter Z ');
figure(2);
plot3(A,B,C,'*');
xlabel('X');
ylabel('Y');
zlabel('Z');
end
if (X==3)
A=input('Enter X ');
B=input('Enter Y ');
[X,Y] = meshgrid(A:.5:B);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
mesh(X,Y,Z)
xlabel('X');
ylabel('Y');
zlabel('Z');
end
if (X~=2)&(X~=1)&(X~=3)
disp('Wrong Argument Use 1 For 2D and 2 for 3D')
end
end
5
OUTPUT:
X=1:
2-D:
6
X=2:
3-D:
7
X=3:
Mesh Plot:
8
Part(iii)
CODE:
function colormap
X=input('input a matrix with values ranging from 1 to 255' )
disp(X);
[B I]=sort(X);
[m n]=size(X);
[s i]=sort(I);
y=reshape(i,[m,n]);
disp('Y=');
disp(y);
end
OUTPUT:
9
Task : 03
function[]= my_func(X)
X=input ('Please input a rectangular matrix: ');
MEAN=mean(X);
MEDIAN=median(X);
STANDARD_DEVIATION=std(X);
disp('For Given Matrix the mean is: ');
disp(MEAN);
disp('For Given Matrix the median is: ');
disp(MEDIAN);
disp('For Given Matrix standard deviation is: ')
disp(STANDARD_DEVIATION);
end
OUTPUT:
10
Part(ii)
CODE:
function[]= my_func(C)
C=input('Enter 10 Value in degree Celcius ')
F=1.8.*C+32;
disp('Equalent value in Fahrenheit displaying using "disp" ');
disp(num2str(F));
fprintf('Equalent value in Fahrenheit displaying using "fprintf"\n');
fprintf('[');
fprintf('%g ', F);
fprintf(']\n');
plot(C,F);
title('celcius vs Farenheit');
xlabel('Celsius');
ylabel('Fahrenheit');
Celsius=C';
Fahrenheit=F';
table(Celsius,Fahrenheit)
end
OUTPUT:
11
Part(iii)
CODE:
function [Binary]=my_func(Decimal)
Decimal=input('enter a number in decimal');
Binary=dec2bin(Decimal);
fprintf('binary equivalent of the entered number is ',Binary);
disp(num2str(Binary));
end
OUTPUT:
12