From a773eae7d55ae96e15a3107c854c66a6477891d7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B8rn=20Rustad?= Date: Fri, 30 Sep 2011 21:45:15 +0200 Subject: [PATCH] More small fixes --- function.py | 1 + main.py | 112 ++++++++++++++++++++++++++-------------------------- newton.py | 4 +- rapport.tex | 20 +++++----- steepest.py | 4 +- 5 files changed, 72 insertions(+), 69 deletions(-) diff --git a/function.py b/function.py index a7a26a3..92cf714 100644 --- a/function.py +++ b/function.py @@ -13,6 +13,7 @@ def _C(x, c): # Calculate gradient of g in point x def gradient_g(x, H, b, c): + print _C(x,c) return -b + H.dot(x) + (1.0/3.0) * _C(x,c).dot(x) # Calculate hessian of g in point x diff --git a/main.py b/main.py index 8d2dd7a..bafd504 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ -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 @@ -44,60 +44,60 @@ def main(): 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() diff --git a/newton.py b/newton.py index c639229..9eb559a 100644 --- a/newton.py +++ b/newton.py @@ -1,9 +1,9 @@ -from enthought.mayavi import mlab +#from enthought.mayavi import mlab from function import function_g, gradient_g, hessian_g import generate import surf import numpy as np -import matplotlib.pyplot as plt +#import matplotlib.pyplot as plt from numpy.linalg import norm, inv # Calculate the next iteration of the Newton method diff --git a/rapport.tex b/rapport.tex index f7816dd..015b481 100644 --- a/rapport.tex +++ b/rapport.tex @@ -34,7 +34,7 @@ \begin{document} -\title{Semester Project 1 TMA4215} +\title{Semester Project 1 in TMA4215} \author{ KANDIDATNUMMER! \footnote{Skriv noe her.} \\ Hva med noe her også? @@ -123,15 +123,15 @@ that the Hessian, $\nabla^{2}g(\mathbf{x})$, is positive definite. \begin{figure}[htb] \centering \includegraphics[width=0.9\textwidth]{steepest} - \caption{Plot of relative residual and function values for iterations of the steepest descent method with optimal $\alpha$} - \label{fig:fn_resid} + \caption{Plot of relative residual and function values for iterations of the + steepest descent method with optimal $\alpha$} \label{fig:fn_resid} \end{figure} \begin{figure}[htb] \centering \includegraphics[width=0.9\textwidth]{surface} - \caption{Surface plot of function with points showing iteration of steepest descent method, both with optimal and constant $\alpha$} - \label{fig:surface} + \caption{Surface plot of function with points showing iteration of steepest + descent method, both with optimal and constant $\alpha$} \label{fig:surface} \end{figure} Consider the equation @@ -151,12 +151,14 @@ where $t_{n+1} = t_n + \alpha$. Looking at this result, we see that it is equiva To find the optimal step length, $\alpha^*$, we solve the equation \begin{eqnarray} - \frac{\partial}{\partial \mathbf{\alpha}}g\left(\mathbf{x}+\alpha\mathbf{u}\right) &=& 0, \text{where } \mathbf{u} = -\nabla g(\mathbf{x} ) \nonumber \\ - &\Updownarrow& \nonumber\\ + \frac{\partial}{\partial + \mathbf{\alpha}}g\left(\mathbf{x}+\alpha\mathbf{u}\right) &=& 0, \text{where } + \mathbf{u} = -\nabla g(\mathbf{x} ) \nonumber \\ + &\Updownarrow& \nonumber \\ A\alpha^3 + B\alpha^2 + C\alpha + D &=& 0, - \end{eqnarray} +\end{eqnarray} where $A$, $B$, $C$, and $D$ is given by - \begin{eqnarray} +\begin{eqnarray} A &=& \frac{1}{3}\mathbf{u}^TC(\mathbf{u})\mathbf{u} \nonumber \\ B &=& \mathbf{x}^TC(\mathbf{u}) \mathbf{u} \nonumber \\ C &=& \mathbf{u}^TH\mathbf{u}+\mathbf{u}^TC(\mathbf{x})\mathbf{u} \nonumber \\ diff --git a/steepest.py b/steepest.py index 976388c..2d5d401 100644 --- a/steepest.py +++ b/steepest.py @@ -1,9 +1,9 @@ -from enthought.mayavi import mlab +#from enthought.mayavi import mlab from function import function_g, gradient_g, _C import generate import surf import numpy as np -import matplotlib.pyplot as plt +#import matplotlib.pyplot as plt import time from numpy.linalg import norm -- 2.47.3