Use nested for loops to perform the following exercises This

Use nested for loops to perform the following exercises. This is FOR MATLAB

(a) Create the 3-D function: T(x, y, z) = x 2 + y 2 + z 2 in a rectangular domain with x = [-2:0.2:2]; y = [-1:0.2:1] and z = [0:0.2:1]. Matrix T should have the dimension of length(x) by length(y) by length(z). Set p1a=T.

(b) Compute the average values of T along the z-direction and put the answer into p1b. The answer should be a 2-D matrix with the dimension of length(x) by length(y).

(c) Compute the average values of T over the x-y planes and put the answer into p1c. The answer should be a vector with the dimension of length(z).

(d) Compute the average value of T over the entire rectangular domain and put the answer into p1d. The answer should be a single number.

Solution

MALTAB CODE

clear all % Clearing the workspace
% The x direction vector
x = -2:0.2:2;
XL = length(x); % Number of points along x direction
y = -1:0.2:1; % The y direction vector
YL = length(y); % Number of points along y direction
z = 0:0.2:1; % The z direction vector
ZL = length(z); % Number of points in
% nested for loops for the evaluvation T;
T = zeros(XL,YL,ZL);
for i = 1:XL % loop for x direction
for j = 1:YL % loop for y direction
for k = 1:ZL % loop for z direction
T(i,j,k) = x(i)^2+y(j)^2+z(k)^2;
end
end
end
p1a = T;
% b part ans
p1b = zeros(XL,YL);
for i = 1:XL
for j = 1:YL
p1b(i,j) = sum(T(i,j,:))/ZL;
end
end
% c part ans
p1c = zeros(1,ZL);
for k =1:ZL
p1c(k) = sum(sum(T(:,:,k)))/(XL*YL);
end
% d part ans
p1d = sum(sum(sum(T)))/(XL*YL*ZL);
% disp% disp
fprintf(\'p1a\')
disp(p1a);
fprintf(\'p1b\')
disp(p1b);
fprintf(\'p1c\')
disp(p1c);
fprintf(\'p1d\')
disp(p1d);

OUTPUT

>> nestedforloop
p1a
(:,:,1) =

Columns 1 through 8

5.0000 4.6400 4.3600 4.1600 4.0400 4.0000 4.0400 4.1600
4.2400 3.8800 3.6000 3.4000 3.2800 3.2400 3.2800 3.4000
3.5600 3.2000 2.9200 2.7200 2.6000 2.5600 2.6000 2.7200
2.9600 2.6000 2.3200 2.1200 2.0000 1.9600 2.0000 2.1200
2.4400 2.0800 1.8000 1.6000 1.4800 1.4400 1.4800 1.6000
2.0000 1.6400 1.3600 1.1600 1.0400 1.0000 1.0400 1.1600
1.6400 1.2800 1.0000 0.8000 0.6800 0.6400 0.6800 0.8000
1.3600 1.0000 0.7200 0.5200 0.4000 0.3600 0.4000 0.5200
1.1600 0.8000 0.5200 0.3200 0.2000 0.1600 0.2000 0.3200
1.0400 0.6800 0.4000 0.2000 0.0800 0.0400 0.0800 0.2000
1.0000 0.6400 0.3600 0.1600 0.0400 0 0.0400 0.1600
1.0400 0.6800 0.4000 0.2000 0.0800 0.0400 0.0800 0.2000
1.1600 0.8000 0.5200 0.3200 0.2000 0.1600 0.2000 0.3200
1.3600 1.0000 0.7200 0.5200 0.4000 0.3600 0.4000 0.5200
1.6400 1.2800 1.0000 0.8000 0.6800 0.6400 0.6800 0.8000
2.0000 1.6400 1.3600 1.1600 1.0400 1.0000 1.0400 1.1600
2.4400 2.0800 1.8000 1.6000 1.4800 1.4400 1.4800 1.6000
2.9600 2.6000 2.3200 2.1200 2.0000 1.9600 2.0000 2.1200
3.5600 3.2000 2.9200 2.7200 2.6000 2.5600 2.6000 2.7200
4.2400 3.8800 3.6000 3.4000 3.2800 3.2400 3.2800 3.4000
5.0000 4.6400 4.3600 4.1600 4.0400 4.0000 4.0400 4.1600

Columns 9 through 11

4.3600 4.6400 5.0000
3.6000 3.8800 4.2400
2.9200 3.2000 3.5600
2.3200 2.6000 2.9600
1.8000 2.0800 2.4400
1.3600 1.6400 2.0000
1.0000 1.2800 1.6400
0.7200 1.0000 1.3600
0.5200 0.8000 1.1600
0.4000 0.6800 1.0400
0.3600 0.6400 1.0000
0.4000 0.6800 1.0400
0.5200 0.8000 1.1600
0.7200 1.0000 1.3600
1.0000 1.2800 1.6400
1.3600 1.6400 2.0000
1.8000 2.0800 2.4400
2.3200 2.6000 2.9600
2.9200 3.2000 3.5600
3.6000 3.8800 4.2400
4.3600 4.6400 5.0000


(:,:,2) =

Columns 1 through 8

5.0400 4.6800 4.4000 4.2000 4.0800 4.0400 4.0800 4.2000
4.2800 3.9200 3.6400 3.4400 3.3200 3.2800 3.3200 3.4400
3.6000 3.2400 2.9600 2.7600 2.6400 2.6000 2.6400 2.7600
3.0000 2.6400 2.3600 2.1600 2.0400 2.0000 2.0400 2.1600
2.4800 2.1200 1.8400 1.6400 1.5200 1.4800 1.5200 1.6400
2.0400 1.6800 1.4000 1.2000 1.0800 1.0400 1.0800 1.2000
1.6800 1.3200 1.0400 0.8400 0.7200 0.6800 0.7200 0.8400
1.4000 1.0400 0.7600 0.5600 0.4400 0.4000 0.4400 0.5600
1.2000 0.8400 0.5600 0.3600 0.2400 0.2000 0.2400 0.3600
1.0800 0.7200 0.4400 0.2400 0.1200 0.0800 0.1200 0.2400
1.0400 0.6800 0.4000 0.2000 0.0800 0.0400 0.0800 0.2000
1.0800 0.7200 0.4400 0.2400 0.1200 0.0800 0.1200 0.2400
1.2000 0.8400 0.5600 0.3600 0.2400 0.2000 0.2400 0.3600
1.4000 1.0400 0.7600 0.5600 0.4400 0.4000 0.4400 0.5600
1.6800 1.3200 1.0400 0.8400 0.7200 0.6800 0.7200 0.8400
2.0400 1.6800 1.4000 1.2000 1.0800 1.0400 1.0800 1.2000
2.4800 2.1200 1.8400 1.6400 1.5200 1.4800 1.5200 1.6400
3.0000 2.6400 2.3600 2.1600 2.0400 2.0000 2.0400 2.1600
3.6000 3.2400 2.9600 2.7600 2.6400 2.6000 2.6400 2.7600
4.2800 3.9200 3.6400 3.4400 3.3200 3.2800 3.3200 3.4400
5.0400 4.6800 4.4000 4.2000 4.0800 4.0400 4.0800 4.2000

Columns 9 through 11

4.4000 4.6800 5.0400
3.6400 3.9200 4.2800
2.9600 3.2400 3.6000
2.3600 2.6400 3.0000
1.8400 2.1200 2.4800
1.4000 1.6800 2.0400
1.0400 1.3200 1.6800
0.7600 1.0400 1.4000
0.5600 0.8400 1.2000
0.4400 0.7200 1.0800
0.4000 0.6800 1.0400
0.4400 0.7200 1.0800
0.5600 0.8400 1.2000
0.7600 1.0400 1.4000
1.0400 1.3200 1.6800
1.4000 1.6800 2.0400
1.8400 2.1200 2.4800
2.3600 2.6400 3.0000
2.9600 3.2400 3.6000
3.6400 3.9200 4.2800
4.4000 4.6800 5.0400


(:,:,3) =

Columns 1 through 8

5.1600 4.8000 4.5200 4.3200 4.2000 4.1600 4.2000 4.3200
4.4000 4.0400 3.7600 3.5600 3.4400 3.4000 3.4400 3.5600
3.7200 3.3600 3.0800 2.8800 2.7600 2.7200 2.7600 2.8800
3.1200 2.7600 2.4800 2.2800 2.1600 2.1200 2.1600 2.2800
2.6000 2.2400 1.9600 1.7600 1.6400 1.6000 1.6400 1.7600
2.1600 1.8000 1.5200 1.3200 1.2000 1.1600 1.2000 1.3200
1.8000 1.4400 1.1600 0.9600 0.8400 0.8000 0.8400 0.9600
1.5200 1.1600 0.8800 0.6800 0.5600 0.5200 0.5600 0.6800
1.3200 0.9600 0.6800 0.4800 0.3600 0.3200 0.3600 0.4800
1.2000 0.8400 0.5600 0.3600 0.2400 0.2000 0.2400 0.3600
1.1600 0.8000 0.5200 0.3200 0.2000 0.1600 0.2000 0.3200
1.2000 0.8400 0.5600 0.3600 0.2400 0.2000 0.2400 0.3600
1.3200 0.9600 0.6800 0.4800 0.3600 0.3200 0.3600 0.4800
1.5200 1.1600 0.8800 0.6800 0.5600 0.5200 0.5600 0.6800
1.8000 1.4400 1.1600 0.9600 0.8400 0.8000 0.8400 0.9600
2.1600 1.8000 1.5200 1.3200 1.2000 1.1600 1.2000 1.3200
2.6000 2.2400 1.9600 1.7600 1.6400 1.6000 1.6400 1.7600
3.1200 2.7600 2.4800 2.2800 2.1600 2.1200 2.1600 2.2800
3.7200 3.3600 3.0800 2.8800 2.7600 2.7200 2.7600 2.8800
4.4000 4.0400 3.7600 3.5600 3.4400 3.4000 3.4400 3.5600
5.1600 4.8000 4.5200 4.3200 4.2000 4.1600 4.2000 4.3200

Columns 9 through 11

4.5200 4.8000 5.1600
3.7600 4.0400 4.4000
3.0800 3.3600 3.7200
2.4800 2.7600 3.1200
1.9600 2.2400 2.6000
1.5200 1.8000 2.1600
1.1600 1.4400 1.8000
0.8800 1.1600 1.5200
0.6800 0.9600 1.3200
0.5600 0.8400 1.2000
0.5200 0.8000 1.1600
0.5600 0.8400 1.2000
0.6800 0.9600 1.3200
0.8800 1.1600 1.5200
1.1600 1.4400 1.8000
1.5200 1.8000 2.1600
1.9600 2.2400 2.6000
2.4800 2.7600 3.1200
3.0800 3.3600 3.7200
3.7600 4.0400 4.4000
4.5200 4.8000 5.1600


(:,:,4) =

Columns 1 through 8

5.3600 5.0000 4.7200 4.5200 4.4000 4.3600 4.4000 4.5200
4.6000 4.2400 3.9600 3.7600 3.6400 3.6000 3.6400 3.7600
3.9200 3.5600 3.2800 3.0800 2.9600 2.9200 2.9600 3.0800
3.3200 2.9600 2.6800 2.4800 2.3600 2.3200 2.3600 2.4800
2.8000 2.4400 2.1600 1.9600 1.8400 1.8000 1.8400 1.9600
2.3600 2.0000 1.7200 1.5200 1.4000 1.3600 1.4000 1.5200
2.0000 1.6400 1.3600 1.1600 1.0400 1.0000 1.0400 1.1600
1.7200 1.3600 1.0800 0.8800 0.7600 0.7200 0.7600 0.8800
1.5200 1.1600 0.8800 0.6800 0.5600 0.5200 0.5600 0.6800
1.4000 1.0400 0.7600 0.5600 0.4400 0.4000 0.4400 0.5600
1.3600 1.0000 0.7200 0.5200 0.4000 0.3600 0.4000 0.5200
1.4000 1.0400 0.7600 0.5600 0.4400 0.4000 0.4400 0.5600
1.5200 1.1600 0.8800 0.6800 0.5600 0.5200 0.5600 0.6800
1.7200 1.3600 1.0800 0.8800 0.7600 0.7200 0.7600 0.8800
2.0000 1.6400 1.3600 1.1600 1.0400 1.0000 1.0400 1.1600
2.3600 2.0000 1.7200 1.5200 1.4000 1.3600 1.4000 1.5200
2.8000 2.4400 2.1600 1.9600 1.8400 1.8000 1.8400 1.9600
3.3200 2.9600 2.6800 2.4800 2.3600 2.3200 2.3600 2.4800
3.9200 3.5600 3.2800 3.0800 2.9600 2.9200 2.9600 3.0800
4.6000 4.2400 3.9600 3.7600 3.6400 3.6000 3.6400 3.7600
5.3600 5.0000 4.7200 4.5200 4.4000 4.3600 4.4000 4.5200

Columns 9 through 11

4.7200 5.0000 5.3600
3.9600 4.2400 4.6000
3.2800 3.5600 3.9200
2.6800 2.9600 3.3200
2.1600 2.4400 2.8000
1.7200 2.0000 2.3600
1.3600 1.6400 2.0000
1.0800 1.3600 1.7200
0.8800 1.1600 1.5200
0.7600 1.0400 1.4000
0.7200 1.0000 1.3600
0.7600 1.0400 1.4000
0.8800 1.1600 1.5200
1.0800 1.3600 1.7200
1.3600 1.6400 2.0000
1.7200 2.0000 2.3600
2.1600 2.4400 2.8000
2.6800 2.9600 3.3200
3.2800 3.5600 3.9200
3.9600 4.2400 4.6000
4.7200 5.0000 5.3600


(:,:,5) =

Columns 1 through 8

5.6400 5.2800 5.0000 4.8000 4.6800 4.6400 4.6800 4.8000
4.8800 4.5200 4.2400 4.0400 3.9200 3.8800 3.9200 4.0400
4.2000 3.8400 3.5600 3.3600 3.2400 3.2000 3.2400 3.3600
3.6000 3.2400 2.9600 2.7600 2.6400 2.6000 2.6400 2.7600
3.0800 2.7200 2.4400 2.2400 2.1200 2.0800 2.1200 2.2400
2.6400 2.2800 2.0000 1.8000 1.6800 1.6400 1.6800 1.8000
2.2800 1.9200 1.6400 1.4400 1.3200 1.2800 1.3200 1.4400
2.0000 1.6400 1.3600 1.1600 1.0400 1.0000 1.0400 1.1600
1.8000 1.4400 1.1600 0.9600 0.8400 0.8000 0.8400 0.9600
1.6800 1.3200 1.0400 0.8400 0.7200 0.6800 0.7200 0.8400
1.6400 1.2800 1.0000 0.8000 0.6800 0.6400 0.6800 0.8000
1.6800 1.3200 1.0400 0.8400 0.7200 0.6800 0.7200 0.8400
1.8000 1.4400 1.1600 0.9600 0.8400 0.8000 0.8400 0.9600
2.0000 1.6400 1.3600 1.1600 1.0400 1.0000 1.0400 1.1600
2.2800 1.9200 1.6400 1.4400 1.3200 1.2800 1.3200 1.4400
2.6400 2.2800 2.0000 1.8000 1.6800 1.6400 1.6800 1.8000
3.0800 2.7200 2.4400 2.2400 2.1200 2.0800 2.1200 2.2400
3.6000 3.2400 2.9600 2.7600 2.6400 2.6000 2.6400 2.7600
4.2000 3.8400 3.5600 3.3600 3.2400 3.2000 3.2400 3.3600
4.8800 4.5200 4.2400 4.0400 3.9200 3.8800 3.9200 4.0400
5.6400 5.2800 5.0000 4.8000 4.6800 4.6400 4.6800 4.8000

Columns 9 through 11

5.0000 5.2800 5.6400
4.2400 4.5200 4.8800
3.5600 3.8400 4.2000
2.9600 3.2400 3.6000
2.4400 2.7200 3.0800
2.0000 2.2800 2.6400
1.6400 1.9200 2.2800
1.3600 1.6400 2.0000
1.1600 1.4400 1.8000
1.0400 1.3200 1.6800
1.0000 1.2800 1.6400
1.0400 1.3200 1.6800
1.1600 1.4400 1.8000
1.3600 1.6400 2.0000
1.6400 1.9200 2.2800
2.0000 2.2800 2.6400
2.4400 2.7200 3.0800
2.9600 3.2400 3.6000
3.5600 3.8400 4.2000
4.2400 4.5200 4.8800
5.0000 5.2800 5.6400


(:,:,6) =

Columns 1 through 8

6.0000 5.6400 5.3600 5.1600 5.0400 5.0000 5.0400 5.1600
5.2400 4.8800 4.6000 4.4000 4.2800 4.2400 4.2800 4.4000
4.5600 4.2000 3.9200 3.7200 3.6000 3.5600 3.6000 3.7200
3.9600 3.6000 3.3200 3.1200 3.0000 2.9600 3.0000 3.1200
3.4400 3.0800 2.8000 2.6000 2.4800 2.4400 2.4800 2.6000
3.0000 2.6400 2.3600 2.1600 2.0400 2.0000 2.0400 2.1600
2.6400 2.2800 2.0000 1.8000 1.6800 1.6400 1.6800 1.8000
2.3600 2.0000 1.7200 1.5200 1.4000 1.3600 1.4000 1.5200
2.1600 1.8000 1.5200 1.3200 1.2000 1.1600 1.2000 1.3200
2.0400 1.6800 1.4000 1.2000 1.0800 1.0400 1.0800 1.2000
2.0000 1.6400 1.3600 1.1600 1.0400 1.0000 1.0400 1.1600
2.0400 1.6800 1.4000 1.2000 1.0800 1.0400 1.0800 1.2000
2.1600 1.8000 1.5200 1.3200 1.2000 1.1600 1.2000 1.3200
2.3600 2.0000 1.7200 1.5200 1.4000 1.3600 1.4000 1.5200
2.6400 2.2800 2.0000 1.8000 1.6800 1.6400 1.6800 1.8000
3.0000 2.6400 2.3600 2.1600 2.0400 2.0000 2.0400 2.1600
3.4400 3.0800 2.8000 2.6000 2.4800 2.4400 2.4800 2.6000
3.9600 3.6000 3.3200 3.1200 3.0000 2.9600 3.0000 3.1200
4.5600 4.2000 3.9200 3.7200 3.6000 3.5600 3.6000 3.7200
5.2400 4.8800 4.6000 4.4000 4.2800 4.2400 4.2800 4.4000
6.0000 5.6400 5.3600 5.1600 5.0400 5.0000 5.0400 5.1600

Columns 9 through 11

5.3600 5.6400 6.0000
4.6000 4.8800 5.2400
3.9200 4.2000 4.5600
3.3200 3.6000 3.9600
2.8000 3.0800 3.4400
2.3600 2.6400 3.0000
2.0000 2.2800 2.6400
1.7200 2.0000 2.3600
1.5200 1.8000 2.1600
1.4000 1.6800 2.0400
1.3600 1.6400 2.0000
1.4000 1.6800 2.0400
1.5200 1.8000 2.1600
1.7200 2.0000 2.3600
2.0000 2.2800 2.6400
2.3600 2.6400 3.0000
2.8000 3.0800 3.4400
3.3200 3.6000 3.9600
3.9200 4.2000 4.5600
4.6000 4.8800 5.2400
5.3600 5.6400 6.0000

p1b Columns 1 through 8

5.3667 5.0067 4.7267 4.5267 4.4067 4.3667 4.4067 4.5267
4.6067 4.2467 3.9667 3.7667 3.6467 3.6067 3.6467 3.7667
3.9267 3.5667 3.2867 3.0867 2.9667 2.9267 2.9667 3.0867
3.3267 2.9667 2.6867 2.4867 2.3667 2.3267 2.3667 2.4867
2.8067 2.4467 2.1667 1.9667 1.8467 1.8067 1.8467 1.9667
2.3667 2.0067 1.7267 1.5267 1.4067 1.3667 1.4067 1.5267
2.0067 1.6467 1.3667 1.1667 1.0467 1.0067 1.0467 1.1667
1.7267 1.3667 1.0867 0.8867 0.7667 0.7267 0.7667 0.8867
1.5267 1.1667 0.8867 0.6867 0.5667 0.5267 0.5667 0.6867
1.4067 1.0467 0.7667 0.5667 0.4467 0.4067 0.4467 0.5667
1.3667 1.0067 0.7267 0.5267 0.4067 0.3667 0.4067 0.5267
1.4067 1.0467 0.7667 0.5667 0.4467 0.4067 0.4467 0.5667
1.5267 1.1667 0.8867 0.6867 0.5667 0.5267 0.5667 0.6867
1.7267 1.3667 1.0867 0.8867 0.7667 0.7267 0.7667 0.8867
2.0067 1.6467 1.3667 1.1667 1.0467 1.0067 1.0467 1.1667
2.3667 2.0067 1.7267 1.5267 1.4067 1.3667 1.4067 1.5267
2.8067 2.4467 2.1667 1.9667 1.8467 1.8067 1.8467 1.9667
3.3267 2.9667 2.6867 2.4867 2.3667 2.3267 2.3667 2.4867
3.9267 3.5667 3.2867 3.0867 2.9667 2.9267 2.9667 3.0867
4.6067 4.2467 3.9667 3.7667 3.6467 3.6067 3.6467 3.7667
5.3667 5.0067 4.7267 4.5267 4.4067 4.3667 4.4067 4.5267

Columns 9 through 11

4.7267 5.0067 5.3667
3.9667 4.2467 4.6067
3.2867 3.5667 3.9267
2.6867 2.9667 3.3267
2.1667 2.4467 2.8067
1.7267 2.0067 2.3667
1.3667 1.6467 2.0067
1.0867 1.3667 1.7267
0.8867 1.1667 1.5267
0.7667 1.0467 1.4067
0.7267 1.0067 1.3667
0.7667 1.0467 1.4067
0.8867 1.1667 1.5267
1.0867 1.3667 1.7267
1.3667 1.6467 2.0067
1.7267 2.0067 2.3667
2.1667 2.4467 2.8067
2.6867 2.9667 3.3267
3.2867 3.5667 3.9267
3.9667 4.2467 4.6067
4.7267 5.0067 5.3667

p1c 1.8667 1.9067 2.0267 2.2267 2.5067 2.8667

p1d 2.2333

>>

Use nested for loops to perform the following exercises. This is FOR MATLAB (a) Create the 3-D function: T(x, y, z) = x 2 + y 2 + z 2 in a rectangular domain wi
Use nested for loops to perform the following exercises. This is FOR MATLAB (a) Create the 3-D function: T(x, y, z) = x 2 + y 2 + z 2 in a rectangular domain wi
Use nested for loops to perform the following exercises. This is FOR MATLAB (a) Create the 3-D function: T(x, y, z) = x 2 + y 2 + z 2 in a rectangular domain wi
Use nested for loops to perform the following exercises. This is FOR MATLAB (a) Create the 3-D function: T(x, y, z) = x 2 + y 2 + z 2 in a rectangular domain wi
Use nested for loops to perform the following exercises. This is FOR MATLAB (a) Create the 3-D function: T(x, y, z) = x 2 + y 2 + z 2 in a rectangular domain wi
Use nested for loops to perform the following exercises. This is FOR MATLAB (a) Create the 3-D function: T(x, y, z) = x 2 + y 2 + z 2 in a rectangular domain wi
Use nested for loops to perform the following exercises. This is FOR MATLAB (a) Create the 3-D function: T(x, y, z) = x 2 + y 2 + z 2 in a rectangular domain wi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site