Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc
- clear all
- close all
- tic
- h = 0.000001;
- it = 0;
- w = 1;
- [x,y] = meshgrid([-5:0.1:5],[-5:0.1:5]); %wejście
- %[x,y] = meshgrid([-2:0.1:2],[-1:0.1:3]);
- %[x,y] = meshgrid([-20:0.1:20],[-20:0.1:20]);
- z = @(x,y) x.^2 + y.^2; %wejście
- %z = @(x,y) 100.*(y-x.^2).^2 + (1-x).^2;
- %z = @(x,y) -cos(x).*cos(y).*(exp(-((x-pi).^2+(y-pi).^2)));
- dok = 0.001; %wejście
- lambda = 1; %wejście
- xp = 5; %wejście
- yp = 0; %wejście
- dx(1) = xp;
- dy(1) = yp;
- while(w == 1)
- dfdx = (z(xp+h,yp) - z(xp-h,yp))./(2.*h);
- dfdy = (z(xp,yp+h) - z(xp,yp-h))./(2.*h);
- grad = [dfdx,dfdy];
- alfa = lambda./norm(grad);
- xp = xp - lambda.*dfdx;
- yp = yp - lambda.*dfdy;
- it = it + 1;
- if(norm(grad) < dok)
- break;
- end
- dx(it) = xp;
- dy(it) = yp;
- if(it > 2)
- if(dx(it) > dx(it-1))
- lambda = lambda /2;
- end
- end
- end
- it
- xp
- yp
- z(xp,yp)
- subplot(2,1,1)
- mesh(x,y,z(x,y)) %plot3,surf,surfl
- %colormap(hot)
- subplot(2,1,2)
- [c,h] = contour(x,y,z(x,y)); %rzutowanie kontur
- title('Metoda gradientu prostego')
- axis square
- clabel(c,h);
- hold on
- plot(dx(1),dy(1),'rx')
- text(dx(1),dy(1),'start') %pierwszy punkt wektora
- plot(dx(end),dy(end),'rx')
- text(dx(end),dy(end),'stop') %ostatni punkt wektora
- plot(dx(2:end-1),dy(2:end-1),'g.')
- plot(dx,dy) %łączenie punktów
- czas = toc %koniecz odliczania czasu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement