{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "reference-location: margin\n", "citation-location: margin\n", "execute:\n", " freeze: auto\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Jupyter Notebook](https://lancejnelson.github.io/PH135/jupyter/sympy.ipynb)\n", "\n", "# The `sympy` module\n", "`Sympy` (pronounced \"sim-pie\") is short for symbolic python and can be used to do symbolic math calculations. You may be familiar with other symbolic math packages like Mathematica or Maple. Python's sympy package will do all of the same things that they will do and it's nice to not have to switch between two different coding languages.\n", "\n", "\n", "## Simple Calculations\n", "You should be very comfortable performing simple calculations by now. Up to this point all of the calculations that you have performed have been represented **approximately**, rather than exactly. For example, $\\cos({\\pi \\over 6}) = {\\sqrt{3} \\over 2} \\text{ (exact)} \\approx 0.8660254 \\text{ (approximate)}$ and when you perform this calculation in Python using the `math` module, the result is approximate:\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from math import cos,pi\n", "\n", "cos(pi/6)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$0.8660254$ is an approximate result; it's very close to the true value but not exact. Sympy can also perform this calculation and the results are represented exactly." ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import cos,pi,init_printing\n", "init_printing(use_unicode = True)\n", "\n", "a=cos(pi/6)\n", "display(a)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `init_printing(use_unicode=True)` function call tells sympy to display mathematical expressions using \"pretty\" formatting. Fractions appear with the numerator on top of the denominator, powers are displayed as superscripts, and other mathematical symbols are rendered to make the mathematical expression look right. Sympy can even simplify expressions. For example, $\\sqrt{8} = 2 \\sqrt{2}$ and sympy automatically performs this simplification for us:" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import sqrt\n", "\n", "sqrt(8)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you prefer to have your expression approximated numerically, you can use the `N` function.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import sqrt,N\n", "\n", "N(sqrt(8))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Forming Mathematical Expressions\n", "\n", "### Defining Symbols (Mathematical Variables)\n", "A common equation that comes up in physics is quadratic function with constant coefficients $$ax^2 + bx + c.$$ To form this expression, we must first define the variables that appear in the expression: `x`, `a`, `b`, and `c`. The `Symbol` function (case sensitive) can be used to define these **mathematical** variables. The argument to the `Symbol` function should be a string containing the desired name of the variable. " ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import Symbol\n", "x = Symbol(\"x\")\n", "a = Symbol(\"a\")\n", "b = Symbol(\"b\")\n", "c = Symbol(\"c\")\n", "x,a,b,c" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of using the `Symbol` function to initialize variables one at a time, you can initialize multiple variables in one line using the `symbols` function. The argument to the `symbols` function should be a string with the names of each variables inside the string and separated by a space. " ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols\n", "\n", "x,a,b,c = symbols(\"x a b c\")\n", "x,a,b,c" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mathematical Expressions\n", "\n", "A mathematical expression, which is composed of multiple mathematical variables (symbols), can now be created using the python variables that we just created" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols\n", "x,a,b,c = symbols(\"x a b c\")\n", "expression = a * x**2 + b * x + c\n", "expression" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Expressions can be used to form new expressions by performing mathematical operations on them" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols\n", "\n", "x,a,b,c = symbols(\"x a b c\")\n", "expression = a * x**2 + b * x + c\n", "\n", "newexpression = x * expression\n", "newexpression" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simplifying Expressions\n", "`sympy` has several handy functions that can perform algebraic manipulations to your expressions. Below you will find some of the most useful functions for simplifying expressions.\n", "\n", "### Evaluating expressions\n", "Once an expression is defined, you can evaluate the expression using the `subs` method. The argument to this method is a dictionary containing the values to be inserted for the associated variables. An example is given below" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols\n", "\n", "x,a,b,c = symbols(\"x a b c\")\n", "expression = a * x**2 + b * x + c\n", "\n", "evalDict ={a:2,b:4,c:9,x:4}\n", "expression.subs(evalDict)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `simplify` function\n", "If you don't know exactly how you want your expression simplified, you should first try the `simplify` function. `simplify` attempts to apply all of the more specialized functions in a smart way to produce a simpler looking result. To use `simplify` simply send the desired expression in as an argument.\n", "\n", "```\n", "simplify(expression)\n", "```" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,cos,sin,simplify\n", "x,y,z = symbols(\"x y z\")\n", "a = simplify(x**2 * z + 4 * x *y*z + 4*y**2*z)\n", "b = simplify(cos(x)**2 + sin(x)**2)\n", "display(a)\n", "display(b)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When `simplify` doesn't do what you were hoping, you'll have to think a little bit harder about how you want to simplify your expression. The functions described in the rest of this section will help you in your choice.\n", "\n", "### The `expand` function\n", "The expand function will put a polynomial function into its canonical monomial form. For example, expanding the following polynomial $$(x + 1)^2$$\n", "will produce the standard form of the polynomial $$x^2 + 2x + 1$$. This may not seem like a simplification but nevertheless it can be helpful at times. To expand a polynomial, call the `expand` function and send in the expression that you want expanded.\n", "\n", "```\n", "expand(expression)\n", "```" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,expand\n", "x = symbols(\"x\")\n", "a = expand((x + 2) * (x - 3))\n", "b = expand((x+3)**2)\n", "display(a)\n", "display(b)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes expanding an expression can result in a single number due to cancellation. \n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import Symbol,expand\n", "x = Symbol(\"x\")\n", "expand((x + 1)*(x - 2) - (x - 1)*x)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `factor` function\n", "\n", "The `factor` function does the opposite of `expand`, taking a canonical polynomial and attempting to factor it into its irreducible parts. For example, factoring the expression below $$x^3 - x^2 + x -1$$ will produce $$(x-1)(x^2 + 1)$$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,factor\n", "x,y,z = symbols(\"x y z\")\n", "factor(x**2 * z + 4 * x *y*z + 4*y**2*z)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also factor an expression that isn't a polynomial. The following trigonometric function can be factored $$\\cos(x)^2 + 2 \\cos(x)\\sin(x) + \\sin(x)^2$$ into $$(\\sin(x) + \\cos(x))^2$$" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,factor,cos,sin\n", "x,y,z = symbols(\"x y z\")\n", "factor(cos(x)**2 + 2 * cos(x) * sin(x) + sin(x)**2)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `collect` function\n", "The `collect` function gathers terms with common powers in an expression. The function takes two arguments: the first is the expression and the second specifies which variable's powers should be collected.\n", "\n", "```\n", "collect(expression, variable)\n", "```" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,collect\n", "x,y,z = symbols(\"x y z\")\n", "collect(x*y + x - 3 + 2 * x**2 - z *x**2 + x**3,x)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `cancel` function\n", "\n", "The `cancel` function is helpful for simplifying an expression by canceling out like terms where possible. This function takes just a single argument, which is the expression to be simplified. The code below will simplify the following expression by canceling the common `(x+1)` that appears in both the numerator and denominator. $${x^2 + 2 x + 1 \\over x^2 + x}$$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,cancel\n", "x,y,z = symbols(\"x y z\")\n", "cancel((x**2 + 2*x + 1)/(x**2 + x))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `trigsimp` function\n", "\n", "If you want to use trigonometric identities to simplify your expression use the `trigsimp` function. For example, most of us know that $\\cos(x)^2 + \\sin(x)^2 = 1$. `trigsimp` will perform this simplification for us. \n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,trigsimp,cos,sin\n", "x = symbols(\"x\")\n", "trigsimp(cos(x)**2 + sin(x)**2)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `expand_trig` function\n", "\n", "`expand_trig` is the opposite of `trigsimp` and usually makes trig functions longer, but that can still be a useful way to view the function. Below we expand the expression $$\\sin(x + y)$$ using a trig identity. \n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,expand_trig\n", "x,y = symbols(\"x y\")\n", "expand_trig(sin(x + y))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculus\n", "`sympy` can do common calculus operations on symbolic expressions. We'll cover: derivatives, integrals, and limits.\n", "\n", "### Derivatives\n", "\n", "To calculate the derivative of an expression, use the `diff` function, which takes two arguments. The first is the expression that you want to take the derivative of and the second is the differentiating variable. In the cell below we calculate the derivative $${d\\over dx} \\cos(x)$$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,diff,cos\n", "x = symbols(\"x\")\n", "diff(cos(x),x)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To take multiple derivatives, simply add more `x`s to the `diff` function call. Below we calculate the derivative $${d^2\\over dx^2} \\cos(x)$$ \n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,diff,cos\n", "x = symbols(\"x\")\n", "diff(cos(x),x,x)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`diff` can perform partial derivatives too. Below we perform the following partial derivatives $${\\partial^2 \\over \\partial z^2} {\\partial \\over \\partial y} {\\partial \\over \\partial x} \\exp(x y z)$$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,diff,exp\n", "x,y,z = symbols(\"x y z\")\n", "diff(exp(x * y * z),x,y,z,z)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A differentiated function is a sympy expression just as good as any other and we can make a plot of it by evaluating the expression over a grid of values." ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,diff\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "x,a,b,c = symbols(\"x a b c\")\n", "expression = a * x**2 + b * x + c\n", "\n", "newexpression = diff(expression,x)\n", "xarray = np.arange(0,5,0.1)\n", "y = [expression.subs({a:-2,b:4,c:9,x:X}) for X in xarray]\n", "yprime = [newexpression.subs({a:-2,b:4,c:9,x:X}) for X in xarray]\n", "plt.plot(xarray,yprime,'r.-',label= \"y'(x)\")\n", "plt.plot(xarray,y,'b.-',label= \"y(x)\")\n", "plt.legend()\n", "plt.grid()\n", "plt.show()" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integrals\n", "\n", "Use the `integrate` function to perform definite and indefinite integrals. For definite integral, the function should be called with two arguments; the first is the expression to be integrated and the second is the tuple containing the integration limits. Below we integrate $$ \\int_0^5 \\exp(-x) dx$$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import integrate, Symbol\n", "\n", "x = Symbol(\"x\")\n", "\n", "integrate(exp(-x),(x,0,5))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The limits of your integral can be at infinity by importing `oo` and using it in the limit specification.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import integrate, Symbol,oo\n", "\n", "x = Symbol(\"x\")\n", "\n", "integrate(exp(-x),(x,0,oo))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiple integrations can be carried out by adding more arguments to the `integrate` function, each one specifying the limits for that variable. Here we integrate $$\\int_0^\\infty \\int_0^\\infty \\exp(-x^2 -y^2) dx dy $$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import integrate, symbols,oo\n", "\n", "x,y = symbols(\"x y\")\n", "\n", "integrate(exp(-x**2 - y**2),(x,0,oo),(y,0,oo))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indefinite integrals can be performed by omitting the limit specification and just indicating the variable to be integrated over. Below we perform the following indefinite integral $$\\int \\int x^2 + y^2 dx dy $$ \n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import integrate, symbols\n", "\n", "x,y = symbols(\"x y\")\n", "\n", "integrate(x**2 + y**2,x,y)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Limits\n", "`sympy` can compute limits symbolically with the `limit` function. The function takes three arguments: the expression, the variable of interest, and the limiting value for that variable. Below we take the following limit $$\\lim_{x \\to 0} {\\sin(x)\\over x}$$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import limit, Symbol,sin\n", "\n", "x = Symbol(\"x\")\n", "\n", "limit(sin(x)/x,x,0)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solving equations\n", "\n", "So far we have seen how to construct expressions but not solve equations. The difference is that an equation is expressing some sort of equality between an multiple expressions. For example, the following is an equation $$5 x^2 + 3 x - 2 = 10$$ To form an equation, you can use the `Eq` function. To use it, send in the expression on the left hand side of the equation for the first argument and the expression on the right hand side of the equation for the second argument.\n", "\n", "```\n", "Eq(lhs,rhs)\n", "```\n", "\n", "Below we form the equation $$5 x + 2y = 8$$" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,Eq\n", "\n", "x,y = symbols(\"x y\")\n", "\n", "equation = Eq(5 * x + 2 * y , 8)\n", "equation" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solving single equations\n", "Once an equation is created, the `solveset` function attempts to solve the function for the desired variables. To use `solveset`, pass the equation as the first argument and the variable being solved for as the second. Here we solve the equation $$3 x^2 - 5 x =8$$ for $x$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,Eq,solveset\n", "\n", "x = symbols(\"x\")\n", "\n", "equation = Eq(3* x**2 - 5 * x, 8)\n", "solveset(equation,x)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### No analytic solution\n", "In some instances, `sympy` will not be able to find any solutions to the equation. For example, the solution to $${\\sin(x)\\over x} = 1$$ cannot be determined through analytical technique even though there are values of x that solve the equation. For situations like this, `sympy` will output an expression that is meant to indicate that it can't find a solution.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,Eq,solveset,sin\n", "\n", "x = symbols(\"x\")\n", "\n", "equation = Eq(sin(x)/x, 1)\n", "solveset(equation,x)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### No solution... period\n", "Some equations really don't have solutions and `sympy` can correctly identify these cases. For example, the equation $$\\exp(x) = 0$$ has no solutions. Let's see what `sympy` tells us when we try to solve this one.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,Eq,solveset,exp\n", "\n", "x = symbols(\"x\")\n", "\n", "equation = Eq(exp(x), 0)\n", "solveset(equation,x)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The $\\emptyset$ symbol means an empty set or there are no solutions to this equation. \n", "\n", "#### Many or Infinite Solutions\n", "Some equations have infinitely many solutions. For example the following equation $$x - x = 0$$ is satisfied for any complex or real value of $x$. Let's see what sympy does when we ask it to solve this one.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,Eq,solveset\n", "\n", "x = symbols(\"x\")\n", "\n", "equation = Eq(x-x, 0)\n", "solveset(equation,x)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output ($\\mathbb{C}$) is communicating that the set of all complex numbers are the solution to this equation. (Meaning you can choose any number and it will be a solution to this equation.) \n", "\n", "\n", "### Solving Systems of equations\n", "Often you will have multiple equations with multiple unknown variables that you want to solve for. They call this a system of equations. An example would be the following system $$3 x - 5 y = 8$$ $$8x + 2y = 3$$. The normal process for solving this system is to solve one equation for $x$ and then plug the expression that results into the other equation. That equations will only have $y$ as a variable and you can do algebra to solve for it. \n", "\n", "#### Linear Systems\n", "The example given above is called a linear system because none of the variables are raised to a power. To solve a system like this, use `sympy`'s `linsolve` function.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,Eq,linsolve\n", "\n", "x,y = symbols(\"x y\")\n", "\n", "equationOne = Eq(3* x - 5 * y, 8)\n", "equationTwo = Eq(8* x + 2 * y, 3)\n", "linsolve([equationOne,equationTwo],(x,y))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It isn't hard to find a system of equations that doesn't have a solution. $$12 x - 3 y = 8$$ $$8x - 2y = 3$$ As with single equations, `sympy` will indicate this with the empty set symbol ($\\emptyset$).\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,Eq,linsolve\n", "\n", "x,y = symbols(\"x y\")\n", "\n", "equationOne = Eq(12* x - 3 * y, 8)\n", "equationTwo = Eq(8* x - 2 * y, 3)\n", "linsolve([equationOne,equationTwo],(x,y))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Nonlinear Systems\n", "\n", "If the equations involve variables being raised to powers, we call it a non linear system and you must use `sympy`'s `nonlinsolve` to solve the system. For example, here is a nonlinear system $$3 x^2 + 5 y^2 = 8$$ $$8x^2 - 12y^2 = 13$$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from sympy import symbols,Eq,nonlinsolve\n", "\n", "x,y = symbols(\"x y\")\n", "\n", "\n", "eqOne = Eq(3*x**2 + 5*y**2,8)\n", "eqTwo = Eq(8*x**2 - 12*y**2,13)\n", "nonlinsolve([eqOne,eqTwo],(x,y))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Flashcards\n", "1. How do you define a mathematical variable in Python?\n", "2. How do you form a mathematical expression in Python? (Give an example)\n", "3. How do you form a mathematical equation in Python? (Give an example)\n", "4. What does the simplify function do?\n", "5. What does the factor function do?\n", "6. How do you perform a derivative in Python?\n", "7. How do you perform an integral in Python? (definite and indefinite)\n", "8. How do you solve an equation in Python?\n", "9. How do you solve a system of equations in Python?\n", "10. How do you evaluate a definite integral at infinity ($\\infty$)?\n", "11. Recite 1 Ne 4:6\n", "\n", "\n", "## Exercises\n", "1. Define a `sympy` variable for $x$ and then evaluate the following expressions.\n", " - $\\sin(x + {\\pi \\over 2})$\n", " - $\\sin(x + \\pi)$ \n", " - $\\cos(x + {\\pi \\over 2})$\n", " - $\\cos(x + \\pi)$\n", " - $\\sin(-x)$ \n", " - $\\cos(-x)$\n", " - $\\sqrt{1 - \\sin(x)^2}$\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python code here" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. A projectile is thrown directly downward from the top of a tall building ($y_i = 150$ meters) and lands with a thud on the ground ($y_f = 0$) $4$ seconds later. The position equation for the projectile is $$y_f = y_i + v_i \\Delta t + {1\\over 2} g \\Delta t^2$$ Use `sympy` to solve this equation for the initial velocity. Note $g = 9.8$ m/s$^2$ Answer: You should find the initial speed to be $\\approx 18$ m/s" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python code here" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. A projectile is launched from the top of a building ($y_i = 23$ m) with an initial speed of $v_i = 60$ m/s and a launch angle of $\\theta_i = 32^\\circ$. The vertical position of the projectile is given by $$y_f = y_i + v_i \\sin \\theta_i \\Delta t - {1\\over 2}g \\Delta t^2$$. Determine the time it takes before the projectile impacts the ground by solving this equation for $\\Delta t$." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python code here" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. An elastic collision is one where the total energy of the particles is the same before and after the collision. The equations that define an elastic collision where one of the objects is initially at rest are $${1\\over 2} m_1 (v_1)_i^2 = {1\\over 2} m_1 (v_1)_f^2 +{1\\over 2} m_2 (v_2)_f^2$$ $$m_1 (v_1)_i = m_1 (v_1)_f + m_2 (v_2)_f$$. Consider a situation where particle 1 ($m_1 = 5 \\text{ kg}$) is initially moving to the right with speed $(v_1)_i = 10 \\text{ m/s}$ and particle 2 is stationary ($m_2 = 2 \\text{ kg}$). Solve this system of equations for$(v_1)_f$ and $(v_2)_f$. Do the results make sense? Then do the following\n", " 1. Make $m_2$ greater than $m_1$. What do you expect will happen. Verify that the results match your guess. \n", " 2. Make the mass of particle 2 much greater than particle 1. What do you expect will happen? Verify that the results match your guess.\n", " 3. Make the mass of particle 1 much greater than particle 2. What do you expect will happen? Verify that the results match your guess." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python code here" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Define a `sympy` variable for $x$ and then calculate the following derivatives:\n", " 1. ${d\\over dx} \\ln(x)$\n", " 2. ${d\\over dx} e^{5x^2}$\n", " 3. ${d\\over dx} \\cos(3 x^3)$\n", " 4. ${d\\over dx} (x^3 - 5x^2 + 10 x)$" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python code here" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. Impulse is defined as the integral of the Force vs. time graph. $$I = \\int F dt$$. Calculate the impulse imparted to an object from $0< t < 10$ if the force that it experiences is given by $$F(t) = e^{-(t - 4)}$$.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python code here" ], "execution_count": null, "outputs": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }