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

I want to create a shape that is a sphere on top of the 3D Gaussian.
something like this:
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?

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)

Related

How to generate a dome by using points in MATLAB

I am trying to generate a semi ellipsoidal dome shape by using x, y, z values. In my code below, I define the x,y,z values but I am unable to assign those values to sphere.
How do I resolve this?
clc
x = [65 55.2125 50.8267 46.7398 42.9232 39.3476 35.9815 32.7882 29.7175 26.6833 23.4690 18.7605];
y = x;
z = [0.0,0.9,2.7,5.2,8.2,11.8,15.8,20.3,25.2,30.7,37.1,47.5]; % max height of dome is 47.5
[x,y,z] = sphere(20);
x = x(12:end,:);
y = y(12:end,:);
z = z(12:end,:);
r = 65; % radius of the dome
surf(r.*x,r.*y,r.*z);
axis equal;
It will be simpler or at least more elegant to use the parametric equations of an ellipsoide.
% semi axis parameters
a = 65; % x-axis
b = 65; % y-axis
c = 47.5; % z-axis
%% Parametrisation
%
% To reach each point of the ellipsoide we need two angle:
% phi ∈ [0,2𝜋]
% theta ∈ [0, 𝜋]
%
% But since we only need half of an ellipsoide we can set
% theta ∈ [0,𝜋/2]
[theta,phi] = ndgrid(linspace(0,pi/2,50),linspace(0,2*pi,50));
x = a*sin(theta).*cos(phi);
y = b*sin(theta).*sin(phi);
z = c*cos(theta);
%plot
surf(x,y,z)
axis equal
Result:
You can remove the bottom half of the sphere by assigning NaNs to the approptiate elements of x and y:
[x,y,z] = sphere(20);
I = (z<0);
x(I) = NaN;
y(I) = NaN;
surf(x,y,z)

How do I plot a curved surface in matlab?

I am trying to make a cut out of a pipe and I want to make a curved surface to represent the outside of the pipe. However when I plot the surface I only get the diagonal of the surface instead of the surface itself. How can I fix this?
MWE:
r = 0:0.1:3;
z = 0:0.1:10;
[rr, zz] = meshgrid(r,z);
% set cut planes angles
theta1 = 0;
theta2 = pi*135/180;
nt = 101; % angle resolution
figure(1);
clf;
t3 = linspace(theta1, (theta2 - 2*pi), nt);
[rr3, tt3] = meshgrid(r,t3);
% Create curved surface
xx5 = r(end) * cos(tt3);
yy5 = r(end) * sin(tt3);
h5 = surface(xx5, yy5,zz)
The mesh-grid you created is based on theta and the radius. However, the radius is constant for the outside of the pipe so instead it should be based on theta and z since those are the two independent variables defining the grid. Based on this reasoning I believe the following is what you're after.
r = 0:0.1:3;
z = 0:0.1:10;
% set cut planes angles
theta1 = 0;
theta2 = pi*135/180;
nt = 101; % angle resolution
figure(1);
clf;
% create a grid over theta and z
t3 = linspace(theta1, (theta2 - 2*pi), nt);
[tt3, zz3] = meshgrid(t3, z);
% convert from cylindical to Cartesian coordinates
xx5 = r(end) * cos(tt3);
yy5 = r(end) * sin(tt3);
% plot surface
h5 = surface(xx5, yy5, zz3, 'EdgeColor', 'none');
% extra stuff to make plot prettier
axis vis3d
axis equal
view(3)
camzoom(0.7);
Try with surf with surf(xx5, yy5, zz). Is this what you are looking for?

How can I make a cylindrical 3D contour plot in Matlab?

I have an axisymmetric flow with m x n grid points in r and z direction and I want to plot the temperature that is stored in a matrix with size mxn in a 3D cylindrical plot as visualized in the link below (My reputation is not high enough to include it as a picture).
I have managed to plot it in 2D (r,z plane) using contour but I would like to add the theta plane for visualization. How can I do this?
You can roll your own with multiple calls to surface().
Key idea is: for each surface: (1) theta=theta1, (2) theta=theta2, (3) z=zmax, (4) z=0, (5) r=rmax, generate a 3D mesh (xx,yy,zz) and the temperature map on that mesh. So you have to think about how to construct each surface mesh.
Edit: completed code is now provided. All magic number and fake data are put at (almost) the top of the code so it's easy to convert it into a general purpose Matlab function. Good luck!
% I have adjusted the range values to show the curved cylinder wall
% display a variable temperature
r = 0:0.1:2.6; % you can also try r = 0:0.1:3.0
z = 0:0.1:10; % you can also try z = 0:0.1:15;
[rr, zz] = meshgrid(r,z);
% fake temperature data
temp = 100 + (10* (3-rr).^0.6) .* (1-((zz - 7.5)/7.5).^6) ;
% visualize in 2D
figure(1);
clf;
imagesc(r,z,temp);
colorbar;
% set cut planes angles
theta1 = 0;
theta2 = pi*135/180;
nt = 40; % angle resolution
figure(2);
clf;
xx1 = rr * cos(theta1);
yy1 = rr * sin(theta1);
h1 = surface(xx1,yy1,zz,temp,'EdgeColor', 'none');
xx2 = rr * cos(theta2);
yy2 = rr * sin(theta2);
h2 = surface(xx2,yy2,zz,temp,'EdgeColor', 'none');
% polar meshgrid for the top end-cap
t3 = linspace(theta1, (theta2 - 2*pi), nt);
[rr3, tt3] = meshgrid(r,t3);
xx3 = rr3 .* cos(tt3);
yy3 = rr3 .* sin(tt3);
zz3 = ones(size(rr3)) * max(z);
temp3 = zeros(size(rr3));
for k = 1:length(r)
temp3(:,k) = temp(end,k);
end
h3 = surface(xx3,yy3,zz3,temp3,'EdgeColor', 'none');
% polar meshgrid for the bottom end-cap
zz4 = ones(size(rr3)) * min(z);
temp4 = zeros(size(rr3));
for k = 1:length(r)
temp4(:,k) = temp(1,k);
end
h4 = surface(xx3,yy3,zz4,temp4,'EdgeColor', 'none');
% generate a curved meshgrid
[tt5, zz5] = meshgrid(t3,z);
xx5 = r(end) * cos(tt5);
yy5 = r(end) * sin(tt5);
temp5 = zeros(size(xx5));
for k = 1:length(z)
temp5(k,:) = temp(k,end);
end
h5 = surface(xx5, yy5, zz5,temp5,'EdgeColor', 'none');
axis equal
colorbar
view(125,25); % viewing angles

MATLAB: fitting a 3d surface to a bar graph

I have a matrix (about 400x400) of numbers between 0 and 1. Here is a plot of the 3D bar graph
I want to fit a 3D surface to the bar graph. Any ideas? The elements of the matrix (the numbers between 0 and 1) should give the height of the surface at each index. I would like the surface to just give us the general shape of the bar graph, and not go through every point.
% init
x = randn(1000,1);
y = randn(1000,1);
nbins = [10 20];
% make histogram
h = histogram2(x,y,nbins)
% set limits and steps
min_x = min(x); max_x = max(x); step_x = (max_x - min_x)/nbins(1);
min_y = min(y); max_y = max(y); step_y = (max_y - min_y)/nbins(2);
% make grid
surf_z = h.Values;
surf_x = [min_x + step_x/2 : step_x : max_x - step_x/2];
surf_y = [min_y + step_y/2 : step_y : max_y - step_y/2];
[xx, yy] = meshgrid(surf_x, surf_y)
% plot 3D surface
surf(xx',yy',surf_z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% other variant
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% make grid and interpolant
[xx, yy] = ndgrid(surf_x, surf_y)
F = griddedInterpolant(xx,yy,surf_z,'spline');
% make 3D surface with a some step value
step = 0.01;
[Xq,Yq] = ndgrid(min(surf_x):step:max(surf_x), min(surf_y):step:max(surf_y));
Zq = F(Xq,Yq);
% plot 3D surface
mesh(Xq,Yq,Zq);

Bending a plane into a closed surface/cylinder

I'm doing a mathematical experiment in Matlab and the result should be a circle in the x,y-plane. But sometimes, the circle starts spiraling. I'm now trying to bend the x,y-plane into a cylinder (as in the following picture). At the moment I only have the x and y coordinates of the points.
I've tried converting them into polar coordinates and then use some 'surf' commando's, but nothing works right now
(source: wtcoeselgem.be)
Edit: I've used the plot3 command, as suggested by Ander Biguri, resulting in the following figure.
(source: wtcoeselgem.be)
I will consider that you have a curve defined by x and y coordinates which you want to fold around a cylinder.
%% // Generate sample data
x = linspace(0,10*pi) ;
y2 = cos(x) ;
y1 = 10*cos(x/10) ;
y = y1+y2 ; y = y-min(y) ;
figure, plot(x,y,'-o') ;
This produces:
Next I define a basic cylinder, nothing original:
%% // Basic cylinder (just for background)
[Xc,Yc,Zc] = cylinder(1,100);
Zc = Zc * max(y) ;
hs = surf(Xc,Yc,Zc) ;
set(hs,'FaceColor',[.8 .8 .8],'FaceAlpha',0.5,'EdgeColor','none') ;
hold on
And here comes the interesting bit:
%% // Fold the points around the cylinder
Number_of_turn = 2 ;
xrange = [min(x),max(x)] ;
xspan = xrange(2)-xrange(1) ;
xc = x / xspan * 2*pi * Number_of_turn ;
Xp = cos(xc) ;
Zp = y ;
Yp = sin(xc) ;
hp = plot3(Xp,Yp,Zp,'-ok') ;
Which render:
For this example I assumed you wanted to wrap your curve around "2 turns" of the cylinder. This is easily changed with the Number_of_turn variable.
Note that you can also change the radius of the cylinder by multiplying the Xp and Yp coordinates by your radius.
The following seems to do more or less what you want
%// Data
xmin = -3;
xmax = 3; %// this piece will get folded into a cylinder
Rc = 5; %// cylinder radius
zmaxc = 5; %// cylinder max z
zminc = -5; %// cylinder min z
%// Spiral
t = linspace(0,1,1000);
r = 1+2*t;
theta = 2*pi*3*t;
x1 = r.*cos(theta);
y1 = r.*sin(theta); %// example spiral. Defined by x1, y1
%// Do the bending
z2 = y1;
phi = (x1-xmin)/(xmax-xmin)*2*pi;
x2 = Rc*cos(phi);
y2 = Rc*sin(phi);
%// Plot cylinder
[xc yc zc] = cylinder(Rc*ones(1,100),100);
zc = zminc + (zmaxc-zminc)*zc;
surf(xc,yc,zc)
shading flat
hold on
%// Plot bent spiral
plot3(x2,y2,z2, 'k.-');
Original spiral:
Two views of the result: