Creating function for implementing steepest descent algorithm

时间:2017-03-22 18:50:08

标签: algorithm matlab mathematical-optimization

I am trying to implement steepest descent algorithm for minimization of 2D function. Let me explain with example.

I have function f1(x1,x2) = 2*x1^2 + x2^2 - 5*x1*x2 and starting with initial guess point p0 = [1,0].

Step 1: Initial guess point p0 = [1,0], convergence parameter e=0.1

Step 2: calculate gradient c1 of f1 at p0. c1=[4,-5] I am using central difference method for that.

Step 3: If norm of c1>e, go to step 4, otherwise stop.

Step 4: Our direction of search is d1 = - c1. So, d1 = [-4,5].

Step 5: Find step size a to minimize f1(a) = f1(p0 + a*d1) = f1(1-4a,5a)

Step 6: Update p0 to p1 as p1 = p0 + a * d1 and to to step 2.

I am trying to implement this example in matlab and do not know how to implement step 5. I know that ant 1D search algorithm, such as bisection method can work. But, problem is to 'converting' f1(1-4a,5a) to a function, that is, substituting (1-4a,5a) into f1. I encounter symbolic constant a here, which I do not know how to deal with. If I write a minimization function, I can pass values to it, but not sure about symbolic variable a. I do not want to use special features of matlab, such as symbolics and trying to keep code at general level, so I can convert it into other programming languages without any problems. Your suggestions are welcome.

1 个答案:

答案 0 :(得分:0)

您可能希望使用fminbnd在行搜索上放置一些边界(下面的-1到1),因为搜索整行是一个定义不明确的任务。不需要符号变量。步长是通过最小化以下匿名函数来确定的:

a = fminbnd(@(a) f1(p(1) + a*d(1), p(2) + a*d(2)), -1, 1);

这里p是当前点,d是搜索方向。 (我不想在你的描述中将它们称为p1和d1,就像它们仅用于第一步一样。)

该示例允许否定a,我觉得这在实践中很有用;有时最好从梯度表示的方向向相反方向前进。要禁止此操作,请将边界更改为0,1(或0,以便在向前方向上进行更积极的搜索)。

相关问题