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.
Find the maximum x-value for this equation:
$$ \sin(x) - x^2$$on the domain $[0, \pi]$
Find the intersection between these two curves:
$$ \frac{(x - 4)^2}{4} $$$$ \frac{(x + 2)^2}{3} $$Consider $-p\ln p$, where $p$ is a probability. What $p$ gives the maximum?
Solve for $x$:
$$ \cos(x) = x $$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.
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.
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 [2]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize as opt
In [3]:
#set-up function which we're optimizing
def fxn_10(x):
return np.sin(x) - x**2
#perform optimization and store result
#invert the sign on the equation to to 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))
In [85]:
def fxn_11(x):
return (x - 4)**2 / 4 - (x + 2)**2 / 3
x = opt.newton(fxn_11, x0=0)
print(x)
grid = np.linspace(-8, 8, 100)
plt.plot(grid, (grid - 4)**2 / 4)
plt.plot(grid, (grid + 2)**2 / 3)
plt.axvline(x, color='red')
plt.show()
In [89]:
#set-up function which we're optimizing
def fxn_12(p):
return -p * np.log(p)
#perform optimization and store result
#invert the sign on the equation to to maximization
result = opt.minimize(lambda u: -fxn_12(u), x0= 0.5, bounds=[(0, 1)])
#make some points to use for plotting
p_grid = np.linspace(0.01, 0.99, 100)
#plot the function
plt.plot(p_grid, fxn_12(p_grid))
#create a vertical line at the optimum x value
plt.axvline(result.x, color='red')
plt.show()
print(result.x)
In [43]:
x_opt = opt.newton(lambda g: np.cos(g) - g, x0=0)
print(x_opt)
x_grid = np.linspace(0,np.pi, 100)
plt.plot(x_grid, x_grid)
plt.plot(x_grid, np.cos(x_grid))
plt.axvline(x_opt, color='purple')
plt.show()
In [46]:
x_opt = opt.minimize(lambda g: (np.cos(g) - g)**2, x0=0)
x_opt = x_opt.x # remember the minimize function returns a bunch of extra crap
print(x_opt)
x_grid = np.linspace(0,np.pi, 100)
plt.plot(x_grid, x_grid)
plt.plot(x_grid, np.cos(x_grid))
plt.axvline(x_opt, color='purple')
plt.show()
In [57]:
def fxn_15(x):
return (4 * x - 8 )**2 + (3 * x - 5)**2
x_opt = opt.minimize(fxn_15, x0=0)
x_opt = x_opt.x
print(x_opt)
#not a great
x_grid = np.linspace(1,3, 100)
plt.plot(x_grid, 4 *x_grid - 8)
plt.plot(x_grid, 3 * x_grid- 5)
plt.axhline(0, color='red')
plt.axvline(x_opt,color='red')
plt.show()
In [91]:
def non_convex(x):
return np.cos(14.5 * x - 0.3) + (x + 0.2) * x
def poly(x):
return x ** 3 - x**2 + x
my_constraints = {'type': 'ineq', 'fun': lambda y: non_convex(y) - poly(y)}
kwargs = {'constraints': my_constraints}
result = opt.basinhopping(non_convex, x0=0, minimizer_kwargs=kwargs, niter=1000)
print(result)
x_opt = result.x
x_grid = np.linspace(-2, 2, 100)
plt.plot(x_grid, non_convex(x_grid))
plt.plot(x_grid, poly(x_grid))
plt.axvline(x_opt, color='orange')
plt.ylim(-1, 2)
plt.show()
print(x_opt)
In [ ]: