]> git.rustad.me Git - nummat1/commitdiff
More small fixes
authorBjørn Rustad <bjornrus@samfundet.no>
Fri, 30 Sep 2011 19:45:15 +0000 (21:45 +0200)
committerBjørn Rustad <bjornrus@samfundet.no>
Fri, 30 Sep 2011 19:45:15 +0000 (21:45 +0200)
function.py
main.py
newton.py
rapport.tex
steepest.py

index a7a26a307cec83661e877477b27f4421cb2d6fc7..92cf71407e1a68d9e65086ee9fe2933bd9953350 100644 (file)
@@ -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 8d2dd7a47f959c4bdaf2e1a94dcdd2179ee94a6f..bafd50452672a2082d8fc453d270eea8c1bb39af 100644 (file)
--- 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()
index c63922946ae76ef6cf461d0c2e7052a6cb4b2775..9eb559ad672cb82ec4ab016b2aa3aea71e2b246e 100644 (file)
--- 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
index f7816dd5ad43c1c81530675c73952d7324f4a2f7..015b48108909e9ae44487db25800c6f33971a5e1 100644 (file)
@@ -34,7 +34,7 @@
 \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
@@ -123,15 +123,15 @@ that the Hessian, $\nabla^{2}g(\mathbf{x})$, is positive definite.
 \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
@@ -151,12 +151,14 @@ where $t_{n+1} = t_n + \alpha$. Looking at this result, we see that it is equiva
 \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
index 976388c886c5db64e78626b3b5d65832743af259..2d5d401f3564a937b7040441201a4c1f2619665b 100644 (file)
@@ -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