Title: Composite Functions

Topic: Functions

Difficulty:

Required Skills: Basic math operations, defining and calling functions,

Language: Python

In this activity students will write their own composite functions. They will also write a few composite functions based on real-world type situations. The answers are included below, but students would be given only the comments as a guide.

# Create two functions f and g

def f(x):
 
return(x**2+2)
 
def g(x):
 
return(3*x-1)

# Create the composite function f(g(x)) and call it composite_fg. You can solve for f(g(x)) on paper.

def composite_fg(x):
 
return(9*x**2-6*x+3)

# Compute f(g(3)) using both functions f and g separately and by using composite_fg. Are they equal?

print(f(g(3)))
print(composite_fg(3))

# Create the composite function g(f(x)) and call it composite_gf.

def composite_gf(x):
 
return(3*x**2+5)

# Compute g(f(5)) using both functions g and f separately and by using composite_gf. Are they equal?

print(g(f(5)))
print(composite_gf(5))

# Examples

# You work forty hours a week at a furniture store. You receive a $220 weekly salary, plus a 3% commision on sales over $5000. Assume that you sell enough this week to get the commission. Use composite functions to calculate how much you take home in commission when you sell $8000.

def amount_over(sales):
 
return sales - 5000
 
def commision(amt):
 
return amt*0.03

print(commision(amount_over(8000)))

# You make a purchase at a local hardware store, but what you've bought is too big to take home in your car. For a small fee, you arrange to have the hardware store deliver your purchase for you. You pay for your purchase, plus the sales taxes, plus the fee. The taxes are 7.5% and the fee is $20.  
# Part a - Write a function t(x) for the total, after taxes, on the purchase amount x. Write another function d(x) for the total, including the delivery fee, on the purchase amount x.

def t(x):
 
return(x + 0.075*x)
 
def d(x):
 
return(x+20)
 
# Part b - Calculate and interpret d(t(x)) and t(d(x)). Which results in a lower cost to you?

print(d(t(20)))
print(t(d(20)))

# ANSWER d(t(x)) results in a lower cost because the tax is not being applied to the delivery fee

# Part c - Suppose taxes, by law, are not to be charged on delivery fees. Which composite function must then be used?

# ANSWER d(t(x) must be used