Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.62 KB | None | 0 0
  1. clear
  2. close all
  3. R = 0.125;
  4. epsilon = 0.001;
  5. x0=[1 4];
  6. [x,y]=meshgrid(-10:.2:10);
  7. z = (x + 2).^2 + (y + 2).^2 + ((y - x).^2)/R;
  8. hold on
  9. contour(x,y,z);
  10. [dx,dy]=gradient(z);
  11. quiver(x,y,dx,dy);
  12. xs=x0;
  13. itsk = 100;
  14. step=0.05;
  15.    for i=1:itsk
  16.         x0 = xs - step * gradient1(xs,R);
  17.         plot([xs(1),x0(1)],[xs(2),x0(2)],'r',[xs(1),x0(1)],[xs(2),x0(2)],'ro')
  18.         d = abs(xs - x0);
  19.         xs=x0;
  20.        % R = R/5;
  21.         if(d < epsilon)
  22.         break;
  23.         end
  24.    end
  25. disp(x0);
  26. function gr = gradient1(x,R)
  27. gr(1) = (2*(R*(x(1) + 2) + x(1) - x(2)))/R;
  28. gr(2) = (2*(R*(x(2) + 2) - x(1) + x(2)))/R;
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement