# Parametric surface with the range of one variable depending on the other variable

I want to plot surface f(x,y) below

f(x, y) = x^2
x = r*cos(theta)
y = r*sin(theta)
(theta, 0, 2*pi)
(r, 0, 1 - cos(theta))


I tried this:

theta, r = var('theta r')
x = r*cos(theta)
y = r*sin(theta)
parametric_plot3d((x, y, x^2), (theta, 0, 2*pi), (r, 0, 1 - cos(theta)))


but it results in an error.

In Wolfram, i can plot this surface by this code below.

x = r*Cos[t]
y = r*Sin[t]

ParametricPlot3D[
{x, y, x^2},
{t, 0, 2*Pi},
{r, 0, 1 - Cos[t]}
]

edit retag close merge delete

Welcome to Ask Sage! Thank you for your question.

( 2022-07-07 13:44:41 +0200 )edit

Thanks for correcting the title :)

( 2022-07-07 14:36:38 +0200 )edit

Sort by » oldest newest most voted

Although @slelievre has solve the question, I would like to provide some additional solutions in terms of the plot3d function which may be helpful.

First solution

This is essentially a rewrite of @slelievre code by replacing parametric_plot3d by plot3d:

var("z,t,θ")
r = t*(1-cos(θ))
Cyl = (r*cos(θ),r*sin(θ),z)
plot3d(r^2*cos(θ)^2, (t,0,1), (θ,0,2*pi), transformation=Cyl,
plot_points=[10,120])


Please note that, in cylindrical coordinates, the surface is $z=r^2\cos(\theta)^2$.

Second solution

You can plot a function whose value is NaN outside the domain. The corresponding points will not appear in the graphic:

var("z,r,θ")
Cyl = (r*cos(θ),r*sin(θ),z)
def fun(r,θ):
if r<=1-cos(θ):
return r^2*cos(θ)^2
else:
return NaN
plot3d(fun, (r,0,2), (θ,0,2*pi), transformation=Cyl,
plot_points=[100, 300])


The problem of this approach is that the surface boundary appears too much jagged.

Third solution

You can plot the surface in a bigger domain and then remove the unwanted part:

var("x,y,z")
fig = plot3d(x^2, (x,-2,2), (y,-2,2))
cond = lambda x,y,z: sqrt(x^2+y^2) <= 1-cos(arctan2(y,x))
show(fig)


In cartesian coordinates, the surface is $z=x^2$. One has to keep only those points $(x,y,z)$ whose cylindrical coordinates $(r,\theta,z)$ satisfy the condition $r\leq 1-\cos(\theta)$, which can be expressed in cartesian coordinates as $$\sqrt{x^2+y^2}\leq 1-\cos(\operatorname{arctan2}(y,x)).$$ The three solutions can be seen in this SageMath cell, painted in red, green and blue.

more

Sage lacks support for plotting parametric surfaces $M(u, v)$ where the range of $v$ depends on the value of $u$.

However, a workaround is to use a change of variable.

For the example in the question, we can write $r = t(1 - \cos\theta)$.

Then, instead of varying $r$ in $[0, 1 - \cos(\theta)]$, vary $t$ in $[0, 1]$.

sage: t, theta = SR.var('r, theta')

sage: x = t*(1 - cos(theta)) * cos(theta)
sage: y = t*(1 - cos(theta)) * sin(theta)
sage: z = x^2
sage: xyz = (x, y, z)

sage: parametric_plot3d(xyz, (theta, 0, 2*pi), (t, 0, 1), plot_points=[360, 60])


more

Thank you for your answer. I could plot by your code and understand it needs to a little creative.

And thanks for the correcting the text. That is helpful (I'm a non-native).

( 2022-07-08 00:28:49 +0200 )edit