Advertisement
tojooko

Untitled

Jan 15th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.22 KB | None | 0 0
  1. syms x1 x2 x3 x4;
  2. func = 4673+x4^2+8*x2*x4+2*x1*x4+19*x3^2+80*x2*x3+14*x1*x3+218*x2^2+256*x1*x2+1020*x1^2+1504*x1+8*x4+8*x3+40*x2+8*x3*x4;
  3. func = 3688+4*x4^2+24*x2*x4+32*x1*x4+43*x3^2+114*x2*x3+222*x1*x3+103*x2^2+578*x1*x2+1322*x1^2+134*x1-16*x4-118*x3-162*x2+24*x3*x4;
  4. f = matlabFunction(func,'vars',{[x1 x2 x3 x4]});
  5. g = [diff(func,x1) diff(func,x2) diff(func,x3) diff(func,x4)];
  6. grad = matlabFunction(g,'vars',{[x1 x2 x3 x4]});
  7.  
  8. eps = 10^(-2);
  9. x0 = [0 0 0 0];
  10. i = 0;
  11. B = eye(4);
  12. d = grad(x0);
  13. opt = optimset('display','off','tolfun',0,'tolx',1e-28,'MaxFunEvals',inf);
  14. while 1    
  15.     gr = grad(x0);    %gradient w poprzednim kroku
  16.     if norm(gr) < eps
  17.         disp('PRZERWANO OBLICZENIA!!');
  18.         disp([int2str(i) ' ['  num2str(x0,8) ']  '  num2str(f(x0),8) '   ' num2str(norm(grad(x0)),8) ]);
  19.         disp(num2str(B,'%6.4f\t'));
  20.         break;
  21.     end
  22.    
  23.     i = i+1;
  24.     a = fminsearch(@(alf)(f(x0+d*alf)),0,opt);
  25.     f1 = f(x0);
  26.     xstare = x0;
  27.     x0 = x0 + a*d;
  28.  
  29.     if f(x0) >= f1 || mod(i,9) == 0
  30.         B = eye(4);
  31.     else
  32.         dx = (x0 - xstare)';
  33.         dg = grad(x0)' - gr';
  34.         B = B + (dx * dx')/(dx' * dg) - (B*dg* dg' * B) / (dg' * B * dg);
  35.     end
  36.     d = (B*(grad(x0)'))';
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement