Homework 11 Key

CHE 116: Numerical Methods and Statistics

4/11/2019


1. Conceptual Questions (9 Points)

3 Points each

  1. What is the difference between a bound and a constraint?
  2. What must you consider before choosing a method for minimization?
  3. Give an example of a non-convex problem you might see in chemical engineering.

1.1

A bound is a resitrction on the domain. A constraint is an arbitrary function or inequality that must be satisfied for a solution

2.2

Number of dimnsions, if it's non-convex, if it's discrete, if there are bounds or constraints

2.3

Reaction equilibrium problems have two solutions if you do not bound the domain, giving a non-convex problem. Other examples are possible.

2. Optimization Problems (15 Points)

Solve and plot the following expressions using python. Plot a red dot at your solution.

  1. [2 points (plot) + 2 points (solution)] Find all roots of this expression:
$$ \frac{(x−4)^4}{8}+ \frac{(x−2)^2}{2} - 4 $$
  1. [2 + 2 points] Find the minimum of this expression with $r$ bounded to be $[0.9,3]$
$$ 4\left[\frac{1}{r^8} - \frac{1}{r^4}\right] $$
  1. [2 + 2 points] Solve $e^{x / 4} = x$

  2. [3 points] You do not need to plot this problem. Solve the following system of equations and report your answer.

$$ 2\sqrt{x} - 3 \cos z = 3 $$$$ 2 x - z^2 = 18 $$

2.1


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]))


The solution is 1.63 and 4.81

2.2


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()


The minimum value occurs when x = 1.189 and it is -1.0

2.3


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]))


the solution is 1.43

2.4


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]}')


x = 8.999999994920964, z = 0.0