Matlab Answers
Matlab Answers
Exercises
1.
x =
x =
Columns 1 through 13
32
44
34
46
36
48
38
50
40
52
42
54 56
8
>> y = [4 1 3 5]'
Columns 14 through 22
y =
4
1
58
70
60
72
62
64
66
68
74
2. Let x = [2 5 1 6].
a.
a.
to y
>> x+16
ans =
>> y+sum(x)
18
21
17
22
ans =
23
20
b.
22
elements
24
>> x(1:2:end) + 3
ans =
b.
>>x.^y
ans =
81
>> sqrt(x)
ans = 1.4142
2.2361
1.0000
216
2.4495
32768
d.
Compute the square of
each element
>> x.*x
ans =
25
36
>> y/x
ans =
0
0.5000
0
0.1250
c. 10 / 2 \ 5 - 3 + 2 * 4
>> 10 / 2 \ 5 - 3 + 2 * 4
ans =
6
d. 3 ^ 2 / 4
0.3750
0
0.6250
d. Multiply each element in x by the
corresponding element in y, calling
the result "z".
2.2500
e. 3 ^ 2 ^ 2
>> 3 ^ 2 ^ 2
ans =
81
f. 2 + round(6 / 9 + 3 * 2) / 2 3
>> 2 + round(6 / 9 + 3 * 2) / 2 - 3
ans =
2.5000
>> z=x.*y
z =
>> 3 ^ 2 / 4
ans =
12
g. 2 + floor(6 / 9 + 3 * 2) / 2 3
>> 2 + floor(6 / 9 + 3 * 2) / 2 - 3
ans =
2
18
40
h. 2 + ceil(6 / 9 + 3 * 2) / 2 3
e. Add up the elements in z and assign
the result to a variable called "w".
5.
>> 2 + ceil(6 / 9 + 3 * 2) / 2 - 3
ans =
2.5000
72
elements ...
a. 2, 4, 6, 8, ...
>> a = 2:2:20
a =
>> x'*y - w
ans =
Columns 1 through 10
0
2
14
18
10
12
20
b. 10, 8, 6, 4, 2, 0, -2, -4
>> b = 10:-2:-4
>> 2 / 2 * 3
ans =
16
b. 6 - 2 / 5 + 7 ^ 2 1
>> 6 - 2 / 5 + 7 ^ 2 - 1
ans =
53.6000
b =
0
-2
10
-4
x =
y =
1.0000
0.2500
0.5000
a. ln(2 + t + t2)
0.3333
0.2000
ans =
1/2
1/3
1/4
1.3863
1.8181
1.9516
1.5347
1.6790
2.0794
1/5
b. et(1 + cos(3t))
5.3864
rats(z)
x =
y =
z =
0.7500
0.5000
0.0272
0.6667
9.8893
c =
1/2
3/4
14.4838
1.0000
0
2.0671
c. cos2(t) + sin2(t)
0.8000
ans =
0.3428
1.0000
1.0000
1.0000
1.0000
2/3
4/5
d. tan-1(1)
>> d = atan(t)
d =
1.0122
0.7854
xn = (-1)n+1/(2n-1)
1.0637
e. cot(t)
>> n = 1:100;
>> e = cot(t)
0.8761
e =
0.1725
1.1071
0.6421
-0.0292
0.9505
0.3888
-0.2333
-0.4577
y = sum(x)
f. sec2(t) + cot(t) 1
y =
0.7829
t =
1.8000
1.2000
2.0000
1.0e+003 *
0.0031
0.0070
0.0338
1.1718
0.0181
0.0043
9. Plot the functions x, x3, ex and ex
over the interval 0 < x < 4.
>> t = 1:0.2:2
1.0000
f =
1.4000
1.6000
a. on rectangular paper
c. on log-log paper
Be sure to use an appropriate mesh of
x values to get a smooth set of
curves.
1.9053e+008
x=linspace(0.01,0.1,30);
y=sin(1./x);
plot(x,y)
hold on
title('Plot of f(x)= sin(1/x)')
hold off
Array Exercises
1.
Given x = [3 1 5 7 9 2 6],
explain what the following commands
"mean" by
by summarizing the net result of
the command.
a. x(3)
>> x(3)
ans =
f. x([1 6 2 1 1])
ans = 3
1 to column 7.
33
ans = 3
6
This command gives the all the
values in the array from column
1 to end.
d. x(1:end-1)
>> y = A(end-1:end,:)
>> x(1:end-1)
ans =
2
-
ans =
c. x(1:end)
>> x(1:end)
g. sum(x)
>> sum(x)
and 1 respectively.
ans = 3
>> x(1:7)
b. x(1:7)
y =
6
7
2
3
5
9
c. compute the sum over the columns of
A
>> sum(A)
ans =
11
16
12
e. x(6:-2:1)
>> sum(A')'
>> x(6:-2:1)
ans =
-
ans =
2
7
15
17
e. compute the standard error of the
mean of each column of A (NB. the
standard error of the mean is defined
as the standard deviation divided by
>> [x ; y]
ans =
>> N = size(A,1)
N = 3
g. A - 3
>> A - 3
>> e = std(A)/sqrt(N)
ans =
e =
0
2
-2
1.2019
0.8819
-1
3
4
2.5166
a. x + y
ans = 2
>> x+y
ans =
13
b. x + A
a. A'
>> A'
7
d. A - [x' y']
Error using minus in the matrix,
the dimensions of matrix A and the
matrix of the product of x and y
should be the same.
e. [x ; y']
Error using vertcat, CAT arguments
dimensions are not consistent.
f. [x ; y]
ans =
2
g. fliplr(A)
ans =
>> reshape(A,2,6)
ans = 2
6
This command returns A with its
columns
matrix
axis)
e. A(:)
h. [A(end,:)]
>> A(:)
flipped
in
the
left-right
>> [A(end,:)]
ans = 2
3
ans =
7
1
the 3
row of matrix A.
9
5
i. A(1:3,:)
>> A(1:3,:)
ans =
5
Gives
all
the
elements
of
A,
This
command
gives
all
the
f. flipud(A)
>> flipud(A)
ans = 8
j. [A ; A(1:2,:)]
>> [A ; A(1:2,:)]
ans =
2
k. sum(A)
>> sum(A)
ans =
-
13
16
18
>> C = A(1:2:end,:)
C =
2
8
l. sum(A')
>> sum(A')
ans = 25
-
15
16
7
1
9
2
m. sum(A,2)
>> sum(A,2)
ans = 25
15
>> rats(1./A)
16
This command returns the sum of matrix
A along the 2 dimension.
o. [ [ A ; sum(A) ] [ sum(A,2) ;
ans =
1/2
1/3
1/8
1/7
1
1
1/9
1/5
1/2
sum(A(:)) ] ]
>> [ [ A ; sum(A) ] [ sum(A,2) ;
sum(A(:)) ] ]
>> sqrt(A)
ans =
2
25
ans =
15
2.6458
16
13
16
18
56
1.4142
2.6458
3.0000
1.7321
1.0000
2.2361
2.4495
2.8284
1.0000
1.4142
2.2361
>>
A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5]
A =
7
5
>> randn('seed',123456789)
>> F = randn(5,10);
>> N=size(F,1)
1/7
1/6
1/5
avg =
Columns 1 through 8
1.0265
-0.0674
-0.3151
-0.6386
-0.2153
-0.1415
0.3403
-0.3431
Columns 9 through 10
0.1726
0.3760
a. x > y
ans =0
0
0
-
>> s = std(F)
s =
Columns 1 through 8
1.2131
0.7748
0.7533
0.9208
0.3313
1.1053
answer.
0.5499
1.0867
b. y < x
Columns 9 through 10
0.6738
ans =0
0
0.9204
0
-
answer.
tscore =
Columns 1 through 8
1.8922
-0.1999
-1.4533
-0.5754
-0.9093
0.8263
-1.2918
-0.7060
c. x == y
ans = 0
0
1
-
Columns 9 through 10
0.5729
0.9134
d. x <= y
ans = 1
0
1
-
logical 0(false).
i. (x > y) | (y < x)
as an answer.
ans =
1
e. y >= x
ans = 1
0
1
-
0
or (y<x) is
0(false).
-
ans = 1
ans =
1
f. x | y
ans = 0
g. x & y
ans = 1
0
0
-
b. x(x > 5)
ans = 6
10
greater than 5.
logical 0 (false).
c. y(x <= 4)
h. x & (~y)
ans =
1
ans =
0
0
d. x( (x < 2) | (x >= 8) )
ans =
10
e. y( (x < 2) | (x >= 8) )
ans = 3
4
7
f. x(y < 0
ans = Empty matrix: 1-by-0
3. You can see the
differences by
executing the following commands that
attempt to extract the elements
of y
that correspond to either the odd (a.)
or even (b.) elements of x:
a. y(rem(x,2)) vs.
y(logical(rem(x,2)))
>> y(rem(x,2))
??? Subscript indices must either be
real positive integers or logicals.
>> y(logical(rem(x,2)))
b. y(~rem(x,2)) vs.
y(~logical(rem(x,2)))
>> y(~rem(x,2))
a(idxa) = 0
ans =
3
if x < 6
if 6 <= x < 20
if 20 <= x <= 35
ans =
6
>> x = 1:35;
y = zeros(size(x));
>> y(~logical(rem(x,2)))
idx1 = x < 6;
ans =
4. Given x = [3 15 9 12 -1 0 -12 9 6
y(idx1) = 2;
y(idx2) = x(idx2) - 4;
y(idx3) = 36 - x(idx3);
4.
a.
b.
c.
d.
if 0 < x < 10
y = 4*x
elseif 10 < x < 40
y = 10*x
else
y = 500
end
x
x
x
x
=
=
=
=
-1
5
30
100
y
y
y
y
= 500
= 20
= 300
= 500
if n > 1
m = n+1
else
m = n - 1
end
a. n = 7
m = 8
b. n = 0
m = -1
c. n = -10 m = -11
2.
if z < 5
w = 2*z
elseif z < 10
w = 9 - z
elseif z < 100
w = sqrt(z)
else
w = z
end
a.
b.
c.
d.
3.
z
z
z
z
=
=
=
=
1
9
60
200
w
w
w
w
=
=
=
=
2
0
7.7460
200
if T < 30
h = 2*T + 1
elseif T < 10
h = T - 2
else
h = 0
end
a. T = 50
b. T = 15
c. T = 0
h = 0
h = 31
h = 1
A = rand(4,7);
[M,N] = size(A);
for j = 1:M
a. total = 0;
for k = 1:N
if A(j,k) < 0.2
A(j,k) = 0;
else
A(j,k) = 1;
end
end
% loop
until 20 is exceeded
end
count = count + 1; %
3. Given x = [4 1 6] and y = [6 2 7],
compute the following arrays
a. aij = xiyj
b. bij = xi/yj
c. ci = xiyi, then add up the
elements of c.
d. dij = xi/(2 + xi + yj)
e. eij = reciprocal of the lesser of
xi and yj
x = [4 1 6], y = [6 2 7]
time.'])
N = length(x);
for j = 1:N
--------------------------------------
c(j) = x(j)*y(j);
----------------------
for k = 1:N
a(j,k) = x(j)*y(k);
b(j,k) = x(j)/y(k);
d(j,k) = x(j)/(2 + x(j) +
y(k));
e(j,k) = 1/min(x(j),y(k));
end
loop
end
c = sum(c);
% or use 1.a.
% collect 1000
count = zeros(Nrep,1);
for j = 1:Nrep
c. count = 0;
total = 0;
avg = 0;
% test
variable
count(j) = count(j) +
count = count + 1;
rand(1,1);
end
hist(count,min(count):max(count))
Dividing by count
% then gives the new average.
xlabel('Number of random
numbers from U(0,1) added to make 20')
ylabel('Count')
avg = ((count-1)*avg +
rand(1,1))/count;
title(['Histogram of results
var.
---------------------end
b. count = 0;
while 1
disp(['It took
%
count = count + 1;
x = rand(1,1);
% get a number
if (x < 0.85) & (x > 0.8)
% check the value
break
% bail out if in selected range
end
end
disp(['It took
',int2str(count),' numbers this
time.'])
5. while 1
% use of an infinite
loop
TinF = input('Temperature in
F: ');
% get input
if isempty(TinF)
IF-block Exercises
1. n = 7
gives m = 8
n = 9
gives m = -1
Programming Exercises
2. z = 1
gives w = 2
z = 9
gives w = 0
z = 60
gives w = sqrt(60)
0 gives h = 1
4. x = -1
gives y = -4
x = 5
gives y = 20
x = 30
gives y = 120
The
1.
x=0;
for i=1:1000
x=x+(1/((((2*i)-1)^2)*(((2*i)
+1)^2)));
z=sqrt((x*16)+8);
error=abs(z-pi);
if (error < 10^-8)
i
break
end
end
i =
174