QuestionQuestion

Assignment

1. In a file called optimize.py, write a funtion, called optimize_step, the finds the largest value of a   function, between a set of bounds when called like this:
x = optimize_step(f, bounds, n),
where f is the function, bounds is a tuple specifying the lower (inclusive) and upper (exclusive) bounds, and n is the number of steps. This function should start at the lower bound, and take n evenly-spaced evaluations of f, returning the x value of the largest one.

2. Write another function, optimize_random, that takes the same arguments, but uses n random samples between the bounds.

3. Compare the accuracy of your two functions and the Python optimization function we talked about in
class (minimize_scalar) to the actual x value of the maximal value, on a couple of different functions.
Show one function for which the built-in optimizer works well, and one for which it works poorly. Graph the performance of your two approaches as a function of the number of function evaluations you make, and show the accuracy of the built-in function on the same graph, noting how many evaluations it used.

4. In a file called integrate.py, write a function, called integrate_mc, that calculates and returns a Monte Carlo approximation to the definite integral of a function, and can be called like this:
i = integrate_mc(f, bounds, n) where f is the function, bounds are the lower and upper bounds, and n is the number of Monte Carlo samples. You can assume that the function is strictly positive over the integration interval.

5. Graph the approximation error, as a function of the number of samples, of your method for some function.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

def ack(m, n):

    """
    Evaluates the ackermann function
    """

    if m == 0:
       return n + 1
    elif (m > 0) and (n == 0):
       return ack(m-1, 1)
    elif (m > 0) and (n > 0):
       return ack(m-1, ack(m, n-1))

# for larger values of m and n, maximum recursion depth is exceeded

# Exercise 4

def is_power(a, b):
    if a == b:
       return True
    return (a % b == 0) and is_power(a/b, b)

# Exercise 5

def gcd(a, b):

    if a == b or b == 0:
       return a
    elif a > b:
       return gcd(b, a)
    return gcd(a, b % a)...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$13.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats