Skip to content

Commit e5e9103

Browse files
authored
New Problem - Polynomial Derivative
Created a new problem where the user has to find the numerical derivative of a simple polynomial function. Includes edge case when the exponent is zero.
1 parent e2ebc60 commit e5e9103

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Derivative of a Polynomial
2+
3+
A function's derivative is a way of quantifying the function's slope at a given point. It allows us to understand whether the function is increasing, decreasing or flat at specific input.
4+
5+
Taking the derivative of a polynomial at a single point follows a straight-forward rule. This question will show the rule and the edge case you should be on the look-out for.
6+
7+
### Mathematical Definition
8+
9+
When calculating the slope of a function $f(x)$, we usually require two points $x_{1}$ and $x_{2}$ and use the following formula:
10+
11+
$$
12+
\frac{f(x_{2}) - f(x_{1})}{x_{2} - x_{1}}
13+
$$
14+
15+
A derivative generalizes that notion by calculating the slope of a function at a specific point.
16+
A derivative of a function $f(x)$ is mathematically defined as:
17+
18+
$$
19+
\frac{d f(x)}{d x} = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}
20+
$$
21+
22+
Where:
23+
- $x$ is the input to the function
24+
- $h$ is the "step", which is equivalent to the difference $x_{2} - x_{1}$ in the two-point slope-formula
25+
26+
Taking the limit as the step grows smaller and smaller, allow us to quantify the slope at a certain point, instead of having to consider two points as in other methods of finding the slope.
27+
28+
When taking the derivative of a polynomial function $x^{n}$, where $n \neq 0$, then the derivative is: $n x^{n-1}$. In the special case where $n = 0$ then the derivative is zero. This is because $x^{0} = 1$ if $x \neq 0$.
29+
30+
A positive derivative indicates that the function is increasing in that point, a negative derivative indicates that the function is decreasing at that point. A derivative equal to zero indicates that the function is flat, which could potentially indicate a function's minimum or maximum.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def polynomial_derivative(x: float,n: float) -> float:
2+
if n == 0.0:
3+
return 0.0
4+
return round(n*(x**(n-1)),4)
5+
6+
def test_polynomial_derivative():
7+
# Test case 1
8+
x,n = 10.0,0.0
9+
expected_output = 0.0
10+
assert polynomial_derivative(x,n) == expected_output, "Test case 1 failed"
11+
12+
# Test case 2
13+
x,n = 6.0,5.0
14+
expected_output = 6480.0
15+
assert polynomial_derivative(x,n) == expected_output, "Test case 2 failed"
16+
17+
# Test case 3
18+
x,n = 12.0,4.3
19+
expected_output = 15659.0917
20+
assert polynomial_derivative(x,n) == expected_output, "Test case 3 failed"
21+
22+
# Test case 4
23+
x,n = 14.35,1.0
24+
expected_output = 1.0
25+
assert polynomial_derivative(x,n) == expected_output, "Test case 4 failed"
26+
27+
if __name__ == "__main__":
28+
test_polynomial_derivative()
29+
print("All Polynomial Derivative tests passed.")

0 commit comments

Comments
 (0)