Answered step by step
Verified Expert Solution
Link Copied!

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:
$$
\frac{\partial f}{\partial x}\approx \frac{f(x +\Delta)- f(x)}{\Delta}
$$
def derivate_approx(f, x, varval, delta=0.0001):
"""Computes the derivative of f with respect to x, for a given delta,
using the (f(x + delta)- f(x))/ delta method. """
# This is f(x)
f_x = compute(f, varval=varval)
varval_delta = dict(varval)
varval_delta[x]+= delta
# This is f(x + delta)
f_x_plus_delta = compute(f, varval=varval_delta)
return (f_x_plus_delta - f_x)/ delta
def similar(x, y, epsilon):
if x <0 and y <0:
# If they are negative, max and min play opposite roles.
return similar(-x,-y, epsilon)
if abs(x - y)< epsilon:
return True
else:
return max(x, y)/(min(x, y)+ epsilon)<1+ epsilon
#@title Implementation of `test_derivative`
def test_derivative(f, df, x, delta=0.0001, tolerance=0.01, num_tests=1000):
"""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 10 points: `test_derivative`
f =("+",("*", "cat", "cat"),("*", "dog", "cat"))
df1=("+",("*",2, "cat"), "dog")
df2=("+",("*",2, "cat"),("*", "dog", "cat"))
assert test_derivative(f, df1, "cat")== True
assert test_derivative(f, df2, "cat")== False
assert test_derivative(f, df1, "dog")== False
assert test_derivative(f, df1, "donkey")== False
assert test_derivative(f,0, "donkey")== True

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

Medibank Data Hacking case facts

Answered: 1 week ago