LONER2
LONER2
LONER2
LAB MANUAL#4
Lab Task 1:
i) Use Cramer’s Rule to solve the linear system Ax = b if
Make a function which uses “disp” command to give answer of x1, x2.
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 (1)
MATLAB CODE:-
%18-EE-122
%SECTION B
%ALI
%TASK_1_PART_1
function []=cramers_rule()
A=[4 -2;3 -5];
B=[10;11];
A1=A;
A2=A;
A1(:,1)=B;
A2(:,2)=B;
x1=det(A1)/det(A);
x2=det(A2)/det(A);
fprintf('The values of x1 and x2 \n\n');
disp(x1);
disp(x2);
end
Output:-
Part (2)
MATLAB CODE:-
%18-EE-122
%SECTION B
%ALI
%TASK_1_PART_2
function []=universal()
A=input ('Enter the square matrix A of order 2\n');
B=input('Enter the matrix 5 of order 2*1\n');
1
M=A;
M(:,1)=B;
A1=det(M);
A(:,2)=B;
A2=det(A);
fprintf('Displaying the values of A1 and A2 using disp command\n')
disp(A1);
disp(A2);
fprintf('\n Displaying the values of A1 and A2 using fprint command\n');
fprintf('\n the value of A1=%d',A1);
fprintf('\n the value of A2=%d',A2);
end
Output:-
>> ALI
Enter the square matrix A of order 2
[5 6;9 3]
Enter the matrix 5 of order 2*1
[9;5]
Displaying the values of A1 and A2 using disp command
-3.0000
-56
Part(1):-
MATLAB CODE:-
%18-EE-122
%SECTION B
%ALI
2
%TASK_2_PART_1
function[] =plotting()
x=-1;0.1;1;
y=-1;0.1;1;
figure (1);
[x,y]=meshgrid(x,y);
z=y + x.*exp(-3*abs(y));
surf (x,y,z);
title('3D mesh plot');
figure(2);
contour(x,y,z);
title('3d contour plot');
end
Output:-
Part(2):-
MATLAB CODE:-
%18-EE-122
%SECTION B
%ALI
3
%TASK_2_PART_2
function[]=plotting()
x=input('ENTER THE RANGE OF X AND Y\n');
y=x;
choice=input('\n\n 1-PRESS 1 FOR 2D PLOT \n 2-Press 2 for 3d plot\n 3-
PRESS 3 FOR MESH PLOT\n');
if(choice==1)
figure(3);
plot(x,y);
title('2d plot');
xlabel('x');
ylabel('y');
else if (choice==2)
figure(3);
z=y + x.*exp(-3*abs(y));
plot3 (x,y,z);
title ('3d plot');
else if(choice==3)
figure(3);
[x,y]=meshgrid(x,y);
z=y + x.*exp(-3*abs(y));
surf(x,y,z);
title('3d mesh plot');
end
end
end
end
OUTPUT:-
4
5
6
Lab Task 3:-
1) Write a Function statistics which used “nested” functions to prompts user to
enter any rectangular matrix and calculates its i) Mean, ii) Median and iii)
standard deviation. When you run program, it should be of the form… >> statistics
Please enter the matrix: …. For Given Matrix the “mean is: ….” “median is: …. “
“standard deviation is: …”
2) Create a function in MathScript that convert a given temperature from degrees
Celsius to degrees Fahrenheit. Take 10 different values from user and display it in
three different ways….1) Display 2) fprintf 3) plot “ Challenge” can you print a
table of given data?
3) Write a function that prints out the binary equivalent of a number prompt from
user.
Part(1):-
MATLAB CODE:-
%18-EE-122
%SECTION B
%ALI
%TASK_3_PART_1
function [MEAN,MEDIAN,STD]=mms()
mat=input('enter the matrix\n');
MEAN=mean(mat);
MEDIAN=median(mat);
STD=std(mat);
7
end
Output:-
Part(2):-
MATLAB CODE:-
%18-EE-122
%SECTION B
%ALI
%TASK_3_PART_2
function []=temp()
C=input('ENTER 10 VALUES IN DEGREE CELSIUS IN A VECTOR\n');
F=1.8.*C+32;
disp(['EQUIVALENT VALUE IN FAHRENHEIT ',num2str(F)]);
plot(C,F);
title('celsius vs fahrenheit');
xlabel('celsius');
ylabel('fahrenheit');
celsius=C';
fahrenheit=F';
table(celsius,fahrenheit)
end
Ouput:-
>> ALI
ENTER 10 VALUES IN DEGREE CELSIUS IN A VECTOR
[10 20 30 40 50 60 69 88 95 99]
EQUIVALENT VALUE IN FAHRENHEIT 50 68 86 104 122
140 156.2 190.4 203 210.2
ans =
10×2 table
celsius fahrenheit
_______ __________
8
10 50
20 68
30 86
40 104
50 122
60 140
69 156.2
88 190.4
95 203
99 210.2
>>
Part(3);-
MATLAB CODE:-
%18-EE-122
%SECTION B
%ALI
%TASK_3_PART_3
function []=binary_eq()
num=input ('ENTER THE NO. TO BE CONVERTED INTO BINARY FORM\n');
bin=dec2bin(num);
disp('The binary equivalent of entered no. is :- ');
disp(bin);
end
9
Output:-
>> ALI
ENTER THE NO. TO BE CONVERTED INTO BINARY FORM
45
The binary equivalent of entered no. is :-
101101
>>
10