MATLAB Use nested for loops to perform the following exercis
MATLAB
Use nested for loops to perform the following exercises.
(a) Create the 3-D function: T(x,y,z) = x2 + y2 + z2 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 a=T.
(b) Compute the average values of T along the z-direction. 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. The answer should be a vector with the dimension of length(z).
(d) Compute the average value of T over the entire rectangular domain. The answer should be a single number.
Solution
Solution:
%***********************************
clear all;
clc;
x = [-2:0.2:2];
y = [-1:.2:1];
z = [0:.2:1];
T = zeros(length(x) , length(y) , length(z));
%1 definition of T
for i= 1:length(x)
for j=1:length(y)
for k=1:length(z)
T(i,j,k)=(x(i))^2 + (y(j))^2 +(z(k))^2 ;
end
end
end
%2 average along z
ans1 = zeros(length(x),length(y));
for i= 1:length(x)
for j=1:length(y)
ans1(i,j)= sum(T(i,j,:))/length(z) ;
end
end
%2 average along z
ans2 = zeros(1,length(z));
for k=1:length(z)
for i= 1:length(x)
for j=1:length(y)
ans2(k)= sum(sum(T(:,:,k)))/(length(x)*length(y)) ;
end
end
end
%2 average along z
anst = zeros(1,length(z));
for k=1:length(z)
for i= 1:length(x)
for j=1:length(y)
anst(k)= sum(sum(T(:,:,k)))/(length(x)*length(y)) ;
end
end
end
ans3 = sum(anst)/length(z);
%**********************************************************
answer are there in matrix :
a-> T
b-> ans1
ans1 =
5.3667 5.0067 4.7267 4.5267 4.4067 4.3667 4.4067 4.5267 4.7267 5.0067 5.3667
4.6067 4.2467 3.9667 3.7667 3.6467 3.6067 3.6467 3.7667 3.9667 4.2467 4.6067
3.9267 3.5667 3.2867 3.0867 2.9667 2.9267 2.9667 3.0867 3.2867 3.5667 3.9267
3.3267 2.9667 2.6867 2.4867 2.3667 2.3267 2.3667 2.4867 2.6867 2.9667 3.3267
2.8067 2.4467 2.1667 1.9667 1.8467 1.8067 1.8467 1.9667 2.1667 2.4467 2.8067
2.3667 2.0067 1.7267 1.5267 1.4067 1.3667 1.4067 1.5267 1.7267 2.0067 2.3667
2.0067 1.6467 1.3667 1.1667 1.0467 1.0067 1.0467 1.1667 1.3667 1.6467 2.0067
1.7267 1.3667 1.0867 0.8867 0.7667 0.7267 0.7667 0.8867 1.0867 1.3667 1.7267
1.5267 1.1667 0.8867 0.6867 0.5667 0.5267 0.5667 0.6867 0.8867 1.1667 1.5267
1.4067 1.0467 0.7667 0.5667 0.4467 0.4067 0.4467 0.5667 0.7667 1.0467 1.4067
1.3667 1.0067 0.7267 0.5267 0.4067 0.3667 0.4067 0.5267 0.7267 1.0067 1.3667
1.4067 1.0467 0.7667 0.5667 0.4467 0.4067 0.4467 0.5667 0.7667 1.0467 1.4067
1.5267 1.1667 0.8867 0.6867 0.5667 0.5267 0.5667 0.6867 0.8867 1.1667 1.5267
1.7267 1.3667 1.0867 0.8867 0.7667 0.7267 0.7667 0.8867 1.0867 1.3667 1.7267
2.0067 1.6467 1.3667 1.1667 1.0467 1.0067 1.0467 1.1667 1.3667 1.6467 2.0067
2.3667 2.0067 1.7267 1.5267 1.4067 1.3667 1.4067 1.5267 1.7267 2.0067 2.3667
2.8067 2.4467 2.1667 1.9667 1.8467 1.8067 1.8467 1.9667 2.1667 2.4467 2.8067
3.3267 2.9667 2.6867 2.4867 2.3667 2.3267 2.3667 2.4867 2.6867 2.9667 3.3267
3.9267 3.5667 3.2867 3.0867 2.9667 2.9267 2.9667 3.0867 3.2867 3.5667 3.9267
4.6067 4.2467 3.9667 3.7667 3.6467 3.6067 3.6467 3.7667 3.9667 4.2467 4.6067
5.3667 5.0067 4.7267 4.5267 4.4067 4.3667 4.4067 4.5267 4.7267 5.0067 5.3667
c->ans2
ans2 =
1.8667 1.9067 2.0267 2.2267 2.5067 2.8667
d->ans3
ans3 =
2.2333
![MATLAB Use nested for loops to perform the following exercises. (a) Create the 3-D function: T(x,y,z) = x2 + y2 + z2 in a rectangular domain with x = [-2:0.2:2] MATLAB Use nested for loops to perform the following exercises. (a) Create the 3-D function: T(x,y,z) = x2 + y2 + z2 in a rectangular domain with x = [-2:0.2:2]](/WebImages/18/matlab-use-nested-for-loops-to-perform-the-following-exercis-1035464-1761537204-0.webp)
![MATLAB Use nested for loops to perform the following exercises. (a) Create the 3-D function: T(x,y,z) = x2 + y2 + z2 in a rectangular domain with x = [-2:0.2:2] MATLAB Use nested for loops to perform the following exercises. (a) Create the 3-D function: T(x,y,z) = x2 + y2 + z2 in a rectangular domain with x = [-2:0.2:2]](/WebImages/18/matlab-use-nested-for-loops-to-perform-the-following-exercis-1035464-1761537204-1.webp)