Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

LAB 1:

Exercise 1: Evaluate following Matlab code.


a) >> 2^(1+2)/3
ans =

2.6667

b) >> 3*4-5^2*2-3
ans = -41
c) >> 3*(3*4-2*5^2-3)
ans = -123
d) >> (2/3^2*5)*(3-4^3)^2
ans = 4.1344e+03
Exercise 2 :
A) Generate three row vectors V1,V2,V3 and Compute the following .
>>V1=[1 3 sqrt(5)];
>>V2=[7 5];
>>V3=[3 4 5];
a) >> V4=3*V1
V4 =

3.0000

9.0000

6.7082

b) >> V5=2*V1-3*V3
V5 = -7.0000 -6.0000 -10.5279
c) V6=V1+V2
Matrix dimensions must agree.
B) Generate row vector V7 by concatenating existing vectors V3 & V1 using
following expression:
V7=[2*V3,-V1]
V7 =

6.0000

8.0000 10.0000 -1.0000 -3.0000 -2.2361

C) Generate row vector having seven elements , extract alternate entries:


V9=linspace(0,7,7)
V9 =

1.1667

2.3333

3.5000

>> V9(1:2:7)
ans =

2.3333

4.6667

7.0000

4.6667

5.8333

7.0000

D) Generate column vector by transposing row vector


V10=transpose(V9)
V10 =
0
1.1667
2.3333
3.5000
4.6667
5.8333
7.0000
E) Compute y=mx+c m=0.5 , c=-2 , x= 0,1.5,3,4,5,7,9,10
Create vector t with ten elements 1to 10
m=0.5
m=

0.5000

>> c=-2
c=

-2

>> x=[0,1.5,3,4,5,7,9,10]
x=

1.5000

3.0000

4.0000

5.0000

7.0000

9.0000 10.0000

0.5000

1.5000

2.5000

y=m*x+c
y = -2.0000 -1.2500 -0.5000

3.0000

F) t=[1:10]
t=

10

>> x=t.*sin(t)
x = 0.8415 1.8186
3.7091 -5.4402

0.4234 -3.0272 -4.7946 -1.6765

4.5989

7.9149

>> y=(t-1)./(t+1)
y=
0.8000

0 0.3333
0.8182

0.5000

>> z=(sin(t.^2))./(t.^2)

0.6000

0.6667

0.7143

0.7500

0.7778

z = 0.8415 -0.1892
-0.0078 -0.0051

0.0458 -0.0180 -0.0053 -0.0275 -0.0195

Exercise 3:
1. 1) c=[1.1 -3.2 3.4 0.6; 0.6 1.2 -0.6 3.1; 1.3 0.6 5.5 0.0]
c=
1.1000 -3.2000

3.4000

0.6000

0.6000

1.2000 -0.6000

3.1000

1.3000

0.6000

5.5000

c(2,:)
ans =

0.6000

1.2000 -0.6000

3.1000

>> c(:,end)
ans =
0.6000
3.1000
0
>> c(1:2,2:end)
ans =
-3.2000

3.4000

0.6000

1.2000 -0.6000

3.1000

>> c(6)
ans =

0.6000

>> c(4:end)
ans = -3.2000
3.1000
0

1.2000

0.6000

>> c(1:2,2:4)
ans =
-3.2000

3.4000

0.6000

1.2000 -0.6000

3.1000

3.4000 -0.6000

5.5000

0.6000

0.0144

>> c(3)=c(1)
ans =
1.1000 -3.2000

3.4000

0.6000

0.6000

1.2000 -0.6000

3.1000

1.1000

0.6000

5.5000

3.4000

0.6000

0.6000 -0.6000

3.1000

c(:,2)=[]
c=
1.1000

1.1000

5.5000

>> c(2,2)=0
c=
1.1000

3.4000

0.6000

1.1000

5.5000

0.6000

3.1000
0

2) Find result of following expressions :


a=[1 0 2 1]
a=

>> a=[1 0 ;2 1]
a=
1

>> b=[-1 2 ;0 1]
b=
-1

>> c=[3 ;2]


c=
3

2
>> d=5
d=

>> a+b
ans =
0

>> a+c
Error using +
Matrix dimensions must agree.
>> a.*b
ans =
-1

>> a*b
ans =
-1

-2

>> a*c
ans =
3
8
>> a+d
ans =
6

>> a.*d
ans =
5
10

0
5

>> a*d
ans =
5

10

>> a./b
ans =
-1

Inf

>> a./a
ans =
1 NaN
1

>> a.^2
ans =
1

3) Create matrices using matrix generation functions ;


>>D=zeros(2,3)
D=
0

>>E=5*eye(3,3)
E=
5

>> F=3*ones(2,2)
F=
3

Exercise 4: Study functions using Help menu and check.


1. fliplr : Flip array in left/right direction.
Y = fliplr(X) returns X with the order of elements flipped left to right
along the second dimension.
2. zeros Zeros array.
zeros(N) is an N-by-N matrix of zeros.
3. linspace Linearly spaced vector.
linspace(X1, X2) generates a row vector of 100 linearly
equally spaced points between X1 and X2.
4. Sin Sine of argument in radians.
sin(X) is the sine of the elements of X.
5. cos

Cosine of argument in radians.

cos(X) is the cosine of the elements of X.


6. magic Magic square.
magic(N) is an N-by-N matrix constructed from the integers
1 through N^2 with equal row, column, and diagonal sums.
7. plot Linear plot.
plot(X,Y) plots vector Y versus vector X.
Example: plot(X,Y,'c+:') plots a cyan dotted line with a plus
at each data point;
8. title Graph title.
title('text') adds text at the top of the current axis.

9. subplot Create axes in tiled positions.

Example:
subplot(2,1,1), PLOT(income)
subplot(2,1,2), PLOT(outgo)
plots income on the top half of the window and outgo on the
bottom half. If the CurrentAxes is nested in a uipanel the
panel is used as the parent for the subplot instead of the
current figure.
10.xlabel X-axis label.
xlabel('text') adds text beside the X-axis on the current axis.
11.ylabel Y-axis label.
ylabel('text') adds text beside the Y-axis on the current axis.
12.input Prompt for user input.
Example:
reply = input('Do you want more? Y/N [Y]:','s');
if isempty(reply)
reply = 'Y';
end
13.eye Identity matrix.
eye(N) is the N-by-N identity matrix.
Example:
x = eye(2,3,'int8');
14.log10 Common (base 10) logarithm.
log10(X) is the base 10 logarithm of the elements of X.
15.floor Round towards minus infinity.
floor(X) rounds the elements of X to the nearest integers
infinity.

towards minus

ceil Round towards plus infinity.


ceil(X) rounds the elements of X to the nearest integers

towards infinity.

16.rand Uniformly distributed pseudorandom numbers.


R = rand(N) returns an N-by-N matrix containing pseudorandom values drawn
from the standard uniform distribution on the open interval(0,1).
Example 2: Use the RANDI function, instead of rand, to generate
integer values from the uniform distribution on the set 1:100.
r = randi(100,1,5);
17.exp Exponential.
exp(X) is the exponential of the elements of X, e to the X.
18.angle Phase angle.
angle(H) returns the phase angles, in radians, of a matrix with

complex elements.
19.abs Absolute value.
abs(X) is the absolute value of the elements of X.
20.log Natural logarithm.
log(X) is the natural logarithm of the elements of X.
21.sqrt Square root.
sqrt(X) is the square root of the elements of X.
Complex results are produced if X is not positive.
22.save Save workspace variables to file.
save(FILENAME) stores all variables from the current workspace in a
MATLAB formatted binary file (MAT-file) called FILENAME.
23.load Load data from MAT-file into workspace.
S = load(FILENAME) loads the variables from a MAT-file into a structure
array, or data from an ASCII file into a double-precision array.
Example:
load('handel.mat', 'y')
% Only variable y
24.legend Display legend.
legend(string1,string2,string3, ...) puts a legend on the current plot
using the specified strings as labels.
Examples:
x = 0:.2:12;
plot(x,besselj(1,x),x,besselj(2,x),x,besselj(3,x));
legend('First','Second','Third','Location','NorthEastOutside')

You might also like