Search code examples
matlabmatlab-figure

how to plot a sphere on top of Gaussian 3D?


I want to create a shape that is a sphere on top of the 3D Gaussian.

something like this:

Sphere on 3D Gaussian

for plotting Gaussian I wrote tihs:



% isotropic Gaussian parameters
n = 100; % resolution
s = 2; % width

x = linspace(-5,5,n);
[X,Y] = meshgrid(x);

gaus2d = exp( -(X.^2 + Y.^2 )/(2*s^2));

figure(1), clf
surf(x,x,gaus2d)

and for sphere:


rotate3d on
hold on

[x1,y1,z1] = sphere;

% adjusting the radius of sphere 
x1 = x1*s;
y1 = y1*s;
z1 = z1*s;

surf(x1,y1,z1)

The problem is: I don't know how to shift the sphere on top of the Gaussian. How to transfer sphere on top of the Gaussain?


Solution

  • You can add a constant to the sphere's z-values in ordner to 'lift' it up:

    % isotropic Gaussian parameters
    n = 100; % resolution
    s = 2; % width
    
    x = linspace(-5,5,n);
    [X,Y] = meshgrid(x);
    
    gaus2d = exp( -(X.^2 + Y.^2 )/(2*s^2));
    
    figure(1), clf
    surf(x,x,gaus2d)
    
    rotate3d on
    hold on
    
    [x1,y1,z1] = sphere;
    
    % adjusting the radius of sphere 
    x1 = x1*s;
    y1 = y1*s;
    z1 = z1;
    
    % add a constant to sphere, so that it is on top of gauss
    addi = max(gaus2d(:)) - min(z1(:));
    z1 = z1 + addi;
    
    surf(x1,y1,z1)