{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# Lab 01 - CoCalc introduction" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### _Replace this text with your names!_" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "Cocalc ([CoCalc.com](http://cocalc.com)) combines the capabilities of a word processor and a sophisticated calculator. This is a quick introduction to get you started. I assume you have already gone through the first two [mathphys CoCalc links](https://people.goshen.edu/~paulmr/physix/mathphys/) which show you how to sign up for an account and get started with a notebook.\n", "\n", " \n", "### Cells\n", "\n", "There's a dropdown menu above that lets you choose between `Markdown` and `Code`:\n", "+ This cell is a `Markdown` cell, used for **writing**.\n", "+ The cell below is a `Code` cell used for **calculations**, or more generally for coding. See the `sin(...)` example below,\n", "+ There's also a `Raw` option on that menu, which we'll ignore for the time being.\n", "\n", "The language (or 'kernel') that you're using for code cells is also shown in the menu bar above. Yours probably says \"**SageMath 8.3**\" right now, e.g.\n", "\n", "\n", "
\n", " \n", "
\n", "\n", "## Calculations with `Code` cells\n", "\n", "The code cell below calculates $\\sin^2\\left(\\frac{2\\pi}{3}\\right)$ using the current kernel--**SageMath**. Observe that \n", "+ SageMath has names for many constants (`pi` for $\\pi$). \n", "+ Most of these are _case sensitive_. So you won't get the same effect from `Pi` as `pi`. \n", "+ You must use `*` for multiplication. The caret, `^`, for exponentiation. \n", "+ The square appears at the end because $\\sin^2(2\\pi/3)$ is shorthand for $\\left(\\sin(2\\pi/3)\\right)^2$. \n", "\n", "To carry out the calculation below, \n", "+ Click your mouse **anywhere in the cell** (not just at the end of the line) and \n", "+ then press the `Shift+Enter` keys together.\n", "\n", "A new \"output\" cell will be created with the answer.\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3/4" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" } ], "source": [ "( \n", " sin( 2*pi/3 ) \n", ")^2" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "[By the way, work through the rest of this notebook, hitting `Shift+Enter` in each input cell in turn to see the result.]\n", "\n", "+ Notice in the cell above that you can add extra spaces and line breaks (use these for readability) and they'll be ignored for purposes of calculation.\n", "\n", "+ You get an **exact** answer (the fraction 3/4) unless you indicate otherwise. You can request an _approximate_ answer by placing your expression inside the brackets of the the `N(...)` function, or putting a decimal point in to any of the numbers. E.g., 3.0 indicates an approximate number whereas 3 indicates an exact number. \n", "+ Click your mouse anywhere in the cell below and press the `Shift+Enter` keys together. A new output cell will be created with the answer to _only the last expression_ of the 3 entered below:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.750000000000000" ] }, "execution_count": 2, "metadata": { }, "output_type": "execute_result" } ], "source": [ "# This is a comment in a code cell.\n", "#\n", "# There are 3 expressions here (each on their own line)\n", "# but only the last one should result in a printed, numerical value\n", "#\n", "(sin(2*pi/3))^2\n", "N( (sin(2*pi/3))^2 )\n", "N((sin(2.*pi/3))^2)\n", " " ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### By default Sage only prints the output of the last command, if you have multiple commands.\n", "\n", "To print several results, enclose all your commands (separated with commas) within **sqare brackets**. This structure is called a _list_ and will print out a list of answers, one for each expression. Here are 3 results:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[3/4, 0.750000000000000, 0.750000000000000]" ] }, "execution_count": 3, "metadata": { }, "output_type": "execute_result" } ], "source": [ "[ (sin(2*pi/3))^2, N( (sin(2*pi/3))^2 ), N( (sin(2.*pi/3.))^2 ) ]" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**Exercise 1**: Explain in words what is being calculated in each of the 4 expressions below. You should explicitly recognize what _base_ is being used in each logarithmic expression. (For example, the first one is calculating the logarithm (base $e$) of the exponential constant $e$. That is to say, the answer is 1 because $e$ is equal to $e^1$):\n", "\n", "1. Use the plus button on the bottom toolbar, , to insert a new cell.\n", "1. Make it a `Markdown` cell to write out your explanations. You will use the `Shift+Enter` key combination to \"render\" your markdown cell.\n", "1. Move your markdown cell after the calculations cell using the up and down arrow keys as necessary on the toolbar." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 1, 4.60517018598809, 2.00000000000000]" ] }, "execution_count": 5, "metadata": { }, "output_type": "execute_result" } ], "source": [ "[ ln(e), log(e), log(100.), log(100.,10) ]" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "In SageMath the single '`=`' is actually the _assignment operator_. So `x=7` assigns 7 to the variable $x$. But assigning $x$ to 7 makes no sense so `7=x` will throw an error.\n", " \n", "The = in an equation such as\n", "$$x=7-x$$\n", "expresses a relationship, **equality**, between two _expressions_. So, this sense of '_equals_' gets a different symbol in SageMath, namely **`==`**. You can _solve_ the equation above for $x$ like this:\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[x == (7/2)]" ] }, "execution_count": 27, "metadata": { }, "output_type": "execute_result" } ], "source": [ "solve( x==7-x, x )" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "You can assign equations to named objects, and solve systems of equations (using lists...):" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[x == 4, y == 6]]" ] }, "execution_count": 28, "metadata": { }, "output_type": "execute_result" } ], "source": [ "eq1= x+y==10\n", "eq2= y-x==2\n", "solve( [eq1, eq2], [x,y])" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**Exercise 4**: Use `solve(...)` to solve the quadratic equation\n", "$$x^2-5x= -6$$ \n", "for $x$. [Hint: '5x' will mean nothing to SageMath. Code that piece of the expression differently to express multiplication.] Does the answer (or answers) you get make sense?" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[x == 6, x == -1]" ] }, "execution_count": 29, "metadata": { }, "output_type": "execute_result" } ], "source": [ "solve(x^2-(5*x)==6,x)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "
circumference (inches) | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
usable lumber (board feet) | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |