Hey I'm teaching a calculus course, and for an example in my lecture on surface integrals I would like to generate a surface plot in MATLAB of a portion of a circular cylinder. Here is my MATLAB code for the case where the parameter domain is rectangular:
clc; clear
syms theta z
x(theta,z) = cos(theta)
y(theta,z) = sin(theta)
z(theta,z) = z
fsurf(x, y, z, [0 2*pi 0 1])
However, the surfaced required for the problem instead has 0<z<1+sin(theta)
I'm not sure what is the best way to modify this code for the case where the domain is not rectangular. Thanks!
You could continue to use a rectangular domain, but use a dummy variable t
and a mapping to your z
:
syms theta t
x(theta,t) = cos(theta);
y(theta,t) = sin(theta);
z(theta,t) = t .* (1 + sin(theta));
fsurf(x, y, z, [0 2*pi 0 1])
You can do it numerically with the same result:
theta = linspace(0, 2*pi, 100);
t = linspace(0, 1, 50)';
X = repmat(cos(theta), length(t), 1);
Y = repmat(sin(theta), length(t), 1);
Z = t * (1 + sin(theta));
surf(X, Y, Z)
To avoid introducing t
, you can do it by component with the same result:
theta = linspace(0, 2*pi, 100);
Nz = 50;
Z = nan(numel(theta),Nz);
for ntheta = 1:numel(theta)
Z(ntheta,:) = linspace(0,1+sin(theta(ntheta)),Nz);
end
THETA = repmat(theta',1,Nz);
X = cos(THETA);
Y = sin(THETA);
surf(X, Y, Z)
The previous option is just a vectorized version of this.