Вы находитесь на странице: 1из 19

Exercise:

Evaluate the following MATLAB expressions yourself before checking the answers in
MATLAB:

(a) 2ˆ(1 + 2)/3


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

(b) 3*4-5^2*2-3
>> 3*4-5^2*2-3
ans =
-41

(c) 3*(3*4-2*5^2-3)
>> 3*(3*4-2^2*5-3)
ans =
-33

(d) (2/3^2*5)*(3-4^3)^2
>> (2/3^2*5)*(3-4^3)^2
ans =
4.1344e+03
Exercise:
Study the following functions through the help menu, and check these functions in the
command window with suitable illustrations:

i) fliplr xi) sin xxi) eye


ii) zeros xii) cos xxii) log10
iii) ones xiii) exp xxiii) floor, ceil
iv) linspace xiv) angle xxiv) rand, randi
v) plot xv) abs xxv) repmat
vi) title xvi) log xxvi) reshape
vii) subplot xvii) sqrt xxvii) permute
viii) xlabel xviii) save xxviii) resize
ix) ylabel xix) load xxix) round
x) legend xx) input xxx) find

i) y = [1 3 5 4 7 8];
>>fliplr(y)
ans =
8 7 4 5 3 1

ii) >>zeros(5)
ans =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
iii) >>ones(2)
ans =
1 1
1 1

iv) >> u = linspace(4,2,10)


u=
4.0000 3.7778 3.5556 3.3333 3.1111 2.8889 2.6667 2.4444 2.2222 2.0000

v) >> plot(u,y)
vi) plot(u,y);title('graph 1');xlabel('x');ylabel('y');

vii), viii), ix), x) >>subplot(2,1,1);


plot(u,y);
title('graph 1');
xlabel('u');
ylabel('y');
legend('u vs. y');
subplot(2,1,2);
plot(y,u);
title('graph 2');
xlabel('y');
ylabel('u');
legend('y vs. u');

xi) >> x=linspace(0,1,10)


x=
0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000
>> sin(x)
ans =
0 0.1109 0.2204 0.3272 0.4300 0.5274 0.6184 0.7017 0.7764 0.8415

xii) >> cos(x)


ans =
1.0000 0.9938 0.9754 0.9450 0.9028 0.8496 0.7859 0.7125 0.6303 0.5403
xii) >>exp(3.56)
ans =
35.1632
xiv) >> z= [1 - 1i 2 + 1i 3 - 1i 4 + 1i];
>> P = angle(z)
P=
-0.7854 0.4636 -0.3218 0.2450
xv) >> abs(z)
ans =
1.4142 2.2361 3.1623 4.1231
xvi)>>log(26.14)
ans =
3.2635
xvii)>>sqrt(5)
ans =
2.2361
xviii) >> save('today')
xix) >> load('today')
xx) >> a = input('Enter a Even Number: ')
Enter a Even Number: 6
a=
6
xxi) >>eye(3,1)
ans =
1
0
0

xxii) >> log10(46.52)


ans =
1.6676
xxiii) >> c = [-5.3 2.6 9.4 7.1];
>> floor(c)
ans =
-6 2 9 7
>> ceil(c)
ans =
-5 3 10 8
xxiv) >>randi(5,5)
ans =
5 1 1 1 4
5 2 5 3 1
1 3 5 5 5
5 5 3 4 5
4 5 5 5 4
>>rand(2,3)
ans =
0.8147 0.1270 0.6324
0.9058 0.9134 0.0975
xxv) >> b = repmat(c,2)
b=
-5.3000 2.6000 9.4000 7.1000 -5.3000 2.6000 9.4000 7.1000
-5.3000 2.6000 9.4000 7.1000 -5.3000 2.6000 9.4000 7.1000

xxvi) >> reshape(b,[8,2])


ans =
-5.3000 -5.3000
-5.3000 -5.3000
2.6000 2.6000
2.6000 2.6000
9.4000 9.4000
9.4000 9.4000
7.1000 7.1000
7.1000 7.1000

xxvii) >>permute(x,[4 2 3 1 5])


ans =
1 -3 4 6 -7
xxviii) >> a

a=

0.2785 0.9575 0.1576


0.5469 0.9649 0.9706

>>resizem(a,3)

ans =

0.2785 0.2785 0.2785 0.9575 0.9575 0.9575 0.1576 0.1576 0.1576


0.2785 0.2785 0.2785 0.9575 0.9575 0.9575 0.1576 0.1576 0.1576
0.2785 0.2785 0.2785 0.9575 0.9575 0.9575 0.1576 0.1576 0.1576
0.5469 0.5469 0.5469 0.9649 0.9649 0.9649 0.9706 0.9706 0.9706
0.5469 0.5469 0.5469 0.9649 0.9649 0.9649 0.9706 0.9706 0.9706
0.5469 0.5469 0.5469 0.9649 0.9649 0.9649 0.9706 0.9706 0.9706
xxix) >> round(sqrt(2),4)
ans =
1.4142

xxx) >> find(b)

ans =

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Exercise
a) Generate three row vectors 𝑉1 = [1 3 √5] ,𝑉2 = [7 5] &𝑉3= [3 4 5] and Compute the
following i) 𝑉4=3∗𝑉1 ii) 𝑉5=2∗𝑉1−3∗𝑉3 iii) 𝑉6=𝑉1+𝑉2

>> v1=[1 3 sqrt(5)];


>> v2=[7 5];
>> v3=[3 4 5];
>> v4=3*v1
v4 =
3.0000 9.0000 6.7082
>> v5=2*v1-3*v3
v5 =
-7.0000 -6.0000 -10.5279
>> v6=v1+v2
Matrix dimensions must agree.

b) Generate a row vector V7 by concatenating the existing vectors V3 and V1 using the
following expression 𝑉7= [2∗𝑉3,−𝑉1].
>> v7=[2*v3,-v1]
v7 =
6.0000 8.0000 10.0000 -1.0000 -3.0000 -2.2361

c) Generate a row vector having seven elements, and extract its alternate entries.
>> v = [1 3 4 5 2 6 7]
v=
1 3 4 5 2 6 7
>> v=v(1:2:7)
v=
1 4 2 7

d) Generate a column vector by transposing a row vector.


>> reshape(v,[4,1])
ans =
1
4
2
7
e) The equation of a straight line is 𝑦=𝑚𝑥+𝑐, where m & c are constants. Compute the
y-coordinates of a line with slope 𝑚=0.5 and the intercept 𝑐 = −2 at the following x-
coordinates: 𝑥=0,1.5,3,4,5,7,9 𝑎𝑛𝑑 10.
>> x = [0 1.5 3 4 5 7 9 10]; m=0.5; c=-2;
>> y = m*x+c
y=
-2.0000 -1.2500 -0.5000 0 0.5000 1.5000 2.5000 3.0000
f) Create a vector t with 10 elements: 1, 2, 3…, 10. Now compute the following
quantities:
i) x = t sin(t)

t=[1:10]
t=
1 2 3 4 5 6 7 8 9 10
>> x=t.*sin(t)

x=

0.8415 1.8186 0.4234 -3.0272 -4.7946 -1.6765 4.5989 7.9149 3.7091 -5.4402

ii) y = (t-1)/(t+1)

>> y = (t-1)./(t+1)
y=
0 0.3333 0.5000 0.6000 0.6667 0.7143 0.7500 0.7778 0.8000 0.8182
iii) z = (sin(t2))/(t2)
>> z = (sin(2*t))./(2*t)

z=
0.4546 -0.1892 -0.0466 0.1237 -0.0544 -0.0447 0.0708 -0.0180 -0.0417 0.0456
Exercise
1. Assume array c is defined as,
𝑐=[1.1−3.23.40.61.2−0.61.30.65.5 0.63.10]

Determine the following and comment on the results.


a) c(2,:)
b) c(:,end)
c) c(1:2,2:end)
d) c(6)
e) c(4:end)
f) c(1:2,2:4)
g) Replace the 3rd element in the 1st column by the 1st element in the 1st column
h) c(:,2) = [ ]
i) c(2,2) = 0

a) >> c = [1.1 -3.2 3.4 0.6;0.6 1.2 -0.6 3.1;1.3 0.6 5.5 0]
c=
1.1000 -3.2000 3.4000 0.6000
0.6000 1.2000 -0.6000 3.1000
1.3000 0.6000 5.5000 0
>>c(2,:)
ans =
0.6000 1.2000 -0.6000 3.1000
b) >> c(:,end)
ans =
0.6000
3.1000
0
c) >> c(1:2,2:end)
ans =
-3.2000 3.4000 0.6000
1.2000 -0.6000 3.1000

d) >>c(6)
ans =
0.6000
e) >> c(4:end)
ans =
-3.2000 1.2000 0.6000 3.4000 -0.6000 5.5000 0.6000 3.1000 0
f) >>c(1:2,2:4)
ans =
-3.2000 3.4000 0.6000
1.2000 -0.6000 3.1000
g) >> c(1,1)=c(1,3);c(1,3)=c(1,1)
c=
3.4000 -3.2000 3.4000 0.6000
0.6000 1.2000 -0.6000 3.1000
1.3000 0.6000 5.5000 0
h) >> c = [1.1 -3.2 3.4 0.6;0.6 1.2 -0.6 3.1;1.3 0.6 5.5 0]
c=
1.1000 -3.2000 3.4000 0.6000
0.6000 1.2000 -0.6000 3.1000
1.3000 0.6000 5.5000 0
>> c(:,2)=[]
c=
1.1000 3.4000 0.6000
0.6000 -0.6000 3.1000
1.3000 5.5000 0

i) >>c(2,2)=0
c=
1.1000 3.4000 0.6000
0.6000 0 3.1000
1.3000 5.5000 0
2. Assume a, b, c and d are defined as follows:

𝑎=[10; 21],𝑏=[−12;01],𝑐=[3 ; 2],𝑑=5

What is the result of the following expressions?


a) Find the size of matrices ‘a’, ’b’, ’c’, and ‘d’
b) a+b
c) a+c
d) a.*b
e) a*b
f) a*c
g) a+d
h) a.*d
i) a*d
j) a./b
k) a./a
l) a.^2

>> a=[1 0;2 1]


a=
1 0
2 1
>> b=[-1 2; 0 1]
b=
-1 2
0 1
>> c=[3;2]
c=
3
2
>> d=5
d=
5
a) >> size(a)
ans =
2 2
>> size(b)
ans =
2 2
>> size(c)
ans =
2 1
>> size(d)
ans =
1 1

b) >>a+b
ans =
0 2
2 2
c) >>a+c
ans =
4 3
4 3

d) >> a.*b
ans =
-1 0
0 1

e) >> a*b
ans =
-1 2
-2 5

f) >> a*c
ans =
3
8

g) >>a+d
ans =
6 5
7 6
h) >> a.*d
ans =
5 0
10 5

i) >> a*d
ans =
5 0
10 5

j) >> a./b
ans =
-1 0
Inf 1

k) >> a./a
ans =
1 NaN
1 1

l) >> a.^2
ans =
1 0
4 1
3. Create the following matrices with the help of the matrix generation functions zeros, eye
and ones. See the online help on these functions, if required ( eg: help eye).
𝐷=[000;000] 𝐸= [500 ; 050 ;005] 𝐹=[33; 33]
>> D=zeros(2,3)

D=
0 0 0
0 0 0
>> E=5.*eye(3,3)
E=
5 0 0
0 5 0
0 0 5
>> F=3.*ones(2,2)
F=
3 3
3 3

4. Assume array c is defined as shown, and determine the following.


𝑐= [5−13; 920 −6; 25155]
a. C3 = mod(c,3)
b. Create the logical array L of C3
c. Use L as a logical index to pick out the elements of A that are multiples of 3.
d. Create a logical array of ‘c’ to pick out the elements of A that are multiples of 5.

>> c=[5 -1 3;9 20 -6;25 15 5]


c=
5 -1 3
9 20 -6
25 15 5
a) >> C3=mod(c,3)
C3 =
2 2 0
0 2 0
1 0 2
b) >> L=logical(mod(c,3))
L=
3×3 logical array
1 1 0
0 1 0
1 0 1

c) >> c(~L)
ans =

9
15
3
-6
d) >> c(L)
ans=
5
25
-1
20
5

Вам также может понравиться