Homework 8

CHE 116: Numerical Methods and Statistics

Prof. Andrew White

Version 1.0 (3/15/2016)


Short Exercises (48 Points)

Answer each using Python. Write out any constraints or rearrangements of equations in Markdown. You must make a graph showing the demonstrating your solution for each problem (4 Points per problem). You must also print your numerical answer.

1.0 See example

Find the maximum x-value for this equation:

$$ \sin(x) - x^2$$

on the domain $[0, \pi]$

1.1

Find the intersection between these two curves:

$$ \frac{(x - 4)^2}{4} $$$$ \frac{(x + 2)^2}{3} $$

1.2

Consider $-p\ln p$, where $p$ is a probability. What $p$ gives the maximum?

1.3

Solve for $x$:

$$ \cos(x) = x $$

1.4

Repeat 1.3 by creating an objective function and minimizing it.

Hint: Try rearranging the equation into an expression that is $0$ when the equation is satisifed and POSITIVE everywhere else.

1.5

Using a similar idea of an objective function, what $x$ most satisfies the following equations:

$$ 4 x + 4 = 12 $$$$ 3x - 2 = 3$$

Hint: you can only minimize one thing, so try adding together multiple objective functions.

1.6

Consider these two curves:

$$f(x) = \cos(14.5 x - 0.3) + ( x + 0.2) x $$$$g(x) = x^3 - x^2 + x$$

Find the $x^*$ that both minimizes $f(x^*)$ and has the property that $f(x^*) > g(x^*)$


In [3]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize as opt

Answer 1.0


In [87]:
#set-up function which we're optimizing
def fxn_10(x):
    return np.sin(x) - 0.2 * x**2

#perform optimization and store result
#The lambda is to invert the sign on the equation to do maximization
result = opt.minimize(lambda can_write_anything_here: -fxn_10(can_write_anything_here), x0= np.pi / 2, bounds=[(0, np.pi)])

#make some points to use for plotting
x_grid = np.linspace(0, np.pi, 100)

#plot the function
plt.plot(x_grid, fxn_10(x_grid))

#create a vertical line at the optimum x value
plt.axvline(result.x, color='red')

plt.show()

print('Optimum x: {}'.format(result.x))


Optimum x: [ 1.1105099]