Answer:
(a) Following is the matlab code:
a=1; %% index variable for x
b=1; %% index variable for y
c=1; %% index variable for z
for x=-2:0.01:2 %% for loop goes through x
for y=-1:0.01:1 %% nested for loop goes through y
for z= 0:0.01:1 %% nested for loop goes through z
M(a,b,c)=exp(-(sin(2*x)*cos(4*y)*tanh(z))); %%Computing M
c=c+1;
end
b=b+1;
c=1;
end
a=a+1;
b=1;
c=1;
end
(b) v=mean(M(:)); %% (:) enables to calculate mean of all elements
(c) f=mean(mean(M, 1), 2); %% 1st along x, then along y
f=reshape(f,[size(f,3) 1]); %% reshaping into vector form
(d) g=mean(M,3); %%along z
(e) surf(g) %% surface plot
Explanation:
(a) We use three nested loops to compute M, 1st for loop goes over all values of x, second nested loop goes over y and third nested loop goes over z. We compute the expression in the third nested loop and then index it separately using variables a, b and c.
(b) To compute mean of all values we make use of (:) which enables to calculate mean of all elements in M.
(c) To compute along x-y plane, we first compute mean along x dimension and then along y dimension. Finally we reshape into vector form.
(d) We just mention the dimension 3 to compute mean along z dimension.
(e) We use surf function to plot g in part d.