Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear all
- close all
- [X,Y] = meshgrid(-5:0.1:5);
- F = (X + 2).^2 + (Y + 2).^2;
- figure(1)
- contour(X,Y,F,100,'k')
- axis equal
- Z = zeros(size(F));
- G = (Y - X);
- P = max(G,Z).^2;
- miu = 10;
- T = F + miu*P;
- figure(2)
- contour(X,Y, F, 50, 'k')
- hold on
- x = -5:0.01:5;
- y = x+2;
- plot(x,y,'r')
- axis([-5 5 -5 5])
- axis equal
- zlim([-10 10])
- R = 0.25;
- epsilon = 0.001;
- x0=[1 4];
- xs=x0;
- itsk = 200;
- step=0.1;
- for i=1:itsk
- x0 = xs - step * gradient1(xs,R);
- plot([xs(1),x0(1)],[xs(2),x0(2)],'r',[xs(1),x0(1)],[xs(2),x0(2)],'ro')
- d = abs(xs - x0);
- xs=x0;
- %R = R/5;
- if(d < epsilon)
- break;
- end
- end
- function gr = gradient1(x,R)
- gr(1) = (2*(R*(x(1) + 2) + x(1) - x(2)))/R;
- gr(2) = (2*(R*(x(2) + 2) - x(1) + x(2)))/R;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement