You are on page 1of 14

University of Engineering and Technology, Taxila

Department of Electrical Engineering


Signals and Systems

Lab Manual 4

INTRODUCTION TO PROGRAMMING IN MATLAB

Name of Student: ZARAFSHAN ABBAS

Roll number: 18-EE-36

Rubrics Marks Obtained


Data Analysis and Calculations 5
Result Interpretation 5
Total 10

Prepared By Engr. Hammad Haider

1
LAB NO 04

Task : 01

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(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:

2
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.
Task : 02

i) Evaluate a function of two variables z = y + x. e−3|y| over the range x


= −1: 0.1: 1, y = −1: 0.1: 1. Plot a 3-D mesh surface and a contour plot of
this function.
ii) Make a function that takes function value as an input from user and
gives its 2D, 3D or mesh plot depending upon the requirement of user.
iii)Z is an N*M matrix which contains integers from 1 to 255(an
image,perhaps).There are only K unique integers in the matrix
(K<255).Write a function that maps the integers in the matrix from the
range (1,255) to (1,K) while preserving the order of the numbers (see
the example below).This operation is similar to compressing the
colormap of an image.

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:

3-D Mesh Surface:

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');
en
d
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');
en
d
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');
en
d
if (X~=2)&(X~=1)&(X~=3)
disp('Wrong Argument Use 1 For 2D and 2 for 3D')
end
end
OUTPUT:

X=1:

2- D:
X=2:

3- D:
X=3:

Mesh Plot:
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:
Task : 03

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(i)
CODE:

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:
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:
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:
University of Engineering and Technology, Taxila
Department of Electrical Engineering
Signals and Systems

Lab Manual 5

INTRODUCTION TO PROGRAMMING AND LOOPS IN MATLAB

Name of Student: ZARAFSHAN ABBAS

Roll number: 18-EE-36

Rubrics Marks Obtained


Data Analysis and Calculations 5
Result Interpretation 5
Total 10

Prepared By Engr. Hammad Haider

You might also like