Question
Consider a finite difference approximation D(x) to the first derivative of a function F, defined by arbitrary coefficients , , and , such that F(x)D(x)=1/x*(*F(xx)+*F(x)+*F(x+x))
Consider a finite difference approximation D(x) to the first derivative of a function F, defined by arbitrary coefficients , , and , such that
F(x)D(x)=1/x*(*F(xx)+*F(x)+*F(x+x))
Write a function derivative(f, x0, coeffs, dx) taking the arguments 'f' (a function), 'x0' (a number), 'coeffs' (a list of 3 numbers), and 'dx' (a positive number representing x), which computes and returns the finite difference approximation D(x0) to the first derivative of a generic function f at the point x0, where D(x) is defined as above.
The list 'coeffs' contains the 3 coefficients ,,,,, in this order.
For example:
Test | Result |
---|---|
import numpy as np def f(x): ''' Constant function f(x) = 0. ''' return np.zeros_like(x) d = derivative(f, 0, [0, -1, 1], 0.01) print(f'{abs(d):.6f}') | 0.000000 |
import numpy as np def f(x): return x*x d = [derivative(f, 1, [-0.5, 0, 0.5], 0.01), derivative(f, 1, [-0.5, 0, 0.5], 0.1), derivative(f, 1, [-0.5, 0, 0.5], 0.5)] print(f'{d[0]:.6f}') print(f'{d[1]:.6f}') print(f'{d[2]:.6f}') | 2.000000 2.000000 2.000000 |
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