The slope of a secant line is the first step toward the derivative.
What to build
Find the slope of the line through two points on a quadratic curve, one at x and one at x + h.
Requirements
- The function is f(x) = a·x² + b·x + c.
- Compute the difference quotient (f(x + h) − f(x)) ÷ h.
- Print it with 4 decimal places.
Input
One line with five numbers: a, b, c, x and h. The number h is not 0.
Output
One line: slope S, with S to 4 decimal places.
Constraints
All numbers are between -50 and 50.
Example
Input:
1 0 0 3 1
Output:
slope 7.0000
Why it works
The difference quotient is the average rate of change between x and x + h. For a quadratic it equals 2ax + b + a·h, so as h shrinks it approaches the derivative 2ax + b.
Examples
1 0 0 3 1
slope 7.0000
1 0 0 3 0.001
slope 6.0010
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Define f(x) = a*x*x + b*x + c, then compute (f(x + h) - f(x)) / h.
Hint 2
Read all five numbers from one line and convert them to floats.
Hint 3
Print with `:.4f`.
Solution
Show one correct solution (try it yourself first)
a, b, c, x, h = [float(v) for v in input().split()]
def f(t):
return a * t * t + b * t + c
print(f"slope {(f(x + h) - f(x)) / h + 0.0:.4f}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step