daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clc
  2. clear all
  3. close all
  4.  
  5. tic
  6.  
  7. h = 0.000001;
  8. it = 0;
  9. w = 1;
  10.  
  11. [x,y] = meshgrid([-5:0.1:5],[-5:0.1:5]); %wejście
  12. %[x,y] = meshgrid([-2:0.1:2],[-1:0.1:3]);
  13. %[x,y] = meshgrid([-20:0.1:20],[-20:0.1:20]);
  14.  
  15. z = @(x,y) x.^2 + y.^2; %wejście
  16. %z = @(x,y) 100.*(y-x.^2).^2 + (1-x).^2;
  17. %z = @(x,y) -cos(x).*cos(y).*(exp(-((x-pi).^2+(y-pi).^2)));
  18.  
  19. dok = 0.001; %wejście
  20. lambda = 1; %wejście
  21. xp = 5; %wejście
  22. yp = 0; %wejście
  23. dx(1) = xp;
  24. dy(1) = yp;
  25.  
  26. while(w == 1)
  27.     dfdx = (z(xp+h,yp) - z(xp-h,yp))./(2.*h);
  28.     dfdy = (z(xp,yp+h) - z(xp,yp-h))./(2.*h);
  29.     grad = [dfdx,dfdy];
  30.    
  31.     alfa = lambda./norm(grad);
  32.    
  33.     xp = xp - lambda.*dfdx;
  34.     yp = yp - lambda.*dfdy;
  35.     it = it + 1;
  36.    
  37.     if(norm(grad) < dok)
  38.         break;
  39.     end
  40.    
  41.     dx(it) = xp;
  42.     dy(it) = yp;
  43.    
  44.     if(it > 2)
  45.         if(dx(it) > dx(it-1))
  46.             lambda = lambda /2;
  47.         end
  48.     end
  49. end
  50.  
  51.    
  52. it
  53. xp
  54. yp
  55.  
  56. z(xp,yp)
  57.  
  58. subplot(2,1,1)
  59. mesh(x,y,z(x,y)) %plot3,surf,surfl
  60. %colormap(hot)
  61.  
  62. subplot(2,1,2)
  63. [c,h] = contour(x,y,z(x,y)); %rzutowanie kontur
  64. title('Metoda gradientu prostego')
  65. axis square
  66. clabel(c,h);
  67.  
  68. hold on
  69.  
  70. plot(dx(1),dy(1),'rx')
  71. text(dx(1),dy(1),'start') %pierwszy punkt wektora
  72. plot(dx(end),dy(end),'rx')
  73. text(dx(end),dy(end),'stop') %ostatni punkt wektora
  74.  
  75. plot(dx(2:end-1),dy(2:end-1),'g.')
  76. plot(dx,dy) %łączenie punktów
  77.  
  78. czas = toc %koniecz odliczania czasu
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top