非线性无约束优化函数

时间:2014-04-22 20:34:34

标签: matlab optimization mathematical-optimization nonlinear-optimization

我有这个非线性无约束优化问题: F = 2 * PI *(X ^ 2)+ 2 * PI X ý

我必须使用Newton的Method和Gradient Method两种方法在MatLab中解决它。我编写了代码,但是我得到了一些我无法解决的错误,所以我真的需要一些帮助,因为我必须用这些方法的结果制作一个图表,然后比较它们。

Gradient Method的代码:

这是问题给出的功能:

function f=functie()
  syms x y;
  f=(2*pi*(x^2))+(2*pi*x*y);
end

这是对给定点中函数的评估:

function y = feval_obj(x)
 y=(2*pi*(x(1)^2))+(2*pi*x(1)*x(2));
end

这是给定点的函数渐变:

function y = gradient_obj(val)
 gradient_f = gradient(functie);
 syms x y;
 y = subs(gradient_f, [x, y], val);
end

我必须检查每次迭代问题的理想步骤,所以我需要另一个函数来返回函数f的值(x + alpha * d)

function f = phi_obj(alpha, x, d)
 f = feval_obj(x + alpha * d);
end

渐变方法的代码为:

function xmin=gradient_method(x0,eps)
 %Initializing of the vectors/matrix that we need
 puncte_gradient=[]; %gradient points 
 puncte_iteratie=[]; %iteration points
 valori_functie=[]; %function values 
 norme_gradienti=[]; %gradients norm
 %I will use a vector g to keep the current gradient
 x=x0; 
 g=gradient_obj(x); 
 while(norm(g)>eps)
  g=gradient_obj(x); 
  puncte_gradient=[puncte_gradient g];
  puncte_iteratie=[puncte_iteratie x];
  valori_functie=[valori_functie; feval_obj(x)];
  norme_gradienti=[norme_gradienti; norm(g)];
  alpha=fminsearch(@(alpha) phi_obj(alpha,x,-g), 1);
  x=x-alpha*g
 end
 xmin=x;

 %This is the chart display of the data I get 
 t=1:length(valori_functie);
 figure(1)
 hold on
 plot(t,norme_gradienti(t),'k','LineWidth',2);
 hold off
 figure(2)
 hold on
 plot(t,valori_functie(t),'k','LineWidth',2);
 hold off

 % For drawing the contour lines and the gradient method_s evolution we have: 
 [x1,x2]=meshgrid([1.2:0.01:2.8],[0.4:0.01:1.6]);
 z=(2*pi*(x1^2))+(2*pi*x1*x2);
 figure(3)
 hold on
 contour(x1,x2,z,valori_functie);
 plot3(puncte_iteratie(1,:),puncte_iteratie(2,:),valori_functie,'r');
 scatter3(puncte_iteratie(1,:),puncte_iteratie(2,:),valori_functie,'filled');
 hold off
end

我会在设法解决此问题后立即使用牛顿法更新帖子。那么有没有人有任何想法我为什么会收到错误?为了找到理想的步骤, x0 应为初始值, eps 应为容差。 (如果梯度的范数大于算法应该停止的容差)。

0 个答案:

没有答案