8.2. Functions#
Note
Below is the list of topics that are covered in this section:
Function Definition and Terminology
Function Behaviors
Common Functions (Linear, Exponential, Logarithm, Logistic)
Function Operations, Compositions, and Inverse
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
8.2.1. Function Definition and Terminology#
A function is a rule that assigns exactly one output to each input.
When a function
Example 1
is a rule that adding into the input , giving the output . is a rule that squaring the input , then subtracting the result from , giving the output .
Code example for
Show code cell source
#define the function f(x)
def f(x):
return x+3
# Ask for user input (uncomment the next line if using an interactive JNB)
user_input = float(input("Enter input value x ="))
# Call the function with the user input
output = f(user_input)
# Print the output
print("The output is f(x) =", output)
Enter input value x =1
The output is f(x) = 4.0
The domain of a function is the set of all input values that can be plugged into a function such that the output exists.
The range of a function is the set of all possible output values that a function can take.
Note that for the domain we need to avoid division by zero, square roots of negative numbers, and logarithms of nonpositive numbers. Can you explain why?
Python itself does not have built-in functionality to directly determine the domain and range of a given function. The domain and range of a function often depend on the specific characteristics of the function and its mathematical properties.
Example 2
Given
Excluding the zeroes of the denominator and negative number inside the square root, the domain of
Code example for
# Define the function symbolically
x = sp.Symbol('x')
f = 1 / (sp.sqrt(x))
# Find the domain: denominator not zero AND inside square root must be positive
domain = sp.solve(sp.denom(f) != 0 and x>0, x)
# Print the domain
print("Domain:", domain)
Domain: (0 < x) & (x < oo)
8.2.2. Function Behaviors#
Increasing, Decreasing, and Constant Function#
A function
increasing if the output values increase as the input values increase.
decreasing if the output values decrease as the input values increase.
constant if the output values remain the same as the input values increase.
Concave Up and Concave Down Function, Inflection Point#
A function
concave up if a graph of
appears to be a portion of an arc opening upward.concave down if a graph of
appears to be a portion of an arc opening downward.
A point on a function changes concavity is called an inflection point.
8.2.3. Common Functions (Linear, Exponential, Logarithm, Logistic)#
Linear Functions#
A linear function is a function that can be expressed in the form
The slope
Determines the steepness or inclination of the line. A positive slope indicates an upward trend, a negative slope indicates a downward trend, and a slope of zero represents a horizontal line.
Represents the rate at which the function’s output changes with respect to the input.
Is calculated as the ratio of the change in
-coordinates to the change in -coordinates between two points on the line, that is, between point and , the slope .
The
Is the point where the line intersects the
-axis.Represents the value of the
when .
To find the
Below is the code prompting user to input the constants
Show code cell source
# Prompt the user to input the slope and y-intercept
m = float(input("Enter the value for the slope (m): "))
b = float(input("Enter the value for the y-intercept (b): "))
# Define the linear function
def linear_function(x):
return m * x + b
# Generate a range of x values for plotting
x = np.linspace(-10, 10, 100)
# Compute the corresponding y values using the linear function
y = linear_function(x)
# Plot the linear function
plt.plot(x, y, label='f(x) = mx + b')
# Plot the x-axis and y-axis lines
plt.axhline(0, color='black', linewidth=0.5) # x-axis
plt.axvline(0, color='black', linewidth=0.5) # y-axis
# Set plot labels and title
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Linear Function')
# Add a legend
plt.legend()
# Display the plot
plt.show()
Enter the value for the slope (m): 2
Enter the value for the y-intercept (b): 4

Exponential Functions#
An exponential function is a function in the form
Below is the code to plot the graphs of
Show code cell source
# Define the x range
x = np.linspace(-3, 3, 100)
# Define the functions
f1 = 2 ** x
f2 = (1 / 2) ** x
# Plot the functions
plt.plot(x, f1, label='f(x) = 2^x')
plt.plot(x, f2, label='f(x) = (1/2)^x')
# Plot the x-axis and y-axis lines
plt.axhline(0, color='black', linewidth=0.5) # x-axis
plt.axvline(0, color='black', linewidth=0.5) # y-axis
# Set plot labels and title
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Plot of f(x) = 2^x and f(x) = (1/2)^x')
# Add a legend
plt.legend()
# Display the plot
plt.show()

Properties of
. The -intercept is always at . . An exponential function is always positive (never touch zero either)The domain of
is while the range of is .If
then goes to as goes to . goes to as goes to .
If
then goes to as goes to . goes to as goes to .
The natural exponential function is
Logarithm Functions#
A logarithm (log) function is a function in the form
Below is the code to plot the graphs of
Show code cell source
# Define the functions
def f1(x):
return 1 + 2 * np.log(x)
def f2(x):
return 1 - 2 * np.log(x)
# Generate a range of x values for plotting
x = np.linspace(0.1, 10, 100)
# Compute the corresponding y values for each function
y1 = f1(x)
y2 = f2(x)
# Plot the functions
plt.plot(x, y1, label='f(x) = 1 + 2 * ln(x)')
plt.plot(x, y2, label='f(x) = 1 - 2 * ln(x)')
# Plot the x-axis and y-axis lines
plt.axhline(0, color='black', linewidth=0.5) # x-axis
plt.axvline(0, color='black', linewidth=0.5) # y-axis
# Set plot labels and title
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Plot of f(x) = 1 + 2 * ln(x) and f(x) = 1 - 2 * ln(x)')
# Add a legend
plt.legend()
# Display the plot
plt.show()

Properties of
The input
is restricted to be positive since is only defined for .The log function then can be either increasing or decreasing depending on the value of
.For increasing log functions:
concave down
vertical asymptote
, with goes to goes to as goes to
For decreasing log functions:
concave up
vertical asymptote
, with goes to goes to as goes to
Logistic Functions#
While exponential and log functions are commonly used in real life, it is unrealistic to believe that exponential growth can continue forever. In many situations, the growth ultimately restricted by the logistic availability.
A logistic function is a function in the form
In this function,
Depending on the positive or negative growth of the logistics, the logistic function can either increase or decrease.
Below is an example of the graph of an increasing logistic function
Show code cell source
# Define the x range
x = np.linspace(-10, 10, 100)
# Define the logistic function
f = 400 / (1 + 2.5 * np.exp(-0.5 * x))
# Plot the logistic function
plt.plot(x, f, label='f(x) = 400 / (1 + 2.5 * e^(-0.5x))')
# Plot the x-axis and y-axis lines
plt.axhline(0, color='black', linewidth=0.5) # x-axis
plt.axvline(0, color='black', linewidth=0.5) # y-axis
# Set plot labels and title
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Plot of the Logistic Function')
# Add a legend
plt.legend()
# Display the plot
plt.show()

Below is the code prompting user to input the constants
Show code cell source
# Prompt the user to input the parameters
L = float(input("Enter the value for L: "))
a = float(input("Enter the value for a: "))
b = float(input("Enter the value for b: "))
# Define the logistic function
def logistic_function(x):
return L / (1 + a * np.exp(-b * x))
# Generate a range of x values for plotting
x = np.linspace(-10, 10, 100)
# Compute the corresponding y values using the logistic function
y = logistic_function(x)
# Plot the logistic function
plt.plot(x, y, label='f(x) = L / (1 + a * e^(-b * x))')
# Plot the x-axis and y-axis lines
plt.axhline(0, color='black', linewidth=0.5) # x-axis
plt.axvline(0, color='black', linewidth=0.5) # y-axis
# Set plot labels and title
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Logistic Function')
# Add a legend
plt.legend()
# Display the plot
plt.show()
Enter the value for L: 10
Enter the value for a: 1
Enter the value for b: 2

Properties of
For increasing logistic function:
The function begins concave up then changes to concave down
Horizontal asymptotes
to the left and to the right
For decreasing logistic function:
The function begins concave down then changes to concave up
Horizontal asymptotes
to the left and to the right
Both increasing and decreasing logistic functions have exactly one inflection point.
Without further contextual restriction on the domain, the domain of a logistic function is
8.2.4. Function Operations, Compositions, and Inverse#
Function Operations#
Note that not everything can be mathematically operated. To show a simple example, adding apples and oranges does not equal an exclusive number of either apples or oranges. If given context, it is important to pay attention to what you are trying to operate. As mentioned in the Introduction section, we focus on the values rather than the contexts in this Calculus module.
If the input
function addition:
function subtraction:
function multiplication:
function division:
, where
Example 3
Below is a code example that prompts the user to input a value of
It then computes and returns the sum, difference, product, and quotient of the functions at the given value of
Note that
Show code cell source
def calculate_operations(f, g, x):
# Evaluate f(x) and g(x)
f_value = f(x)
g_value = g(x)
# Compute sum, difference, product, and quotient
sum_result = f_value + g_value
difference_result = f_value - g_value
product_result = f_value * g_value
quotient_result = f_value / g_value
# Return the results
return sum_result, difference_result, product_result, quotient_result
# Prompt the user to enter the value of x
x = float(input("Enter the value of x: "))
# Prompt the user to enter the functions as strings
f_input = input("Enter the first function (f(x)): ")
g_input = input("Enter the second function (g(x)): ")
# Create the functions from the user input
f = eval("lambda x: " + f_input)
g = eval("lambda x: " + g_input)
# Calculate the operations at the given value of x
sum_result, difference_result, product_result, quotient_result = calculate_operations(f, g, x)
# Print the results
print("Sum:", sum_result)
print("Difference:", difference_result)
print("Product:", product_result)
print("Quotient:", quotient_result)
Enter the value of x: 3
Enter the first function (f(x)): x**2
Enter the second function (g(x)): 3*x-1
Sum: 17.0
Difference: 1.0
Product: 72.0
Quotient: 1.125
Function Compositions#
The function composition of two functions is evaluated by plugging one function into the other function. The composition operator is
Example 4
Given
To create a composition function of two user functions at a certain input value, you can prompt the user to enter the functions as strings and dynamically evaluate and compose them.
For
Show code cell source
def compose_functions(f, g):
def composed_function(x):
return f(g(x))
return composed_function
# Prompt the user to enter the functions as strings
f_input = input("Enter the first function: ")
g_input = input("Enter the second function: ")
# Create the functions from the user input
f = eval("lambda x: " + f_input)
g = eval("lambda x: " + g_input)
# Create the composition function
composed = compose_functions(f, g)
# Prompt the user to enter an input value
x = float(input("Enter the input value: "))
# Compute the composition of the user input functions
result = composed(x)
print("Result:", result)
Enter the first function: x**2
Enter the second function: 3*x+1
Enter the input value: 3
Result: 100.0
Inverse Functions#
Given a one-to-one function, a new function can sometimes be created by reversing the input and output of the original function. This reversed function is called an inverse function.
The inverse function of a one-to-one function
Composing a function and its inverse will undo what each of the functions do to the input
Example 5
Given
To reverse what
This means the inverse function first subtracts the input
This can easily checked by confirming that
Below is a code example that prompts the user to input a function
Show code cell source
def find_inverse(f):
def inverse(x):
# Use a binary search algorithm to find the inverse
left = -1000 # Starting point for search
right = 1000 # Ending point for search
precision = 0.0001 # Precision of the inverse calculation
while right - left > precision:
mid = (left + right) / 2
if f(mid) < x:
left = mid
else:
right = mid
return round((left + right) / 2, 4) # Return the approximate inverse with 4 decimal places
return inverse
# Prompt the user to enter the function f(x) as a string
f_input = input("Enter the function f(x): ")
# Create the function from the user input
f = eval("lambda x: " + f_input)
# Calculate the inverse function
inverse_f = find_inverse(f)
# Prompt the user to enter a value for x
x = float(input("Enter a value for x: "))
# Calculate the inverse of f(x) at the given x value
inverse_result = inverse_f(x)
# Print the result
print("Inverse of f(x) at x =", x, "is approximately:", inverse_result)
Enter the function f(x): x**3
Enter a value for x: 4
Inverse of f(x) at x = 4.0 is approximately: 1.5874
8.2.5. Exercises#
Exercises
Find the domain of the following functions.
Given the graph of the function
below.Find the intervals on which the graph of
is increasing or decreasingFind the intervals on which the graph of
is concave up or concave downFind the inflection points (if any)
Plot the following functions on the interval
:Given
and . Find each of the following: