Answered step by step
Verified Expert Solution
Question
1 Approved Answer
One of the best ways of testing a solution to a difficult problem consists in implementing a different solution, and then comparing the two. To
One of the best ways of testing a solution to a difficult problem consists in implementing a different solution, and then comparing the two.
To help test your implementation of derivative, we can use the following property of derivative:
$$
fracpartial fpartial xapprox fracfx Delta fxDelta
$$
def derivateapproxf x varval, delta:
Computes the derivative of f with respect to x for a given delta,
using the fx delta fx delta method.
# This is fx
fx computef varvalvarval
varvaldelta dictvarval
varvaldeltax delta
# This is fx delta
fxplusdelta computef varvalvarvaldelta
return fxplusdelta fx delta
def similarx y epsilon:
if x and y :
# If they are negative, max and min play opposite roles.
return similarxy epsilon
if absx y epsilon:
return True
else:
return maxx yminx y epsilon epsilon
#@title Implementation of testderivative
def testderivativef df x delta tolerance numtests:
Tests if the derivative of f with respect to x is approximately equal to df
Returns True if the test passes for all randomly generated inputs, False otherwise."""
## YOUR SOLUTION HERE
Needs to pass following test:
# Tests points: testderivative
f "cat", "cat" "dog", "cat"
df "cat" "dog"
df "cat" "dog", "cat"
assert testderivativef df "cat" True
assert testderivativef df "cat" False
assert testderivativef df "dog" False
assert testderivativef df "donkey" False
assert testderivativef "donkey" True
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started