Surface

Surface

MATLAB code to generate some parametric surfaces.Equation of the surface is mentioned on the image. The color of the surface in image may differ from that generated by the given MATLAB code.

Mobius Strip

[u,v]=meshgrid(linspace(0,2*pi,250),linspace(-0.8,0.8,50));

t=1; %number of twist

surf(sin(u).*(2-v.*sin(t*u/2)),cos(u).*(2-v.*sin(t*u/2))...

,v.*cos(t*u/2),u...

,'edgecolor','none','facecolor',[0.5 0.5 0.5]);

lighting phong

material([0.4447 0.6154 0.7919])

camlight headlight

daspect([1 1 1])

Swallow Tail

[u,v]=meshgrid(-2:0.05:2,-0.8:0.05:0.8);

x= u.*v.^2 + 3*v.^4;

y= -2.*u.*v - 4*v.^3;

z= u;

surf(x,y,z...

,'facecolor',[0.5 0.76 0.1],'edgecolor','none');

lighting phong

camlight headlight,camlight left

material dull

axis off

daspect([1 1 1])

Bohemian Dome

[u,v]=meshgrid(-2*pi:0.05:2*pi,-2*pi:0.05:pi);

A=0.5;B=1.5;C=1;

surf(A*cos(u),B*cos(v)+A*sin(u),C*sin(v)...

,'facecolor',[0.1 0.67 0.89],'edgecolor','none');

camlight headlight,camlight left,camlight(1,-90)

material metal

axis off

daspect([1 1 1])

Enneper Surface

numPoint=50;

z = cplxgrid(100);

x=real(z);

y=imag(z);

r=1.1*abs(z);

phi=angle(z);

n =5;

x=r.* cos(phi)-r.^(2*n - 1).*cos((2*n - 1)*phi)./(2*n - 1);

y=r.* sin(phi)+r.^(2*n - 1).*sin((2*n - 1)*phi)./(2*n - 1);

z=2*r.^n.*cos(n.*phi)./n;

surf(x,y,z,phi)

%code for outer tube

rth=linspace(0,2.01*pi,300);

rradi = 1.1*ones(1,300);

rx=rradi.* cos(rth)-rradi.^(2*n - 1).*cos((2*n - 1)*rth)./(2*n - 1);

ry=rradi.* sin(rth)+rradi.^(2*n - 1).*sin((2*n - 1)*rth)./(2*n - 1);

rz=2*rradi.^n.*cos(n.*rth)./n;

cmap = makeColorMap(rand(1,3),rand(1,3),rand(1,3),128);

shading interp

colormap([cmap ; flipud(cmap)]);

tubeLines = {[rx' ry' rz']};

daspect([1 1 1])

h=streamtube(tubeLines,0.2,[1 70]);

set(h,'edgecolor','none','facecolor',[0.8 0.8 0.8]);

axis equal

lighting phong

camlight headlight

axis off

enneper_surface

Torus

[U,V]=meshgrid(linspace(-2*pi,2*pi,100));

x=(7+3*cos(V)).*cos(U);

y=(7+3*cos(V)).*sin(U);

z= 3*sin(V);

surf(x,y,z,'facelighting','phong','edgecolor','none','facecolor',[0.5 0.5 0.5]);

material([0.2259 0.5798 0.7604]);

camlight headlight,camlight left

axis off equal

Astroidal Ellipsoid

[u,v]=meshgrid(-2*pi:0.05:2*pi,-2*pi:0.05:2*pi);

surf(cos(u).^3.*cos(v).^3,sin(u).^3.*cos(v).^3,sin(v).^3 ...

,'facecolor',[0.87 0.2 0.78],'edgecolor','none');

lighting phong

material(0.4447 0.6154 0.7919)

camlight headlight,camlight left

axis off

Mobius Strip

[u,v]=meshgrid(linspace(0,2*pi,250),linspace(-0.8,0.8,50));

t=4; %twist

surf(sin(u).*(2-v.*sin(t*u/2)),cos(u).*(2-v.*sin(t*u/2))...

,v.*cos(t*u/2),u...

,'edgecolor','none','facecolor',[0.5 0.5 0.5]);

lighting phong

material([0.4447 0.6154 0.7919])

camlight headlight

daspect([1 1 1])

Water Drop

[u,v]=meshgrid(linspace(0,2*pi,100),linspace(-5,4.4444,100));

x =v.^2.*sqrt((4-0.9.*v)./2).*cos(u);

y =v.^2.*sqrt((4-0.9.*v)./2).*sin(u);

z =v;

surf(x,y,z,z,'edgecolor','none','facecolor',[0.3 0.5 0.9]);

camlight headlight,camlight(1,-90)

material([0.7 0.4 0.6]);

daspect([1 1 0.1])

Unknown

[x,y]=meshgrid(linspace(-5,5,100));

R=(x.*x+y.*y).^0.5;c=rand(1,3);

h=surf(x,y,1./(1+R).*(x.*sin(4*R)+y.*cos(4*R))...

,'facecolor',[0.4 0.7 0.8],'edgecolor','none');

lighting phong, camlight headlight,camlight left

material metal

axis off equal

Unknown

[u,v]=meshgrid(-0.1:0.02:2*pi);

surf(cos(u).*(-1 + 3*cos(v))...

,sin(u).*( 1 + 3*cos(v))...

,1.3* sin(v)...

,'facecolor',rand(1,3),'edgecolor','none');

camlight headlight,camlight left

material metal

daspect([1 1 1])

Unknown

[u,v]=meshgrid(-pi:0.03:pi,-pi:0.03:pi);

surf(u.*v.*sin(3*v),v,u.*v.*cos(3*v)...

,'facecolor',rand(1,3),'edgecolor','none');

camlight headlight,camlight left

material dull

Loxodrome

%% Loxodrome

% Script to generate loxodrome

%

% $$u = [-3\pi\ 3\pi]$$

%

% $$v = [0\ 3\pi]$$

%

% $$a = 1$$

%

% $$b = \sqrt{1+(au)^2}$$

%

% $$x = {sin(u+v)}/b$$

%

% $$y = {cos(u+v)}/b$$

%

% $$z = {-au}/b$$

%

% Define the range

num_points = 150;

u = linspace (-3*pi,3*pi, num_points);

v = linspace (0, pi/3, num_points);

[U, V] = meshgrid (u, v);

% Calculate x,y,z coordinates

a = 1;

b = sqrt(1 + (a.*U).^2);

x = sin(U + V)./b;

y = cos(U + V)./b;

z = -a.*U./b;

[f,v]=surf2patch(x, y, z);

cmap=jet(4);

%% Draw loxodrome

% In each iteration,vertices are rotated by pi/2 and drawn.

% Doing this will give 4 surfaces.

for k=1:4

[xd,yd]=transform2d(v(:,1),v(:,2),0,0,pi/2,0,0);

v(:,1)=xd;

v(:,2)=yd;

h=patch('vertices',v,'faces',f,...

'facecolor',cmap(k,:),'FaceLighting','phong',...

'edgecolor','none','facevertexcdata',[0.8 0.7 0.1],...

'BackFaceLighting','reverselit');

end

%% View and material setting

material([0.1 0.6 0.5 20 0])

camlight

lighting phong

light('Position',[0.5 -10 0.2])

light('Position',[-10 2 0.2])

set(gcf,'position',[5 5 1200 700])

set(gcf,'Renderer','zbuffer');

axis equal on tight,view(3)

% If export_fig function is available then this will work.

callbackStr='export_fig(''png'',''loxo1'')';

sld2Hndl=uicontrol( ...

'Style','pushbutton', ...

'Position',[10 10 40 20], ...

'Callback',callbackStr);

Knot

p=2;

q=5;

t=linspace(0,2*p*pi,500);

x=(2+cos(q/p*t)).*cos(t);

y=(2+cos(q/p*t)).*sin(t);

z=sin(q/p*t);

verts = {[x' y' z']};

h=streamtube(verts,0.5);

set(h,'Facecolor',[0.6072 0.6299 0.3705],'edgecolor','none');

lighting phong

material dull

camlight headlight

axis off equal

view(3)

p=2;q=9;

p=4;q=3;