{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Get started with Math\n",
"### Plotting a function\n",
"\n",
"[Pyplot tutorial](https://matplotlib.org/users/pyplot_tutorial.html)\n",
"\n",
"#### Plot a straight line using a function(python).\n",
"\n",
"Here function is a concept in python which is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.\n",
"\n",
"In the text below, not to confuse with function in coding, function sometimes means a math function when we say a parabola function, an exponential function or a sine function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # A generic library for arrays and matrices\n",
"import matplotlib.pyplot as plt # library for plotting\n",
"# to enable plotting in line in notebook\n",
"%matplotlib inline\n",
"\n",
"dx = 0.5 # step length. Set a smaller value to get more points in the range and higher resolution.\n",
"x = np.arange(-5,5,dx) # create an array of x from -5 to 5 with step 0.5\n",
"print(x)\n",
"\n",
"# define a function for straight line, given x, slope m, y-intersept b.\n",
"# Input arguments x,m,b here are all internal variables of the function not used by the main program.\n",
"def line(x,m,b):\n",
" y = m*x+b\n",
" return y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"k = -0.5 # assign slope\n",
"y_intercept = 2 # assign y-intercept\n",
"# Here m,b are are global variables, not the same as those which define the line function. Don't have to use the same name as input arguments.\n",
"y = line(x,k,y_intercept) # call the line function with specificed input. \n",
"print(y)\n",
"\n",
"plt.figure(figsize=(6,4))\n",
"plt.plot(x,y)\n",
"plt.grid()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Plot a parabola"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# define a function for parabola function, given x, in vertex form\n",
"def parabola(x,xv,yv,a):\n",
" y = a*(x-xv)**2+yv\n",
" return y\n",
"\n",
"xv = 0\n",
"yv = -2\n",
"a = 0.5\n",
"y = parabola(x,xv,yv,a)\n",
"\n",
"plt.figure(figsize=(6,4))\n",
"plt.plot(x,y)\n",
"plt.grid()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Approximate the tangent line and the slope at some point"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Animated illustration: the tangent line (derivative) as the limit of secants"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Youtube - Formal and alternate form of the derivative | Differential Calculus | Khan Academy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import HTML,YouTubeVideo\n",
"# Use either line below to play the youtube video\n",
"HTML('')\n",
"# YouTubeVideo('Df2escG-Vu0')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"idx1 = 10 # index of a point in x. Choose an index less than the length of the array x.\n",
"d = 5 # increment of index. Decrease this value to approach Q toward P.\n",
"\n",
"idx2 = idx1+d # index of a second point in x\n",
"x1,x2 = x[idx1],x[idx2]\n",
"print([x1,x2])\n",
"\n",
"P,Q = [x1,parabola(x1,xv,yv,a)],[x2,parabola(x2,xv,yv,a)] # two points on the parabola\n",
"print([P,Q])\n",
"\n",
"# define a function for a straight line determined by two points\n",
"def line2pts(x,P0,P1):\n",
" slope = (P1[1]-P0[1])/(P1[0]-P0[0])\n",
" y = (x-P0[0])*slope+P0[1]\n",
" return (y,slope) # output 2 variables\n",
"\n",
"tangent_y,k = line2pts(x,P,Q) # output function value to y, and slope to k\n",
"tangent_y = line2pts(x,P,Q)[0] # put an index 0 if you want only the first output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(6,4))\n",
"plt.plot(x,y)\n",
"plt.plot((P[0],Q[0]),(P[1],Q[1]),'r.')\n",
"plt.plot(x,tangent_y,'g')\n",
"plt.grid()\n",
"plt.show()\n",
"\n",
"print('The approximate tangent line slope at x=%.2f is %.2f' % (P[0],k)) # format printing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Assingment\n",
"#### Like the example above, write codes to plot the curves of exponetial function and sine function as indicated below. And define a function to calculate the curve and approximate a tangent line for each."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Simplest case\n",
"y = np.exp(x) # exp is a numpy function. So you need np to refer to it\n",
"y = np.sin(x) # sin is also a numpy function\n",
"\n",
"\n",
"# For more variability of the functions, you can define them with some parameters like below.\n",
"\n",
"A,B = # A - y-intercept, B - steepness\n",
"y = A*np.exp(B*x)\n",
"\n",
"Amp,phi = # Amp - amplitude, phi - initial phase\n",
"y = Amp*np.sin(x+phi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since __line2pts__ for getting a line given two points has been defined in the previous cell, you can directly call the function later. You need to define it only once."
]
},
{
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}