Академический Документы
Профессиональный Документы
Культура Документы
Evaluate the following MATLAB expressions yourself before checking the answers in
MATLAB:
(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) 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
v) >> plot(u,y)
vi) plot(u,y);title('graph 1');xlabel('x');ylabel('y');
a=
>>resizem(a,3)
ans =
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
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
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]
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:
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
c) >> c(~L)
ans =
9
15
3
-6
d) >> c(L)
ans=
5
25
-1
20
5