{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Partial derivatives\n", "\n", "SageMath code for animating a geometric view of partial derivatives.\n", "\n", "Cavalcanti, R. T." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plane and axis" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "E. = EuclideanSpace(3)\n", "xy_plane = E.default_chart().plot(fixed_coords={z: 0}, color='gray',style='--',thickness=1.5, label_axes= False, number_values=4, step=5)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def gen_axis(origin=(0,0,0), size=1):\n", " v0 = vector(origin)\n", " vx = vector([size,0,0])\n", " vy = vector([0,size,0])\n", " vz = vector([0,0,size])\n", " vectors = sum([v.plot(color='gray',frame=False,thickness=size, start=v0) for v in [vx,vy,vz]])\n", " labels = text3d('x',1.1*(vx+v0),fontsize= str(11.5*size)+'%')\n", " labels += text3d('y',1.1*(vy+v0),fontsize= str(11.5*size)+'%')\n", " labels += text3d('z',1.1*vz+v0,fontsize= str(11.5*size)+'%')\n", " return vectors+labels" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "axis_plane = xy_plane+gen_axis(origin=(-8,-8,0),size=12)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "Graphics3d Object" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "axis_plane" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function for generating the graphics" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "f = 1/3*x^2-1/4*y^2+4\n", "my_colors = [colormaps.Set1(k)[:3] for k in range(5)]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def gen_graphics(pos_x,pos_y,partial_x = True):\n", " if partial_x:\n", " tangent = parametric_plot3d((x,pos_y,diff(f,x)(x=pos_x,y=pos_y)*(x-pos_x)+f(x=pos_x,y=pos_y)),\n", " (x,pos_x-2,pos_x+2),color='black', thickness=3)\n", "\n", " else:\n", " tangent = parametric_plot3d((pos_x,y,diff(f,y)(x=pos_x,y=pos_y)*(y-pos_y)+f(x=pos_x,y=pos_y)),\n", " (y,pos_y-2,pos_y+2),color='blue', thickness=3)\n", " surf = plot3d(f,(-4,4),(-4,4),frame=True,mesh=True, color=my_colors[1],plot_points=30, alpha=.7)\n", " plane_y = parametric_plot3d((pos_x,y,z+7),(y,-6,6),(z,-12,6),color=my_colors[4], alpha=.25, plot_points=2)\n", " curve_y = parametric_plot3d((pos_x,y,f(x=pos_x)+.01),(y,-4,4),color=my_colors[4], thickness=4)\n", " plane_x = parametric_plot3d((x,pos_y,z+7),(x,-6,6),(z,-12,6),color='gray', alpha=.5, plot_points=2)\n", " curve_x = parametric_plot3d((x,pos_y,f(y=pos_y)+.01),(x,-4,4),color='gray', thickness=4)\n", " point = sphere((pos_x,pos_y,f(x=pos_x,y=pos_y)),size=.2,color='red')\n", " return surf+plane_x+plane_y+curve_x+curve_y+tangent+point+axis_plane" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "Graphics3d Object" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gen_graphics(-2,1.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Animation" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "vmin,vmax = -2,2\n", "step = .5" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "path1 = [gen_graphics(k,vmin) for k in srange(vmin,vmax,step,include_endpoint=True)]\n", "path2 = [gen_graphics(vmax,k,partial_x = False) for k in srange(vmin,vmax,step,include_endpoint=True)]\n", "path3 = [gen_graphics(k,vmax) for k in srange(vmax,vmin,-step,include_endpoint=True)]\n", "path4 = [gen_graphics(vmin,k,partial_x = False) for k in srange(vmax,vmin,-step,include_endpoint=True)]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "frames = path1+path2+path3+path4" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "Graphics3d Object" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "animate(frames).interactive()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 9.3", "language": "sage", "name": "sagemath" }, "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.9.2" } }, "nbformat": 4, "nbformat_minor": 4 }