Black Lives Matter. Please consider donating to Black Girls Code today.

# 3D Surface Plots in MATLAB®

How to make 3D surface plots in MATLAB®.

### MATLAB® 3d Peak Function

[X,Y,Z] = peaks(25);
figure
surf(X,Y,Z);
fig2plotly()


### 3d Polar Coordinates

%Add a grid of polar coordinates
theta = (0:5:360)*pi/180;
rho = 0:.05:1;
[TH,R] = meshgrid(theta,rho);

%Transform the polar coordinate data into
%2 dimensional Cartesian (xy) coordinates
[X,Y] = pol2cart(TH,R);

%Generate the complex matrix Z
Z = X + 1i*Y;

%Dispay a surface of the mathematical function
f = (Z.^4-1).^(1/4);

%Create figure
fig = figure;
surf(X,Y,abs(f))
colormap winter

%Send to Plotly!
response = fig2plotly(fig, 'strip', true, ...
'filename', 'PolarCoor', ...
'fileopt','overwrite');


### Quadratic surface in its General Form

gv = linspace(-30,30,50); % adjust for appropriate domain
[xx yy zz]=meshgrid(gv, gv, gv);
F = xx.^2 + 2*yy.^2 + 0.5*zz.^2 + .4*xx.*yy + .5*xx.*zz + .6*yy.*zz + 7*xx + 8*yy + 9*zz - 100;

fig = figure
isosurface(xx, yy, zz, F, 0)

resp = fig2plotly(fig,'strip',false)
plotly_url = resp.url
Inspired from Stack Overflow

### Plot a Function of two variables

% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

f = @(x, y) real(atan(x + i*y));
ezsurf(f);
title('Function of two variables');

response = fig2plotly;
plotly_url = response.url;
Inspired from Matlab Docs.

### Klein Bottle

Download kleinBottle.m from <a href="http://www.mathworks.com/matlabcentral/fileexchange/5880-klein-bottle/content//kleinBottle.m" target="_blank">MATLAB<sup>&reg;</sup> File Exchange</a>

[x, y, z] = klein();
h = surf(x, y, z);
p = fig2plotly(h);


### Plot a 3d Implicit Function

% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

z = 0:4*pi; % <-- Or whatever range you want for z
t = 0:2*pi;
[Z,T] = meshgrid(z,t);
X = sin(Z).*cos(T);
Y = sin(Z).*sin(T);

surf(X,Y,Z)
response = fig2plotly;
plotly_url = response.url;

MATLAB is a registered trademark of The MathWorks, Inc.