Untitled

 avatar
unknown
plain_text
a year ago
1.0 kB
6
Indexable
import numpy as np
import matplotlib.pyplot as plt

# Define the function and its derivative
def f(x):
    return np.sqrt(x)

def f_prime(x):
    return 1 / (2 * np.sqrt(x))

# Define the interval and points
x = np.linspace(0, 4, 400)
y = f(x)

# Secant line
secant_x = np.array([0, 4])
secant_y = np.array([f(0), f(4)])

# Tangent line at c = 1
c = 1
tangent_slope = f_prime(c)
tangent_y = tangent_slope * (x - c) + f(c)

# Plot the function, secant line, and tangent line
plt.figure(figsize=(10, 6))
plt.plot(x, y, label='$f(x) = \sqrt{x}$', color='blue')
plt.plot(secant_x, secant_y, label='Secant line', color='green', linestyle='--')
plt.plot(x, tangent_y, label='Tangent line at $c = 1$', color='red', linestyle='--')
plt.scatter([0, 4, c], [f(0), f(4), f(c)], color='black')  # Mark the points
plt.text(c, f(c), '  ($c$, $f(c)$)', verticalalignment='bottom', horizontalalignment='right')
plt.xlabel('$x$')
plt.ylabel('$f(x)$')
plt.legend()
plt.title('Graph of $f(x) = \sqrt{x}$ with Secant and Tangent Lines')
plt.grid(True)
plt.show()

Editor is loading...
Leave a Comment