Demo entry 4722481

HTML

   

Submitted by anonymous on May 10, 2016 at 16:32
Language: Python. Code size: 727 Bytes.

import numpy as np
from numpy import linalg as LA
def steepest_descent(f, gradients, initialguess, approximationlevel, maxiterations):
    n=len(initialguess)
    xk=initialguess
    for iterations in range(maxiterations):
        Vf=np.zeros(n)
        for i in range(0,n):
            Vf[i]= gradients[i](xk)

        pk = -Vf
        print 'pk',pk

        a=1
        c1=0.0001
        ared=1
        pred=0
        while ared>pred:
            ared = f(xk+a*pk)
            pred = f(xk)-a*c1*LA.norm(Vf)**2
            print 'ared',ared
            print 'pred',pred
            print a
            a=a-0.005

        xk=xk+a*pk
        print initialguess,a,pk,xk,f(xk)
    print f(initialguess)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).