2

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)
Lenol
  • 27
  • 8
  • 1
    Are you asking about this → [*Contour plot of a function of 3 variables*](https://stackoverflow.com/questions/36292327/contour-plot-of-a-function-of-3-variables) ? – Sardar Usama Apr 12 '18 at 08:09
  • @SardarUsama This isn't what I want. I am looking for 2D slices (XY plane for example) of the function. Also, the method in the solution wouldn't work since my function is symbolic. – Lenol Apr 12 '18 at 19:11
  • How do you expect to represent 4D or 3D (whatever you have) in XY plane? Please explain what your expected result would look like. – Sardar Usama Apr 12 '18 at 19:13
  • I want my result to look similar to this https://imgur.com/a/AVTXy – Lenol Apr 12 '18 at 19:37
  • How is that representing 3D or 4D in XY plane? That figure is representing 2D (not 3D or 4D) in XY Plane – Sardar Usama Apr 12 '18 at 20:15
  • If I understand correctly you don't want a [`contour3`](https://nl.mathworks.com/help/matlab/ref/contour3.html?s_tid=doc_ta), but a [`contour`](https://nl.mathworks.com/help/matlab/ref/contour.html?s_tid=doc_ta) plot? I'm afraid it's not possible unless you restrict one parameter be constant. A plot like [`contour3`](https://nl.mathworks.com/help/matlab/ref/contour3.html?s_tid=doc_ta) is possible though. – ViG Apr 13 '18 at 19:01

1 Answers1

3

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:

enter image description here

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.

Community
  • 1
  • 1
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120