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)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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 .
Transcribed Image Text: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 .
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY