Question

Classes and Methods: A class for estimating the derivative of a function f) at the form: takes where h is a small change in x

The following are test cases for the derivative of f()In(x), evaluated at r various values of h Test Cases: Test case 1 10 fo

In Python 3.6

Classes and Methods: A class for estimating the derivative of a function f) at the form: takes where h is a small change in x. The goal of this exercise is to use the formula above to differentinte a mat hematical function f(x) implemented as a Python function f(x) Implement class Diff with two special methods. The--init--() method takes in function object and also an optional argument h. The default value of h is le-4. Implement also the special method call so that the instance is callable. This method returns the approximation of the derivative of the funetion using the formula ahove. The following code shows you how this class can be used to compare the exact value of the derivative versus its approximation for the function f(r) = (1 /4)r'. def f(x) return 0.25x**4 df Diff(f) # make function-like object df " # df (%) computes the derivative of f(z) approzi«ately: for x in [.5, 101: d1.value dt (x) # appro# V4tue of derivative of ! at Point # 4 ezact vale of derivative df.value))
The following are test cases for the derivative of f()In(x), evaluated at r various values of h Test Cases: Test case 1 10 for Input Output: (0.09950330853167877, 0.000496691468321 2365) # derivative, approxi- mation erro Test case 2 x = 10.0, f = log, h = 0.5, Input Output: (0.09758032833886343, 0.0024196716611365743) Test case 3 10.0, f = log, h 1.0E-5, Input Output: (0.09999994996512383, 5.003487617283309e-08) x Test case 4 10.0, f= log, h = 1.0E-9. Input Output: (0.1000000082740371, -8.274037094357922e-09) Test case 5 x-10.0, f= log, h = 1.0E-11, Input Output: (0.0999644811372491, 3.551886275091065e-05)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code for the implementation of class Diff:

import math

class Diff:
def __init__(self,f,h=math.exp(-4)):
self.f=f;
self.h=h;
  
def __call__(self,x):
return (self.f(x+self.h)-self.f(x))/self.h;

----------------------------------------------------

please provide positive feedback if it helped

Add a comment
Know the answer?
Add Answer to:
Classes and Methods: A class for estimating the derivative of a function f) at the form: takes wh...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. (a) A function f has first derivative f (r) - and second derivative f"(z) It is also known that the function f h...

    4. (a) A function f has first derivative f (r) - and second derivative f"(z) It is also known that the function f has r-intercept at (-3,0), and a y-intercept at (0,0) (i) Find all critical points, and use them to identify the intervals over which you will examine the behaviour of the first derivative (ii) Use the f'(x), and the First Derivative Test to classify each critical point. (iii) Use the second derivative to examine the concavity around critical...

  • Use Mat Lab to write a function called forwarddiff.m that takes data sample locations and function...

    Use Mat Lab to write a function called forwarddiff.m that takes data sample locations and function samplings at those locations as inputs and returns an approximation of the derivative at the sample points based on forward finite difference method with order of accuracy O(h^3). The function should display nothing to the screen unless an error or warning occurs. The detailed specification is as follows: The approximation of derivative by forward difference method (for O(h^3) is given as: f' (x_1) =...

  • )and second derivative 4. (a) A function f has first derivative f'(x) f(E) It is also known that the function f has...

    )and second derivative 4. (a) A function f has first derivative f'(x) f(E) It is also known that the function f has r-intercept at (-3,0), and a y-intercept at (0, Q) (i) Find all critical points, and use them to identify the intervals over which you will examine the behaviour of the first derivative [3 marks] (ii) Use the f(x), and the First Derivative Test to classify each critical point.[3 marks] (ii) Use the second derivative to examine the concavity...

  • 4. (a) A function f has first derivative f') and second derivative It is also known that the function f has r-i...

    4. (a) A function f has first derivative f') and second derivative It is also known that the function f has r-intercept at (-3,0) and a y-intercept at (0,0) 0) Find all critical points, and use them to identify the intervals over which you will examine the behaviour of the first derivative. (ii) Use the f'(x), and the First Derivative Test to classify each critical point. (ii) Use the second derivative to examine the concavity around critical points that are...

  • 4. (a) A function f has first derivative f' (x) - and second derivative f"(x) It is also known that the functio...

    4. (a) A function f has first derivative f' (x) - and second derivative f"(x) It is also known that the function f has r-intercept at (-3,0), and a y-intercept at (0,0) (i) Find all critical points, and use them to identify the intervals over which you will examine the behaviour of the first derivative. 3 marks] (ii) Use the f'(x), and the First Derivative Test to classify each critical point. 3 marks (iii) Use the second derivative to examine...

  • 4. (a) A function f has first derivative f'(x) and second derivative 2 f" (x) It is also known that the functio...

    4. (a) A function f has first derivative f'(x) and second derivative 2 f" (x) It is also known that the function f has r-intercept at (-3,0), and a y-intercept at (0,0) (i) Find all critical points, and use them to identify the intervals over which you will examine the behaviour of the first derivative. 3 marks] (ii) Use the f(x), and the First Derivative Test to classify each critical point.[3 marks] (iii) Use the second derivative to examine the...

  • 4. (a) A function f has first derivative f'(r) and second derivative It is also known that the function f has r-int...

    4. (a) A function f has first derivative f'(r) and second derivative It is also known that the function f has r-intercept at (-3,0), and a y-intercept at (0, 0) i) Find all critical points, and use them to identify the intervals over which you will examine the behaviour of the first derivative 3 marks (İİ) Úse the f,(x), and the First Derivative Test to classify each critical point. [3 marks] Iİİ) Úse the second derivative to examine the concavity...

  • write a program to compute an approximate value for the derivative of a function using the...

    write a program to compute an approximate value for the derivative of a function using the finite difference formula f ′ (x) ≈ f(x + h) − f(x) h . (2) Test your program using the function sin(x) for x = 1. Determine the error by comparing with the built-in function cos(x). Plot the magnitude of the error as a function of h, for h = 1 2 , 1 4 , 1 8 , . . . You should...

  • 4. The first derivative of a function f(x) at a point x = xo can be approximated with the four-po...

    Solve using MATLAB and provide code please 4. The first derivative of a function f(x) at a point x = xo can be approximated with the four-point central difference formula: dx 12h where h is a small number relative to xo. Write a user-defined function function that calculates the derivative of a math function fx) by using the four-point central difference formula. For the user-defined function name, use dfax-FoPrder(Fun, x0), where Fun is a name for the function that is...

  • question starts at let. than one variable. Let f:R? → R3 be the function given by...

    question starts at let. than one variable. Let f:R? → R3 be the function given by f(x, y) = (cos(x3 - y2), sin(y2 – x), e3x2-x-2y). (a) Let P be a point in the domain of f. As we saw in class, for (x, y) near P, we have f(x, y) f(P) + (Dpf)(h), where h = (x, y) - P. The expression on the right hand side is called the linear approximation of f around P. Compute the linear...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT