A bound is a resitrction on the domain. A constraint is an arbitrary function or inequality that must be satisfied for a solution
Number of dimnsions, if it's non-convex, if it's discrete, if there are bounds or constraints
Reaction equilibrium problems have two solutions if you do not bound the domain, giving a non-convex problem. Other examples are possible.
Solve and plot the following expressions using python. Plot a red dot at your solution.
[2 + 2 points] Solve $e^{x / 4} = x$
[3 points] You do not need to plot this problem. Solve the following system of equations and report your answer.
In [15]:
import scipy.optimize as opt
import numpy as np
import matplotlib.pyplot as plt
def prob1(x):
return (x - 4)**4 / 8 + (x - 2)**2 / 2 - 4
r1 = opt.root(prob1, x0=0)
r2 = opt.root(prob1, x0=8)
x = np.linspace(1, 5, 500)
plt.plot(x, prob1(x))
plt.plot([r1.x, r2.x], [0, 0], 'ro')
plt.show()
print("The solution is {:.2f} and {:.2f}".format(r1.x[0], r2.x[0]))
In [9]:
def prob2(r):
return 4 * r**-8 - 4 * r**-4
r = np.linspace(0.9,3, 1000)
m = opt.minimize(prob2, bounds=[(0.9,3)], x0=1)
print("The minimum value occurs when x = {:.3f} and it is {:.1f}".format(m.x[0],m.fun[0]))
plt.plot(r, prob2(r))
plt.plot(m.x, prob2(m.x), 'ro')
plt.show()
In [16]:
def prob3(x):
return np.exp(x / 4) - x
x = np.linspace(0,3, 100)
i = opt.root(prob3, x0=0)
plt.plot(x, np.exp(x / 4))
plt.plot(x, x)
plt.plot(i.x, i.x, 'ro')
plt.show()
print("the solution is {:.2f}".format(i.x[0]))
In [18]:
def prob4(x):
y1 = np.sqrt(x[0]) * 2 - 3 * np.cos(x[1]) - 3
y2 = 2 * x[0] - x[1]**2 - 18
return y1**2 + y2**2
r = opt.minimize(prob4, x0=[0,0], bounds=[(0,100), (-10,10)])
print(f'x = {r.x[0]}, z = {r.x[1]}')