-from enthought.mayavi import mlab
+#from enthought.mayavi import mlab
import time
import numpy as np
-import matplotlib.pyplot as plt
+#import matplotlib.pyplot as plt
import surf
from steepest import steepest
from function import function_g
f_newton = []
f_nwst = []
- mlab.figure(figure="Surface", bgcolor=(1,1,1), fgcolor=(0,0,0))
-
- plen = 15
- for point in optimal_points[:plen]:
- z = function_g(point, H, b, c)
- f_optimal.append(z)
- p = mlab.points3d([point[0]], [point[1]], [z], scale_factor=0.03, color=(0.7,0.2,0.2))
-
- for point in constant_points[:plen]:
- z = function_g(point, H, b, c)
- f_constant.append(z)
- p = mlab.points3d([point[0]], [point[1]], [z], scale_factor=0.02, color=(1,1,1))
-
- for point in newton_points[:plen]:
- z = function_g(point, H, b, c)
- f_newton.append(z)
-
- for point in nwst_points[:plen]:
- z = function_g(point, H, b, c)
- f_nwst.append(z)
-
- last = optimal_points[len(optimal_points)-1]
- X, Y, Z = surf.surf(H, b, c, last[0]-1, last[0]+1, last[1]-1, last[1]+1, -10, 3)
-
- surface = mlab.mesh(X, Y, Z)
- axes = mlab.axes(color=(0,0,0))
- mlab.outline()
- mlab.show()
-
- fig = plt.figure(1)
- plt.rc('mathtext', default='regular')
- fig.subplots_adjust(hspace=0.4)
- ax = plt.subplot(211)
- plt.title("Relative residual")
- plt.xlabel("Iteration")
- plt.ylabel("Relative residual")
- plt.plot(optimal_residuals[:plen])
- plt.plot(constant_residuals[:plen])
- plt.plot(newton_residuals[:plen])
- plt.plot(nwst_residuals[:plen])
- plt.legend([r'Optimal $\alpha$', r'$\alpha$ = %.2f' % alpha, r'Newtons method', r'Steepest first, then Newton'])
- ax.set_yscale('log')
-
- ax = plt.subplot(212)
- plt.title("Function value")
- plt.xlabel("Iteration")
- plt.ylabel("g(x)")
- plt.plot(f_optimal[:plen])
- plt.plot(f_constant[:plen])
- plt.plot(f_newton[:plen])
- plt.plot(f_nwst[:plen])
-
- plt.show()
- fig.savefig("steepest.eps", format="eps")
+# mlab.figure(figure="Surface", bgcolor=(1,1,1), fgcolor=(0,0,0))
+#
+# plen = 15
+# for point in optimal_points[:plen]:
+# z = function_g(point, H, b, c)
+# f_optimal.append(z)
+# p = mlab.points3d([point[0]], [point[1]], [z], scale_factor=0.03, color=(0.7,0.2,0.2))
+#
+# for point in constant_points[:plen]:
+# z = function_g(point, H, b, c)
+# f_constant.append(z)
+# p = mlab.points3d([point[0]], [point[1]], [z], scale_factor=0.02, color=(1,1,1))
+#
+# for point in newton_points[:plen]:
+# z = function_g(point, H, b, c)
+# f_newton.append(z)
+#
+# for point in nwst_points[:plen]:
+# z = function_g(point, H, b, c)
+# f_nwst.append(z)
+#
+# last = optimal_points[len(optimal_points)-1]
+# X, Y, Z = surf.surf(H, b, c, last[0]-1, last[0]+1, last[1]-1, last[1]+1, -10, 3)
+#
+# surface = mlab.mesh(X, Y, Z)
+# axes = mlab.axes(color=(0,0,0))
+# mlab.outline()
+# mlab.show()
+#
+# fig = plt.figure(1)
+# plt.rc('mathtext', default='regular')
+# fig.subplots_adjust(hspace=0.4)
+# ax = plt.subplot(211)
+# plt.title("Relative residual")
+# plt.xlabel("Iteration")
+# plt.ylabel("Relative residual")
+# plt.plot(optimal_residuals[:plen])
+# plt.plot(constant_residuals[:plen])
+# plt.plot(newton_residuals[:plen])
+# plt.plot(nwst_residuals[:plen])
+# plt.legend([r'Optimal $\alpha$', r'$\alpha$ = %.2f' % alpha, r'Newtons method', r'Steepest first, then Newton'])
+# ax.set_yscale('log')
+#
+# ax = plt.subplot(212)
+# plt.title("Function value")
+# plt.xlabel("Iteration")
+# plt.ylabel("g(x)")
+# plt.plot(f_optimal[:plen])
+# plt.plot(f_constant[:plen])
+# plt.plot(f_newton[:plen])
+# plt.plot(f_nwst[:plen])
+#
+# plt.show()
+# fig.savefig("steepest.eps", format="eps")
if __name__ == "__main__":
main()
\r
\begin{document}\r
\r
-\title{Semester Project 1 TMA4215}\r
+\title{Semester Project 1 in TMA4215}\r
\author{\r
KANDIDATNUMMER! \footnote{Skriv noe her.} \\\r
Hva med noe her også?\r
\begin{figure}[htb]\r
\centering\r
\includegraphics[width=0.9\textwidth]{steepest}\r
- \caption{Plot of relative residual and function values for iterations of the steepest descent method with optimal $\alpha$}\r
- \label{fig:fn_resid}\r
+ \caption{Plot of relative residual and function values for iterations of the\r
+ steepest descent method with optimal $\alpha$} \label{fig:fn_resid}\r
\end{figure}\r
\r
\begin{figure}[htb]\r
\centering\r
\includegraphics[width=0.9\textwidth]{surface}\r
- \caption{Surface plot of function with points showing iteration of steepest descent method, both with optimal and constant $\alpha$}\r
- \label{fig:surface}\r
+ \caption{Surface plot of function with points showing iteration of steepest\r
+ descent method, both with optimal and constant $\alpha$} \label{fig:surface}\r
\end{figure}\r
\r
Consider the equation\r
\r
To find the optimal step length, $\alpha^*$, we solve the equation\r
\begin{eqnarray}\r
- \frac{\partial}{\partial \mathbf{\alpha}}g\left(\mathbf{x}+\alpha\mathbf{u}\right) &=& 0, \text{where } \mathbf{u} = -\nabla g(\mathbf{x} ) \nonumber \\\r
- &\Updownarrow& \nonumber\\\r
+ \frac{\partial}{\partial\r
+ \mathbf{\alpha}}g\left(\mathbf{x}+\alpha\mathbf{u}\right) &=& 0, \text{where }\r
+ \mathbf{u} = -\nabla g(\mathbf{x} ) \nonumber \\\r
+ &\Updownarrow& \nonumber \\\r
A\alpha^3 + B\alpha^2 + C\alpha + D &=& 0,\r
- \end{eqnarray}\r
+\end{eqnarray}\r
where $A$, $B$, $C$, and $D$ is given by\r
- \begin{eqnarray}\r
+\begin{eqnarray}\r
A &=& \frac{1}{3}\mathbf{u}^TC(\mathbf{u})\mathbf{u} \nonumber \\\r
B &=& \mathbf{x}^TC(\mathbf{u}) \mathbf{u} \nonumber \\\r
C &=& \mathbf{u}^TH\mathbf{u}+\mathbf{u}^TC(\mathbf{x})\mathbf{u} \nonumber \\\r