3

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!

1 Answer 1

3

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.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Is there a way to do this without introducing the dummy variable? I plan on sharing the code with my students, and I think they might get confused by this, since it will look different to how we will be writing the parametrization on paper. I'm not married to the symbolic approach (I just do it out of habit), and I'm happy to use the usual numerical functions instead if needed .
See my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.