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.
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.
import numpy as np # A generic library for arrays and matrices import matplotlib.pyplot as plt # library for plotting # to enable plotting in line in notebook %matplotlib inline dx = 0.5 # step length. Set a smaller value to get more points in the range and higher resolution. x = np.arange(-5,5,dx) # create an array of x from -5 to 5 with step 0.5 print(x) # define a function for straight line, given x, slope m, y-intersept b. # Input arguments x,m,b here are all internal variables of the function not used by the main program. def line(x,m,b): y = m*x+b return y
k = -0.5 # assign slope y_intercept = 2 # assign y-intercept # 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. y = line(x,k,y_intercept) # call the line function with specificed input. print(y) plt.figure(figsize=(6,4)) plt.plot(x,y) plt.grid() plt.show()
# define a function for parabola function, given x, in vertex form def parabola(x,xv,yv,a): y = a*(x-xv)**2+yv return y xv = 0 yv = -2 a = 0.5 y = parabola(x,xv,yv,a) plt.figure(figsize=(6,4)) plt.plot(x,y) plt.grid() plt.show()
Animated illustration: the tangent line (derivative) as the limit of secants
from IPython.display import HTML,YouTubeVideo # Use either line below to play the youtube video HTML('<iframe width="640" height="360" src="https://www.youtube.com/embed/Df2escG-Vu0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>') # YouTubeVideo('Df2escG-Vu0')
idx1 = 10 # index of a point in x. Choose an index less than the length of the array x. d = 5 # increment of index. Decrease this value to approach Q toward P. idx2 = idx1+d # index of a second point in x x1,x2 = x[idx1],x[idx2] print([x1,x2]) P,Q = [x1,parabola(x1,xv,yv,a)],[x2,parabola(x2,xv,yv,a)] # two points on the parabola print([P,Q]) # define a function for a straight line determined by two points def line2pts(x,P0,P1): slope = (P1-P0)/(P1-P0) y = (x-P0)*slope+P0 return (y,slope) # output 2 variables tangent_y,k = line2pts(x,P,Q) # output function value to y, and slope to k tangent_y = line2pts(x,P,Q) # put an index 0 if you want only the first output
plt.figure(figsize=(6,4)) plt.plot(x,y) plt.plot((P,Q),(P,Q),'r.') plt.plot(x,tangent_y,'g') plt.grid() plt.show() print('The approximate tangent line slope at x=%.2f is %.2f' % (P,k)) # format printing
# Simplest case y = np.exp(x) # exp is a numpy function. So you need np to refer to it y = np.sin(x) # sin is also a numpy function # For more variability of the functions, you can define them with some parameters like below. A,B = # A - y-intercept, B - steepness y = A*np.exp(B*x) Amp,phi = # Amp - amplitude, phi - initial phase y = Amp*np.sin(x+phi)
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.