Vous êtes sur la page 1sur 6

Lab 12A Review Problems

Solution
Section 1
Problem 1
The script was saved as ReviewProb1.m
% Script "ReviewProb1"
% Solution
%
%
%
%
%
%
%
%
%
%
%
%

What is the pattern?


Z(1,1) = 1
Z(1,2) = 2
...
Z(1,10) = 10
Z(2,1) = 2
Z(2,2) = 4
...
Z(2,10) = 20
Z(3,1) = 3
...
Z(10,10) = 100

% Use nested for loops to accomplish


Z = [];
for i = 1:10
for j = 1:10
Z(i,j) = i*j;
end
end
display(Z)
% Otherwise, could use a single for loop using a row vector
Z = [];
v = 1:10;
for k = 1:10
Z(k,:) = k*v;
end
display(Z)
%
%
%
%
%
%

Second Part, create A with diagonals of Z


What is the pattern?
A(1) = Z(1,1)
A(2) = Z(2,2)
...
A(10) = Z(10,10)

% Use a single for loop to accomplish


A = [];
for q = 1:10
A(q) = Z(q,q);
end

display(A)
% Third Part, create B with odd rows of Z
B = [];
[rows,columns] = size(Z);
row_numbers = 1:2:rows;
B = Z(row_numbers,:)
% Fourth Part, create a surface plot
surf(Z)
title('Organized table')
xlabel('Column number')
ylabel('Row number')
zlabel('Value')

Command Window Output


>> run ReviewProb6
Z =
1
2
3
4
5
6
7
8
9
10

2
4
6
8
10
12
14
16
18
20

3
6
9
12
15
18
21
24
27
30

4
8
12
16
20
24
28
32
36
40

5
10
15
20
25
30
35
40
45
50

6
12
18
24
30
36
42
48
54
60

7
14
21
28
35
42
49
56
63
70

8
16
24
32
40
48
56
64
72
80

9
18
27
36
45
54
63
72
81
90

10
20
30
40
50
60
70
80
90
100

1
2
3
4
5
6
7
8
9
10

2
4
6
8
10
12
14
16
18
20

3
6
9
12
15
18
21
24
27
30

4
8
12
16
20
24
28
32
36
40

5
10
15
20
25
30
35
40
45
50

6
12
18
24
30
36
42
48
54
60

7
14
21
28
35
42
49
56
63
70

8
16
24
32
40
48
56
64
72
80

9
18
27
36
45
54
63
72
81
90

10
20
30
40
50
60
70
80
90
100

16

25

36

49

64

81

100

1
3
5
7
9

2
6
10
14
18

3
9
15
21
27

4
12
20
28
36

5
15
25
35
45

6
18
30
42
54

7
21
35
49
63

8
24
40
56
72

9
27
45
63
81

10
30
50
70
90

Z =

A =
B =

Organized table

100
80

Value

60
40
20
0
10
10
8

6
4

Row number

2
0

Column number

Problem 2
The script was saved as Fin.m
% Script "Fin"
% Soltuion
% Given variables
T_start = 20; %deg C
L = 10; %mm
x = 0:L; % Step size of 1 is small enough for a smooth plot
% Plotting commands
figure
title('Temperature of fin')
xlabel('x (mm)')
ylabel('Temperature (deg C)')
% Use a loop to repeat the plotting commands for each time value
hold on
for t = 0:10:60
T = T_start + exp(-5/t)*(25 - 10*(x.^2)/(L^2));
plot(x,T)
end
hold off

Command Window Output


>> run Fin
Temperature of fin
45

Temperature (deg C)

40

35

30

25

20

5
x (mm)

10

Problem 3
The script was saved as ElevationCalc.m
% The script will find the elevation of a mountain at several combinations
% of x and y points, locate the maximum value, and generate a surface plot
% with an overlay of the maximum elevation
x = linspace(-10,10,100); % mi
y = linspace(-10,10,50); % mi
elevation=[];
for i=1:length(y)
for j=1:length(x)
elevation(i,j)=50*exp(.05*x(j)+.05*y(i))-0.5*(x(j)^2+y(i)^2);
if elevation(i,j)<0
elevation(i,j)=0;
end
end
end
surf(x,y,elevation)
title('Elevation of a mountain')
xlabel('x (miles)')
ylabel('y (miles)')
zlabel('height (yards)')
hold on
apex = max(max(elevation));
[row,column] = find(elevation==apex);
x_max = x(column);
y_max = y(row);
plot3(x_max,y_max,apex,'g.','Markersize',40)
hold off

Command Window Output


>> run ElevationCalc

Elevation of a mountain

60

height (yards)

50
40
30
20
10
0
10
5

10
5

-5
y (miles)

-5
-10

-10

x (miles)

Vous aimerez peut-être aussi