{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Wheel on Inclined Plane" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib widget" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import FloatSlider" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import numpy" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from pysketcher import *\n", "\n", "def inclined_plane():\n", " theta = 30.\n", " L = 10.\n", " a = 1.\n", " xmin = 0\n", " ymin = -3\n", "\n", " drawing_tool.set_coordinate_system(xmin=xmin, xmax=xmin+1.5*L,\n", " ymin=ymin, ymax=ymin+L+1,\n", " #axis=True,\n", " instruction_file='tmp_mpl.py'\n", " )\n", " #drawing_tool.set_grid(True)\n", " fontsize = 18\n", " from math import tan, radians\n", "\n", " B = point(a+L, 0)\n", " A = point(a, tan(radians(theta))*L)\n", "\n", " wall = Wall(x=[A[0], B[0]], y=[A[1], B[1]], thickness=-0.25,\n", " transparent=False)\n", "\n", " angle = Arc_wText(r'$\\theta$', center=B, radius=3,\n", " start_angle=180-theta, arc_angle=theta,\n", " fontsize=fontsize)\n", " angle.set_linecolor('black')\n", " angle.set_linewidth(1)\n", "\n", " ground = Line((B[0]-L/10., 0), (B[0]-L/2.,0))\n", " ground.set_linecolor('black')\n", " ground.set_linestyle('dashed')\n", " ground.set_linewidth(1)\n", "\n", " r = 1 # radius of wheel\n", " help_line = Line(A, B)\n", " x = a + 3*L/10.; y = help_line(x=x)\n", " contact = point(x, y)\n", " normal_vec = point(sin(radians(theta)), cos(radians(theta)))\n", " tangent_vec = point(cos(radians(theta)), -sin(radians(theta)))\n", " c = contact + r*normal_vec\n", " outer_wheel = Circle(c, r)\n", " outer_wheel.set_linecolor('blue')\n", " outer_wheel.set_filled_curves('blue')\n", " hole = Circle(c, r/2.)\n", " hole.set_linecolor('blue')\n", " hole.set_filled_curves('white')\n", "\n", " wheel = Composition({'outer': outer_wheel, 'inner': hole})\n", " wheel.set_shadow(4)\n", "\n", " drawing_tool.set_linecolor('black')\n", " N = Force(contact - 2*r*normal_vec, contact, r'$N$', text_pos='start')\n", " V = Force(c, c + 0.001*tangent_vec, r'$V$', text_pos='end')\n", " V.set_linecolor('red')\n", " mg = Gravity(c, 3*r, text='$Mg$')\n", "\n", " x_const = Line(contact, contact + point(0,4))\n", " x_const.set_linestyle('dotted')\n", " x_const.rotate(-theta, contact)\n", " # or x_const = Line(contact-2*r*normal_vec, contact+4*r*normal_vec).set_linestyle('dotted')\n", " x_axis = Axis(start=contact+ 3*r*normal_vec, length=4*r,\n", " label='$x$', rotation_angle=-theta)\n", "\n", " body = Composition({'wheel': wheel, 'N': N, 'mg': mg})\n", " fixed = Composition({'angle': angle, 'inclined wall': wall,\n", " 'wheel': wheel, 'V': V, 'ground': ground,\n", " 'x start': x_const, 'x axis': x_axis})\n", "\n", " fig = Composition({'body': body, 'fixed elements': fixed})\n", " fig.draw()\n", " return (fig,normal_vec,tangent_vec,c)\n", "\n", "def position(t):\n", " \"\"\"Position of center point of wheel.\"\"\"\n", " global tangent_vec,c\n", " return c + 7*t**2*tangent_vec\n", "\n", "def speed(t):\n", " global tangent_vec\n", " return 0.14*t*tangent_vec \n", "\n", "t = 0\n", "\n", "def move(change):\n", " global fig,t\n", " dt = change.new - t \n", " t = change.new\n", " drawing_tool.erase()\n", " x = position(t)\n", " x0 = position(t-dt)\n", " displacement = x - x0\n", " fig['fixed elements']['V']['arrow']['line'] = Line(x,x+speed(t))\n", " fig['body'].translate(displacement)\n", " fig.draw()\n", " drawing_tool.display()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "slider = FloatSlider(\n", " orientation='horizontal',\n", " description='Time:',\n", " value=0.0,\n", " min=0.0,\n", " max=1.0,\n", " step = 1.0 / 30\n", ")\n", "slider.observe(move, 'value')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1af7c7deda7b418a8a170ec0b34505ec", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FloatSlider(value=0.0, description='Time:', max=1.0, step=0.03333333333333333)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "slider" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "da53e1cea5d447b5af22e27766617152", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig,normal_vec,tangent_vec,c = inclined_plane()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "speed(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "position(0)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4.5 , 4.90747729])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig['fixed elements']['V']['arrow']['line'].x" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4.5 , 4.90747729])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig['fixed elements']['V']['arrow']['line'].y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "speed(0.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "position(t)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }