Skip to content

Instantly share code, notes, and snippets.

@Idorobots
Created March 16, 2013 20:12
Show Gist options
  • Save Idorobots/5178102 to your computer and use it in GitHub Desktop.
Save Idorobots/5178102 to your computer and use it in GitHub Desktop.
fmincon example
function [c, ceq] = h(x)
ceq = [];
c = (x(1) - 1)^2 + (x(2) + 1)^2 - 1;
end
function [xp, yp] = circle(x,y,r)
ang=0:0.01:2*pi;
xp= x + r * cos(ang);
yp= y + r * sin(ang);
end
function zad4()
fun = @(x) x(1)^3 + x(1)^2 + x(1)*x(2) + x(2)^2;
x0 = [0, 0];
[xopt, min] = fmincon(fun, x0, [], [], [], [], [], [], @h);
xopt
min
N = 75;
elems = 100;
[x1, x2] = meshgrid(linspace(-0.7, 0.7, elems), linspace(-0.7, 0.7, elems));
y = x1.^3 + x1.^2 + x1*x2 + x2.^2;
figure();
hold on;
xlabel('x1');
ylabel('x2');
title('Wykres poziomnicowy funkcjonału jakości.');
grid on;
contour(x1, x2, y, N);
plot(xopt(1), xopt(2), '*');
[cx, cy] = circle(1, -1, 1);
plot(cx, cy, '--k', 'LineWidth', 2);
hold off;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment