1. def f(x): """ Evaluates function `f(x) = x^2 - 15 \sin(x * \pi/3)` Parameters ---------- x : array_like Input(s) to the function Returns ------- out : ndarray Function `f`, evaluated at point(s) x """ # your code here 2. def grad_f(x): """ Evaluates gradient of a function `f(x) = x^2 - 15 \sin(x * \pi/3)` Parameters ---------- x : array_like Point(s) at which gradient should be avalueated Returns ------- out : ndarray Gradient of the function `f` evaluaed at point(s) x """ # your code here # TEST 1. f(x) assert f(0.) == 0. assert np.allclose(f(np.array([2.5, 7.5])), np.array([-1.25, 41.25])) assert np.allclose(f(np.arange(-10, 10, 1)), np.arange(-10, 10, 1)**2 - 15*np.sin(np.pi/3*np.arange(-10, 10, 1))) x_min = 1.33668375 assert np.allclose(f(x_min), -12.9944407) # TEST 2. grad_f(x) assert np.allclose(grad_f(0.), -15.7079) assert isinstance(grad_f(0.), float) assert np.allclose(grad_f(0), - 5*np.pi*np.cos(0)), 'Gradient at point 0 is wrong' assert np.allclose(grad_f(np.arange(-10, 10, 1)), 2*np.arange(-10, 10, 1) - 5*np.pi*np.cos(np.pi/3*np.arange(-10, 10, 1))) x_min = 1.33668375 assert np.allclose(grad_f(x_min), 0, atol=1e-05)
1. def f(x):
"""
Evaluates function `f(x) = x^2 - 15 \sin(x * \pi/3)`
Parameters
----------
x : array_like
Input(s) to the function
Returns
-------
out : ndarray
Function `f`, evaluated at point(s) x
"""
# your code here
2. def grad_f(x):
"""
Evaluates gradient of a function `f(x) = x^2 - 15 \sin(x * \pi/3)`
Parameters
----------
x : array_like
Point(s) at which gradient should be avalueated
Returns
-------
out : ndarray
Gradient of the function `f` evaluaed at point(s) x
"""
# your code here
# TEST 1. f(x)
assert f(0.) == 0.
assert np.allclose(f(np.array([2.5, 7.5])), np.array([-1.25, 41.25]))
assert np.allclose(f(np.arange(-10, 10, 1)), np.arange(-10, 10, 1)**2 - 15*np.sin(np.pi/3*np.arange(-10, 10, 1)))
x_min = 1.33668375
assert np.allclose(f(x_min), -12.9944407)
# TEST 2. grad_f(x)
assert np.allclose(grad_f(0.), -15.7079)
assert isinstance(grad_f(0.), float)
assert np.allclose(grad_f(0), - 5*np.pi*np.cos(0)), 'Gradient at point 0 is wrong'
assert np.allclose(grad_f(np.arange(-10, 10, 1)), 2*np.arange(-10, 10, 1) - 5*np.pi*np.cos(np.pi/3*np.arange(-10, 10, 1)))
x_min = 1.33668375
assert np.allclose(grad_f(x_min), 0, atol=1e-05)
![Consider a function
f(x) = x² – 15 sin(x).
Implement funtions f(x) and grad_f(x), which evaluate function and its gradient in any given point x .](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fa040abc5-8ab2-450b-b204-641f94fadada%2Ff3c7614d-52d7-4569-a947-f12ad177483f%2Fk1l7nk_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)