How do I plot 2D slices (contours) of a 4D function in matlab? I am not able to use slice, as my function f is symbolic. The function is a 3D sine fourier transform where the fourier coefficients are chosen randomly from a probability distribution.
r=zeros(10,10,10);
for k1=1:10
for k2=1:10
for k3=1:10
r(k1,k2,k3)=rand./(sqrt(2)*(k1.^2+k2.^2+k3.^2).^1/4);
end
end
end
syms x1 x2 x3;
f=0;
for n1=1:10
for n2=1:10
for n3=1:10
f=f+r(n1,n2,n3).*sin((pi/5)*(n1.*x1))*sin((pi/5)*(n2.*x2))*sin((pi/5)*(n3.*x3));
end
end
end
contourf(f)
You are dealing with very very long symbolic equations, so this will take heavy computational time.
An approach to do this is evaluating the contours numerically. For that, you need to numerically compute 1 slice, and then you can easily contour. However your function is very computationally heavy.
Taking your example, you can (for convenience) do:
f2=@(a,b,c)(double(subs(f,[x1 x2 x3],[a b c])));
now f2(a,b,c)
will give us the numeric value of the function f
at point [x1 x2 x3]=[a b c]
.
now we can simply:
% create a domain of interest with desired steps
[x1,x2]=meshgrid(-1:0.1:1,-1:0.2:1);
%choose a z value
z=1;
% evaluate the function on the chosen domain
slc=zeros(size(x1));
for jj=1:size(x1,1)
for kk=1:size(x1,2)
slc(jj,kk)=f2(x1(jj,kk),x2(jj,kk),z);
end
end
% plot
contour(x1,x2,slc,10)
If you add to your code rng(1)
in the beginning, you should see this output:
You should be able to modify this code easily to add different values/ranges/dimensions for your slice.
Note that what you have is volumetric data, which can be argued about it being 4D data or not. However, if you do have it numerically (instead of symbolically), you should have a look at other volumetric data visualization techniques here.