Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168742
Image: ubuntu2004
Log Worksheet
var('a, x') f(x)=a*x^3 print(type(f)) print(f) show(f) print(f(2, a=5)) print type(f(2,a=5))
<type 'sage.symbolic.expression.Expression'> x |--> a*x^3
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ a x^{3}
40 <type 'sage.symbolic.expression.Expression'>
g(x)= derivative(f, x) show(g) g(x=2,a=3)
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ 3 \, a x^{2}
36
f(x)=a*x^3
f
x |--> a*x^3
f(x)=a*x^3 f
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ a x^{3}
var('x') sinc(x)=sin(x)/x plot(sinc, (x, -10,10))
plot(sinc, x, xmin=-15, xmax=15, thickness=2, color='red', legend_label='sinc')
p1=plot(sin, (-2*pi, 2*pi), thickness=2.0, rgbcolor=(0.5,1,0),legend_label='sin(x)') p2=plot(cos, (-2*pi, 2*pi), thickness=3.0, color='purple', alpha=0.5, legend_label='cos(x)') plt=p1+p2 plt.axes_labels(['x', 'f(x)']) show(plt)
var('t') pp=parametric_plot((cos(t), sin(t)), (t, 0, 2*pi), fill=True, fillcolor='red') pp.show(aspect_ratio=1, figsize=(3,3), frame=True)sp
var('u, v') f1=(4+(3+cos(v))*sin(u), 4+(3+cos(v))*cos(u), 4+sin(v)) f2=(8+(3+cos(v))*cos(u), 3+sin(v), 4 + (3+cos(v))*sin(u)) p1=parametric_plot3d(f1,(u, 0, 2*pi), (v, 0, 2*pi), texture="red") p2=parametric_plot3d(f2, (u, 0, 2*pi), (v, 0, 2*pi), texture="blue") combination=p1+p2 combination.show()
sphere?

File: /sagenb/sage_install/sage-4.7/local/lib/python2.6/site-packages/sage/plot/plot3d/shapes2.py

Type: <type ‘function’>

Definition: sphere(center=(0, 0, 0), size=1, **kwds)

Docstring:

Return a plot of a sphere of radius size centered at (x,y,z).

INPUT:

  • (x,y,z) - center (default: (0,0,0)
  • size - the radius (default: 1)

EXAMPLES: A simple sphere:

sage: sphere()

Two spheres touching:

sage: sphere(center=(-1,0,0)) + sphere(center=(1,0,0), aspect_ratio=[1,1,1])

Spheres of radii 1 and 2 one stuck into the other:

sage: sphere(color='orange') + sphere(color=(0,0,0.3), \
             center=(0,0,-2),size=2,opacity=0.9)

We draw a transparent sphere on a saddle.

sage: u,v = var('u v')
sage: saddle = plot3d(u^2 - v^2, (u,-2,2), (v,-2,2))
sage: sphere((0,0,1), color='red', opacity=0.5, aspect_ratio=[1,1,1]) + saddle

TESTS:

sage: T = sage.plot.plot3d.texture.Texture('red')
sage: S = sphere(texture=T)
sage: T in S.texture_set()
True
var('x, y, z') f=x^2+y^2+z^2 p=plot3d(f, (-5,5), (-5, 5), adaptive=False) p.show()
/sagenb/sage_install/sage-4.7/local/lib/python2.6/site-packages/sage/plot/plot3d/parametric_plot3d.py:538: DeprecationWarning: Unnamed ranges for more than one variable is deprecated and will be removed from a future release of Sage; you can used named ranges instead, like (x,0,2) g, ranges = setup_for_eval_on_grid(f, [urange,vrange], plot_points)
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_49.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyKCd4LCB5LCB6JykKZj14XjIreV4yK3peMgpwPXBsb3QzZChmLCAoLTUsNSksICgtNSwgNSksIGFkYXB0aXZlPUZhbHNlKQpwLnNob3coKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmpxOxu8D/___code___.py", line 6, in <module> exec compile(u'p.show()' + '\n', '', 'single') File "", line 1, in <module> File "base.pyx", line 1114, in sage.plot.plot3d.base.Graphics3d.show (sage/plot/plot3d/base.c:10294) File "base.pyx", line 829, in sage.plot.plot3d.base.Graphics3d._prepare_for_jmol (sage/plot/plot3d/base.c:6835) File "base.pyx", line 844, in sage.plot.plot3d.base.Graphics3d._box_for_aspect_ratio (sage/plot/plot3d/base.c:7345) File "base.pyx", line 214, in sage.plot.plot3d.base.Graphics3d._safe_bounding_box (sage/plot/plot3d/base.c:3529) File "parametric_surface.pyx", line 318, in sage.plot.plot3d.parametric_surface.ParametricSurface.bounding_box (sage/plot/plot3d/parametric_surface.c:2991) File "parametric_surface.pyx", line 366, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3521) File "parametric_surface.pyx", line 361, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3451) File "parametric_surface.pyx", line 580, in sage.plot.plot3d.parametric_surface.ParametricSurface.eval_grid (sage/plot/plot3d/parametric_surface.c:5222) File "wrapper_rdf.pyx", line 73, in sage.ext.interpreters.wrapper_rdf.Wrapper_rdf.__call__ (sage/ext/interpreters/wrapper_rdf.c:1290) ValueError
var('x y') f(x, y, z)= x^2+y^2+z^2 f.show()
\newcommand{\Bold}[1]{\mathbf{#1}}\left( x, y, z \right) \ {\mapsto} \ x^{2} + y^{2} + z^{2}
implicit_plot?

File: /sagenb/sage_install/sage-4.7/local/lib/python2.6/site-packages/sage/misc/decorators.py

Type: <type ‘function’>

Definition: implicit_plot(*args, **kwds)

Docstring:

implicit_plot takes a function of two variables, f(x,y) and plots the curve f(x,y) = 0 over the specified xrange and yrange as demonstrated below.

implicit_plot(f, (xmin, xmax), (ymin, ymax), ...)

implicit_plot(f, (x, xmin, xmax), (y, ymin, ymax), ...)

INPUT:

  • f – a function of two variables or equation in two variables
  • (xmin, xmax) – 2-tuple, the range of x values or (x,xmin,xmax)
  • (ymin, ymax) – 2-tuple, the range of y values or (y,ymin,ymax)

The following inputs must all be passed in as named parameters:

  • plot_points – integer (default: 150); number of points to plot in each direction of the grid
  • fill – boolean (default: False); if True, fill the region f(x,y) < 0.
  • linewidth – integer (default: None), if a single integer all levels will be of the width given, otherwise the levels will be plotted with the widths in the order given.
  • linestyle – string (default: None), the style of the line to be plotted, one of: solid, dashed, dashdot or dotted.
  • color – string (default: blue), the color of the plot. Colors are defined in sage.plot.colors; try colors? to see them all.
  • legend_label – the label for this item in the legend

EXAMPLES:

A simple circle with a radius of 2. Note that since the input function is an expression, we need to explicitly declare the variables in 3-tuples for the range:

sage: var("x y")
(x, y)
sage: implicit_plot(x^2+y^2-2, (x,-3,3), (y,-3,3)).show(aspect_ratio=1)

I can do the same thing, but using a callable function so I don’t need to explicitly define the variables in the ranges, and filling the inside:

sage: x,y = var('x,y')
sage: f(x,y) = x^2 + y^2 - 2
sage: implicit_plot(f, (-3, 3), (-3, 3),fill=True).show(aspect_ratio=1)

The same circle but with a different line width:

sage: implicit_plot(f, (-3,3), (-3,3), linewidth=6).show(aspect_ratio=1)

And again the same circle but this time with a dashdot border:

sage: implicit_plot(f, (-3,3), (-3,3), linestyle='dashdot').show(aspect_ratio=1)

You can also plot an equation:

sage: var("x y")
(x, y)
sage: implicit_plot(x^2+y^2 == 2, (x,-3,3), (y,-3,3)).show(aspect_ratio=1)

You can even change the color of the plot:

sage: implicit_plot(x^2+y^2 == 2, (x,-3,3), (y,-3,3), color="red").show(aspect_ratio=1)

Here is a beautiful (and long) example which also tests that all colors work with this:

sage: G = Graphics()
sage: counter = 0
sage: for col in colors.keys(): # long time
...       G += implicit_plot(x^2+y^2==1+counter*.1, (x,-4,4),(y,-4,4),color=col)
...       counter += 1
sage: G.show(aspect_ratio=1,frame=False)

We can define a level-n approximation of the boundary of the Mandelbrot set:

sage: def mandel(n):
...       c = polygen(CDF, 'c')
...       z = 0
...       for i in range(n):
...           z = z*z + c
...       def f(x, y):
...           val = z(CDF(x, y))
...           return val.norm() - 4
...       return f

The first-level approximation is just a circle:

sage: implicit_plot(mandel(1), (-3, 3), (-3, 3)).show(aspect_ratio=1)

A third-level approximation starts to get interesting:

sage: implicit_plot(mandel(3), (-2, 1), (-1.5, 1.5)).show(aspect_ratio=1)

The seventh-level approximation is a degree 64 polynomial, and implicit_plot does a pretty good job on this part of the curve. (plot_points=200 looks even better, but it takes over a second.)

sage: implicit_plot(mandel(7), (-0.3, 0.05), (-1.15, -0.9),plot_points=50).show(aspect_ratio=1)
implicit_plot3d?

File: /sagenb/sage_install/sage-4.7/local/lib/python2.6/site-packages/sage/plot/plot3d/implicit_plot3d.py

Type: <type ‘function’>

Definition: implicit_plot3d(f, xrange, yrange, zrange, **kwds)

Docstring:

Plots an isosurface of a function.

INPUT:

  • f - function
  • xrange - a 2-tuple (x_min, x_max) or a 3-tuple (x, x_min, x_max)
  • yrange - a 2-tuple (y_min, y_may) or a 3-tuple (y, y_min, y_may)
  • zrange - a 2-tuple (z_min, z_maz) or a 3-tuple (z, z_min, z_maz)
  • plot_points - (default: “automatic”, which is 50) the number of function evaluations in each direction. (The number of cubes in the marching cubes algorithm will be one less than this). Can be a triple of integers, to specify a different resolution in each of x,y,z.
  • contour - (default: 0) plot the isosurface f(x,y,z)==contour. Can be a list, in which case multiple contours are plotted.
  • region - (default: None) If region is given, it must be a Python callable. Only segments of the surface where region(x,y,z) returns a number >0 will be included in the plot. (Note that returning a Python boolean is acceptable, since True == 1 and False == 0).

EXAMPLES:

sage: var('x,y,z')
(x, y, z)

A simple sphere:

sage: implicit_plot3d(x^2+y^2+z^2==4, (x, -3, 3), (y, -3,3), (z, -3,3))

A nested set of spheres with a hole cut out:

sage: implicit_plot3d((x^2 + y^2 + z^2), (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=[1,3,5], \
...                   region=lambda x,y,z: x<=0.2 or y>=0.2 or z<=0.2).show(viewer='tachyon')

A very pretty example from http://iat.ubalt.edu/summers/math/platsol.htm:

sage: T = RDF(golden_ratio)
sage: p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))
sage: r = 4.77
sage: implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=40).show(viewer='tachyon')

As I write this (but probably not as you read it), it’s almost Valentine’s day, so let’s try a heart (from http://mathworld.wolfram.com/HeartSurface.html)

sage: p = (x^2+9/4*y^2+z^2-1)^3-x^2*z^3-9/(80)*y^2*z^3
sage: r = 1.5
sage: implicit_plot3d(p, (x, -r,r), (y, -r,r), (z, -r,r), plot_points=80, color='red', smooth=False).show(viewer='tachyon')

The same examples also work with the default Jmol viewer; for example:

sage: T = RDF(golden_ratio)
sage: p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))
sage: r = 4.77
sage: implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=40).show()

Here we use smooth=True with a Tachyon graph:

sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, smooth=True)

We explicitly specify a gradient function (in conjunction with smooth=True) and invert the normals:

sage: gx = lambda x, y, z: -(2*x + y^2 + z^2)
sage: gy = lambda x, y, z: -(x^2 + 2*y + z^2)
sage: gz = lambda x, y, z: -(x^2 + y^2 + 2*z)
sage: implicit_plot3d(x^2+y^2+z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, \
...       plot_points=40, smooth=True, gradient=(gx, gy, gz)).show(viewer='tachyon')

A graph of two metaballs interacting with each other:

sage: def metaball(x0, y0, z0): return 1 / ((x-x0)^2 + (y-y0)^2 + (z-z0)^2)
sage: implicit_plot3d(metaball(-0.6, 0, 0) + metaball(0.6, 0, 0), (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=2)

MANY MORE EXAMPLES:

A kind of saddle:

sage: implicit_plot3d(x^3 + y^2 - z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=0)

A smooth surface with six radial openings:

sage: implicit_plot3d(-(cos(x) + cos(y) + cos(z)), (x, -4, 4), (y, -4, 4), (z, -4, 4))

A cube composed of eight conjoined blobs:

sage: implicit_plot3d(x^2 + y ^2 + z^2 +cos(4*x)+cos(4*y)+cos(4*z)-0.2, (x, -2, 2), (y, -2, 2), (z, -2, 2))

A variation of the blob cube featuring heterogeneously sized blobs:

sage: implicit_plot3d(x^2 + y ^2 + z^2 +sin(4*x) + sin(4*y) + sin(4*z) -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))

A klein bottle:

sage: implicit_plot3d((x^2+y^2+z^2+2*y-1)*((x^2+y^2+z^2-2*y-1)^2-8*z^2)+16*x*z*(x^2+y^2+z^2-2*y-1), (x, -3, 3), (y, -3.1, 3.1), (z, -4, 4))

A lemniscate:

sage: implicit_plot3d(4*x^2*(x^2+y^2+z^2+z)+y^2*(y^2+z^2-1), (x, -0.5, 0.5), (y, -1, 1), (z, -1, 1))

Drope:

sage: implicit_plot3d(z - 4*x*exp(-x^2-y^2), (x, -2, 2), (y, -2, 2), (z, -1.7, 1.7))

A cube with a circular aperture on each face:

sage: implicit_plot3d(((1/2.3)^2 *(x^2 + y^2 + z^2))^-6 + ( (1/2)^8 * (x^8 + y^8 + z^8) )^6 -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))

A simple hyperbolic surface:

sage: implicit_plot3d(x*x + y - z*z, (x, -1, 1), (y, -1, 1), (z, -1, 1))

A hyperboloid:

sage: implicit_plot3d(x^2 + y^2 - z^2 -0.3, (x, -2, 2), (y, -2, 2), (z, -1.8, 1.8))

Duplin cycloid:

sage: implicit_plot3d((2^2 - 0^2 - (2 + 2.1)^2) * (2^2 - 0^2 - (2 - 2.1)^2)*(x^4+y^4+z^4)+ 2*((2^2 - 0^2 - (2 + 2.1)^2 )*(2^2 - 0^2 - (2 - 2.1)^2)* (x^2 * y^2+x^2 * z^2+y^2 * z^2))+2* 2^2 *((-0^2-2^2+2^2+2.1^2)* (2 *x *2+2* y* 0-2^2)-4*0 *2.1^2 *y)*(x^2+y^2+z^2)+ 4 * 2^4 * (2 *x+0 *y)* (-2^2+0 * y+2 * x)+4* 2^4 * 2.1^2 * y^2+2^8, (x, -2, 2.2), (y, -2, 2), (z, -1.3, 1.3))

Sinus:

sage: implicit_plot3d(sin(pi*((x)^2+(y)^2))/2 +z, (x, -1, 1), (y, -1, 1), (z, -1, 1))

A torus:

sage: implicit_plot3d((sqrt(x*x+y*y)-3)^2 + z*z - 1, (x, -4, 4), (y, -4, 4), (z, -1, 1))

An octahedron:

sage: implicit_plot3d(abs(x)+abs(y)+abs(z) - 1, (x, -1, 1), (y, -1, 1), (z, -1, 1))

A cube:

sage: implicit_plot3d(x^100 + y^100 + z^100 -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))

Toupie:

sage: implicit_plot3d((sqrt(x*x+y*y)-3)^3 + z*z - 1, (x, -4, 4), (y, -4, 4), (z, -6, 6))

A cube with rounded edges:

sage: implicit_plot3d(x^4 + y^4 + z^4 - (x^2 + y^2 + z^2), (x, -2, 2), (y, -2, 2), (z, -2, 2))

Chmutov:

sage: implicit_plot3d(x^4 + y^4 + z^4 - (x^2 + y^2 + z^2-0.3), (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1.5, 1.5))

Further Chutmov:

sage: implicit_plot3d(2*(x^2*(3-4*x^2)^2+y^2*(3-4*y^2)^2+z^2*(3-4*z^2)^2) -3, (x, -1.3, 1.3), (y, -1.3, 1.3), (z, -1.3, 1.3))

Clebsch:

sage: implicit_plot3d(81*(x^3+y^3+z^3)-189*(x^2*y+x^2*z+y^2*x+y^2*z+z^2*x+z^2*y) +54*x*y*z+126*(x*y+x*z+y*z)-9*(x^2+y^2+z^2)-9*(x+y+z)+1, (x, -1, 1), (y, -1, 1), (z, -1, 1))

Looks like a water droplet:

sage: implicit_plot3d(x^2 +y^2 -(1-z)*z^2, (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1, 1))

Sphere in a cage:

sage: implicit_plot3d((x^8 + z^30 + y^8 - (x^4 + z^50 + y^4 -0.3))*(x^2 + y^2 + z^2 -0.5), (x, -1.2, 1.2), (y, -1.3, 1.3), (z, -1.5, 1.5))

Ortho circle:

sage: implicit_plot3d(((x^2 + y^2 - 1)^2 + z^2)* ((y^2 + z^2 - 1)^2 + x^2)* ((z^2 + x^2 - 1)^2 + y^2) - 0.075^2 *(1 + 3* (x^2 + y^2 + z^2)), (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1.5, 1.5))

Cube sphere:

sage: implicit_plot3d(12 - ((1/2.3)^2 *(x^2 + y^2 + z^2))^-6 - ( (1/2)^8 * (x^8 + y^8 + z^8) )^6, (x, -2, 2), (y, -2, 2), (z, -2, 2))

Two cylinders intersect to make a cross:

sage: implicit_plot3d((x^2 + y^2 - 1) * ( x^2 + z^2 - 1) - 1, (x, -3, 3), (y, -3, 3), (z, -3, 3))

Three cylinders intersect in a similar fashion:

sage: implicit_plot3d((x^2 + y^2 - 1) * ( x^2 + z^2 - 1)* ( y^2 + z^2 - 1) - 1, (x, -3, 3), (y, -3, 3), (z, -3, 3))

A sphere-ish object with twelve holes, four on each XYZ plane:

sage: implicit_plot3d(3*(cos(x) + cos(y) + cos(z)) + 4* cos(x) * cos(y) * cos(z), (x, -3, 3), (y, -3, 3), (z, -3, 3))

A gyroid:

sage: implicit_plot3d(cos(x) * sin(y) + cos(y) * sin(z) + cos(z) * sin(x), (x, -4, 4), (y, -4, 4), (z, -4, 4))

Tetrahedra:

sage: implicit_plot3d((x^2 + y^2 + z^2)^2 + 8*x*y*z - 10*(x^2 + y^2 + z^2) + 25, (x, -4, 4), (y, -4, 4), (z, -4, 4))

TESTS:

Test a separate resolution in the X direction; this should look like a regular sphere:

sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=(10, 40, 40), contour=4)

Test using different plot ranges in the different directions; each of these should generate half of a sphere. Note that we need to use the aspect_ratio keyword to make it look right with the unequal plot ranges:

sage: implicit_plot3d(x^2 + y^2 + z^2, (x, 0, 2), (y, -2, 2), (z, -2, 2), contour=4, aspect_ratio=1)

sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, 0, 2), (z, -2, 2), contour=4, aspect_ratio=1)

sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, 0, 2), contour=4, aspect_ratio=1)

Extra keyword arguments will be passed to show():

sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, viewer='tachyon')

An implicit plot that doesn’t include any surface in the view volume produces an empty plot:

sage: implicit_plot3d(x^2 + y^2 + z^2 - 5000, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6)
implicit_plot3d(x^2+y^2+z^2==9, (x, -5, 5), (y,-5, 5), (z,-5,5),color='red', contour=[1, 3, 5], mesh=True)
implicit_plot3d(x^2+y^2+z^2==5, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=[1,3,5])