Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Implementing Identity-Quadratic Functions in Python | Functions and Their Properties
Mathematics for Data Science

bookImplementing Identity-Quadratic Functions in Python

Identity Function

The identity function returns the input value unchanged, following the form f(x)=xf(x) = x. In Python, we implement it as:

# Identity Function
def identity_function(x):
    return x

The identity function returns the input value unchanged, following the form f(x)=xf(x)=x. To visualize it, we generate x-values from -10 to 10, plot the line, mark the origin (0,0)(0,0), and include labeled axes and grid lines for clarity.

12345678910111213141516171819202122232425
import numpy as np import matplotlib.pyplot as plt # Identity Function def identity_function(x): return x x = np.linspace(-10, 10, 100) y = identity_function(x) plt.plot(x, y, label="f(x) = x", color='blue', linewidth=2) plt.scatter(0, 0, color='red', zorder=5) # Mark the origin # Add axes plt.axhline(0, color='black', linewidth=1) plt.axvline(0, color='black', linewidth=1) # Add labels plt.xlabel("x") plt.ylabel("f(x)") # Add grid plt.grid(True, linestyle='--', alpha=0.6) plt.legend() plt.title("Identity Function: f(x) = x") plt.show()
copy

Constant Function

A constant function always returns the same output, regardless of the input. It follows f(x)=cf(x) = c.

# Constant Function
def constant_function(x, c):
    return np.full_like(x, c)

A constant function always returns the same output, regardless of the input, following the form f(x)=cf(x) = c. To visualize it, we generate x-values from -10 to 10 and plot a horizontal line at y=5y = 5. The plot includes axes, labels, and a grid for clarity.

123456789101112131415161718
import numpy as np import matplotlib.pyplot as plt def constant_function(x, c): return np.full_like(x, c) x = np.linspace(-10, 10, 100) y = constant_function(x, c=5) plt.plot(x, y, label="f(x) = 5", color='blue', linewidth=2) plt.axhline(0, color='black', linewidth=1) plt.axvline(0, color='black', linewidth=1) plt.xlabel("x") plt.ylabel("f(x)") plt.grid(True, linestyle='--', alpha=0.6) plt.legend() plt.title("Constant Function: f(x) = 5") plt.show()
copy

Linear Function

A linear function follows the form f(x)=mx+bf(x) = mx + b, where mm represents the slope and bb the y-intercept.

# Linear Function
def linear_function(x, m, b):
    return m * x + b

A linear function follows the form f(x)=mx+bf(x) = mx + b, where mm is the slope and bb is the y-intercept. We generate x-values from -20 to 20 and plot the function with both axes, a grid, and marked intercepts.

1234567891011121314151617181920
import numpy as np import matplotlib.pyplot as plt def linear_function(x, m, b): return m * x + b x = np.linspace(-20, 20, 400) y = linear_function(x, m=2, b=-5) plt.plot(x, y, color='blue', linewidth=2, label="f(x) = 2x - 5") plt.scatter(0, -5, color='red', label="Y-Intercept") plt.scatter(2.5, 0, color='green', label="X-Intercept") plt.axhline(0, color='black', linewidth=1) plt.axvline(0, color='black', linewidth=1) plt.xlabel("x") plt.ylabel("f(x)") plt.grid(True, linestyle='--', alpha=0.6) plt.legend() plt.title("Linear Function: f(x) = 2x - 5") plt.show()
copy

Quadratic Function

A quadratic function follows f(x)=ax2+bx+cf(x) = ax^2 + bx + c, creating a parabolic curve. Key features include the vertex and x-intercepts.

# Quadratic Function
def quadratic_function(x):
    return x**2 - 4*x - 2

A quadratic function follows f(x)=ax2+bx+cf(x) = ax^2 + bx + c, forming a parabolic curve. We generate x-values from -2 to 6, plot the function, and mark the vertex and intercepts. The plot includes both axes, a grid, and labels.

12345678910111213141516171819202122232425
import numpy as np import matplotlib.pyplot as plt def quadratic_function(x): return x**2 - 4*x - 2 x = np.linspace(-2, 6, 200) y = quadratic_function(x) plt.plot(x, y, color='blue', linewidth=2, label="f(x) = xΒ² - 4x - 2") plt.scatter(2, quadratic_function(2), color='red', label="Vertex (2, -6)") plt.scatter(0, quadratic_function(0), color='green', label="Y-Intercept (0, -2)") # X-intercepts from quadratic formula x1, x2 = (4 + np.sqrt(24)) / 2, (4 - np.sqrt(24)) / 2 plt.scatter([x1, x2], [0, 0], color='orange', label="X-Intercepts") plt.axhline(0, color='black', linewidth=1) plt.axvline(0, color='black', linewidth=1) plt.xlabel("x") plt.ylabel("f(x)") plt.grid(True, linestyle='--', alpha=0.6) plt.legend() plt.title("Quadratic Function: f(x) = xΒ² - 4x - 2") plt.show()
copy
question mark

Which code correctly defines a quadratic function in Python that calculates (f(x) = x^2 - 4x - 2)?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how to interpret the graphs for each function?

What are the key differences between the identity, constant, linear, and quadratic functions?

Can you help me modify one of these functions for a different example?

Awesome!

Completion rate improved to 1.96

bookImplementing Identity-Quadratic Functions in Python

Swipe to show menu

Identity Function

The identity function returns the input value unchanged, following the form f(x)=xf(x) = x. In Python, we implement it as:

# Identity Function
def identity_function(x):
    return x

The identity function returns the input value unchanged, following the form f(x)=xf(x)=x. To visualize it, we generate x-values from -10 to 10, plot the line, mark the origin (0,0)(0,0), and include labeled axes and grid lines for clarity.

12345678910111213141516171819202122232425
import numpy as np import matplotlib.pyplot as plt # Identity Function def identity_function(x): return x x = np.linspace(-10, 10, 100) y = identity_function(x) plt.plot(x, y, label="f(x) = x", color='blue', linewidth=2) plt.scatter(0, 0, color='red', zorder=5) # Mark the origin # Add axes plt.axhline(0, color='black', linewidth=1) plt.axvline(0, color='black', linewidth=1) # Add labels plt.xlabel("x") plt.ylabel("f(x)") # Add grid plt.grid(True, linestyle='--', alpha=0.6) plt.legend() plt.title("Identity Function: f(x) = x") plt.show()
copy

Constant Function

A constant function always returns the same output, regardless of the input. It follows f(x)=cf(x) = c.

# Constant Function
def constant_function(x, c):
    return np.full_like(x, c)

A constant function always returns the same output, regardless of the input, following the form f(x)=cf(x) = c. To visualize it, we generate x-values from -10 to 10 and plot a horizontal line at y=5y = 5. The plot includes axes, labels, and a grid for clarity.

123456789101112131415161718
import numpy as np import matplotlib.pyplot as plt def constant_function(x, c): return np.full_like(x, c) x = np.linspace(-10, 10, 100) y = constant_function(x, c=5) plt.plot(x, y, label="f(x) = 5", color='blue', linewidth=2) plt.axhline(0, color='black', linewidth=1) plt.axvline(0, color='black', linewidth=1) plt.xlabel("x") plt.ylabel("f(x)") plt.grid(True, linestyle='--', alpha=0.6) plt.legend() plt.title("Constant Function: f(x) = 5") plt.show()
copy

Linear Function

A linear function follows the form f(x)=mx+bf(x) = mx + b, where mm represents the slope and bb the y-intercept.

# Linear Function
def linear_function(x, m, b):
    return m * x + b

A linear function follows the form f(x)=mx+bf(x) = mx + b, where mm is the slope and bb is the y-intercept. We generate x-values from -20 to 20 and plot the function with both axes, a grid, and marked intercepts.

1234567891011121314151617181920
import numpy as np import matplotlib.pyplot as plt def linear_function(x, m, b): return m * x + b x = np.linspace(-20, 20, 400) y = linear_function(x, m=2, b=-5) plt.plot(x, y, color='blue', linewidth=2, label="f(x) = 2x - 5") plt.scatter(0, -5, color='red', label="Y-Intercept") plt.scatter(2.5, 0, color='green', label="X-Intercept") plt.axhline(0, color='black', linewidth=1) plt.axvline(0, color='black', linewidth=1) plt.xlabel("x") plt.ylabel("f(x)") plt.grid(True, linestyle='--', alpha=0.6) plt.legend() plt.title("Linear Function: f(x) = 2x - 5") plt.show()
copy

Quadratic Function

A quadratic function follows f(x)=ax2+bx+cf(x) = ax^2 + bx + c, creating a parabolic curve. Key features include the vertex and x-intercepts.

# Quadratic Function
def quadratic_function(x):
    return x**2 - 4*x - 2

A quadratic function follows f(x)=ax2+bx+cf(x) = ax^2 + bx + c, forming a parabolic curve. We generate x-values from -2 to 6, plot the function, and mark the vertex and intercepts. The plot includes both axes, a grid, and labels.

12345678910111213141516171819202122232425
import numpy as np import matplotlib.pyplot as plt def quadratic_function(x): return x**2 - 4*x - 2 x = np.linspace(-2, 6, 200) y = quadratic_function(x) plt.plot(x, y, color='blue', linewidth=2, label="f(x) = xΒ² - 4x - 2") plt.scatter(2, quadratic_function(2), color='red', label="Vertex (2, -6)") plt.scatter(0, quadratic_function(0), color='green', label="Y-Intercept (0, -2)") # X-intercepts from quadratic formula x1, x2 = (4 + np.sqrt(24)) / 2, (4 - np.sqrt(24)) / 2 plt.scatter([x1, x2], [0, 0], color='orange', label="X-Intercepts") plt.axhline(0, color='black', linewidth=1) plt.axvline(0, color='black', linewidth=1) plt.xlabel("x") plt.ylabel("f(x)") plt.grid(True, linestyle='--', alpha=0.6) plt.legend() plt.title("Quadratic Function: f(x) = xΒ² - 4x - 2") plt.show()
copy
question mark

Which code correctly defines a quadratic function in Python that calculates (f(x) = x^2 - 4x - 2)?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
some-alt