Skip to content

Instantly share code, notes, and snippets.

@MichaelStett
Created January 20, 2020 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelStett/fdf385294fb92978b87c424caecaa181 to your computer and use it in GitHub Desktop.
Save MichaelStett/fdf385294fb92978b87c424caecaa181 to your computer and use it in GitHub Desktop.
Find min in Gradient Matlab
close all; clear all; clc;
eps = 0.01;
x0 = -4;
y0 = -4;
F = @(x,y)(x.^2).*2 + y.^2 + x.*y - x.*6 - y.*5 + 8;
x = linspace(-5, 5);
y = linspace(-5, 5);
[X, Y] = meshgrid(x, y);
Z = F(X, Y);
contour(X, Y, Z, 50);
hold on;
grid on;
v0 = [x0, y0];
v1 = [x0 - 1, y0 - 1];
counter = 0;
while true
prevX = v0(1);
prevY = v0(2);
counter = counter + 1;
% Y's
F=@(y)2*v0(1).^2 + y.^2 + v0(1).*y - 6*v0(1) - 5*y + 8;
result = calc(F, -5, 5, v0(2), v1(2));
v0(2) = result(1);
v1(2) = result(2);
plotPoint(v1(1), v1(2));
plotLine([v0(1),v0(1)],[prevY,v0(2)]);
% X's
F=@(x)2*x.^2 + v0(2).^2 + x*v0(2) - 6*x - 5*v0(2) + 8;
result = calc(F, -5, 5, v0(1), v1(1));
v0(1) = result(1);
v1(1) = result(2);
plotPoint(v0(1), v0(2));
plotLine([prevX, v0(1)],[v0(2),v0(2)]);
fprintf("Iter: %i \n", counter);
printPoint(v0(1), v0(2));
printPoint(v1(1), v1(2));
fprintf("\n");
if diff(v0, v1) < eps
% fprintf("break \n");
break;
end
end
function printPoint(x, y)
fprintf("x: %0.2f, y: %0.2f \n", x, y);
end
function plotPoint(x, y)
plot(x,y,'r.');
end
function plotLine(from, to)
line(from, to, 'Color','black');
end
function result = calc(F, a, b, n0, n1)
eps = 0.01;
a1=(((a+b)/2) + a) / 2;
a2=(((a+b)/2) + b) / 2;
while b - a > eps
if F(a1)<F(a2)
b = a2;
else
a = a1;
end
a1=(((a+b)/2)+a)/2;
a2=(((a+b)/2)+b)/2;
end
n1=n0;
n0=(a1+a2) / 2;
result = [n0, n1];
end
function result = diff(v0, v1)
result = sqrt((v1(1) - v0(1)).^2 + ((v1(2) - v0(2)).^2));
end
close all; clear all; clc;
% const
alpha = 0.2;
x0 = -4;
y0 = -4;
F = @(x,y) (x.^2).*2 + y.^2 + x.*y - x.*6 - y.*5 + 8;
x = linspace(-5, 5);
y = linspace(-5, 5);
[X, Y] = meshgrid(x, y);
Z = F(X, Y);
contour(X, Y, Z, 50);
hold on;
flag = 1;
while flag == 1
flag = 0;
vecA = [
0
y0 + alpha
x0 + alpha
y0 - alpha
x0 - alpha
];
vecF = [
F(x0, y0)
F(x0, vecA(2))
F(vecA(3), y0)
F(x0, vecA(4))
F(vecA(5), y0)
];
[ymin, i] = min(vecF);
if i > 1
if i == 2 || i == 4
y0 = vecA(i);
end
if i == 3 || i == 5
x0 = vecA(i);
end
flag = 1;
end
plot(x0, y0, 'r.');
end
fprintf("x: %0.2f, y: %0.2f \n", x0, y0);
eps = 0.1;
lambda = 0.1;
F = @(x,y)(x.^2).*2 + y.^2 + x.*y - x.*6 - y.*5 + 8;
x = linspace(-5, 5);
y = linspace(-5, 5);
x0 = 5;
y0 = 5;
[X, Y] = meshgrid(x, y);
Z = F(X, Y);
contour(X, Y, Z, 50);
hold on;
grid on;
vn = [x0, y0];
for n=1:100
grad = GradF(vn(1), vn(2));
gradnorm = norm(grad);
n_grad = grad / gradnorm;
vn1 = vn - n_grad.*lambda;
fprintf("%d: [ %.2f, %.2f ] \n", n, vn1(1), vn1(2));
plotPoint(vn1(1), vn1(2));
% fprintf("diff: %.5f \n", gradnorm - eps)
if gradnorm - eps < 0
break;
end
vn = vn1;
end
function plotPoint(x, y)
plot(x,y,'r.');
end
function result = GradF(x, y)
dfx = x.*4 + y - 6;
dfy = y.*2 + x - 5;
result = [dfx, dfy];
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment