{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# 3D Plots in Sage on CoCalc\n", "\n", "https://doc.sagemath.org/html/en/reference/plot3d/index.html" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" } ], "source": [ "var('x y')\n", "k = 2\n", "plot3d(4 * sin(x) * cos(y) * exp(-0.1 * x^2), (x, -k*pi, k*pi), (y, -k*pi, k*pi), mesh=True)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## implicit plot" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 2, "metadata": { }, "output_type": "execute_result" } ], "source": [ "var('x, y, z')\n", "T = RDF(golden_ratio)\n", "F = 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))\n", "r = 4.77\n", "implicit_plot3d(F, (x,-r,r), (y,-r,r), (z,-r,r), plot_points=40, color='darkkhaki')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### parametric plot with limits on the value (z-limit) via implicit plots" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 3, "metadata": { }, "output_type": "execute_result" } ], "source": [ "var('a k')\n", "f(a, k) = a * e^k\n", "implicit_plot3d(f(x, y) - z, (x,0,10), (y,0,10), (z, 0, 10))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Revolutions" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "a torus by rotating a cicle with offset" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 4, "metadata": { }, "output_type": "execute_result" } ], "source": [ "var('u')\n", "circle = (cos(u), sin(u))\n", "revolution_plot3d(circle, (u,0,2*pi), axis=(2,1), show_curve=True, opacity=0.5).show(aspect_ratio=(1,1,1))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 5, "metadata": { }, "output_type": "execute_result" } ], "source": [ "var('u')\n", "line = u\n", "parabola = u^2\n", "sur1 = revolution_plot3d(line, (u,0,1), opacity=0.5, rgbcolor=(1,0.5,0), show_curve=True, parallel_axis='x')\n", "sur2 = revolution_plot3d(parabola, (u,0,1), opacity=0.5, rgbcolor=(0,1,0), show_curve=True, parallel_axis='x')\n", "(sur1+sur2).show()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" } ], "source": [ "u = var('u')\n", "f = u^2\n", "revolution_plot3d(f, (u,0,2), axis=(1,0.2), show_curve=True, opacity=0.5, rgbcolor=(0,1,0)).show(aspect_ratio=(1,1,1))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Parametric plots" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" } ], "source": [ "var('u,v')\n", "parametric_plot3d((cos(u), 2*sin(u)+cos(v), sin(v)), (u,0,2*pi), (v,-pi,pi), mesh=True)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## 3d vector field" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 8, "metadata": { }, "output_type": "execute_result" } ], "source": [ "x,y,z=var('x y z')\n", "plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## 3D list plots" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": "\n\n", "text/plain": [ "Graphics3d Object" ] }, "execution_count": 9, "metadata": { }, "output_type": "execute_result" } ], "source": [ "pi = float(pi)\n", "m = matrix(RDF, 6, [sin(i^2 + j^2) for i in [0,pi/5,..,pi] for j in [0,pi/5,..,pi]])\n", "list_plot3d(m, color='yellow', frame_aspect_ratio=[1, 1, 1/3], num_points=40, mesh=True, interpolation_type='clough')" ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 9.0", "language": "sagemath", "metadata": { "cocalc": { "description": "Open-source mathematical software system", "priority": 1, "url": "https://www.sagemath.org/" } }, "name": "sage-9.0" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }