[X,Y]= meshgrid(-4:.1:4);
Z=X.^2 + X.*Y + 0.5*Y.^2 - X - Y;
contour(X,Y,Z);
meshc(X,Y,Z);
zad18
tolfun=10^-5;
tolx=10^-3;
options=optimset('Tolx', tolx, 'TolFun',tolfun,'PlotFcns',@optimplotfval);
figure(1);
fun=@(x)sin(x(1)).*sin(x(2)).*exp(-(x(1).^2+x(2).^2));
x0=[-1,1];
%x0=[1,-1];
[x,fval,exitflag,output] = fminsearch(fun,x0,options)
figure(2);
[x,y]=meshgrid(-3:0.1:3, -3:0.1:3);
z=sin(x).*sin(y).*exp(-((x).^2+y.^2));
surf(x,y,z)
figure(3);
contourf(x,y,z);
zad1
tolx=10^-16;
tolfun=10^-8;
options=optimset('TolFun',tolfun,'PlotFcns',@optimplotfval);
fun = @(x) (x(1)-2)^2+(x(2)-2)^2;
x0=[5,3];
[x,fval,exitflag,output]=fminsearch(fun,x0,options)
zad3
clc;
clear;
[X,Y]= meshgrid(-4:.1:4);
Z=X.^2 + X.*Y + 0.5*Y.^2 - X - Y;
contour(X,Y,Z);
%meshc(X,Y,Z);
tolx=10^-6;
tolfun=10^-2;
options=optimset('TolFun',tolfun,'PlotFcns',@optimplotfval);
%fun = @(x) (x(1)-2)^2+(x(2)-2)^2;
fun = @(x) 4*x(1)^2-2.1*x(1)^4+(1/3)*x(1)^6+x(1)*x(2)-4*x(2)^2+4*x(2)^4
%fun = @(x) x(1)^2 + x(1)*x(2) + 0.5*x(2)^2 - x(1) - x(2);
x0=[0,0];
[x,fval,exitflag,output]=fminsearch(fun,x0,options)
zad 3
clc;
clear;
[X,Y] = meshgrid(-2:0.1:2, -1.2:0.1:1.2);
%Z=X.^2 + X.*Y + 0.5*Y.^2 - X - Y;
Z= 4*X.^2-2.1*X.^4+(1/3)*X.^6+X.*Y-4*Y.^2+4*Y.^4;
figure(1)
V = -3:0.1:2.5;
%V = -100:0.05:100;
[C,h] = contour(X,Y,Z,V);
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)
%meshc(X,Y,Z);
%surf(X,Y,Z)
tolx=10^-8;
tolfun=10^-2;
options=optimset('TolX',tolx,'TolFun',tolfun,'PlotFcns',@optimplotfval);
fun = @(x) 4*x(1)^2-2.1*x(1)^4+(1/3)*x(1)^6+x(1)*x(2)-4*x(2)^2+4*x(2)^4;
%x0=[-1.5,-0.5];
%x0=[1.5,0.5];
x0=[0,1];
%x0=[0,-1];
%x0=[-1.5,0.9];
%x0=[1.5,-0.9];
%[x,fval,exitflag,output]=fminsearch(fun,x0,options)
[x,fval,exitflag,output, grad, hessian]=fminunc(fun,x0,options)
zad4
3. ogr liniowe
[x y]=meshgrid(-4:.1:4);
z=x.^2+x.*y+0.5.*y.^2-x-y;
con =-x+y+1;
con1 = x+y-4;
contour(x,y,z)
hold on
contour(x,y,con,[1e-8])
hold on
contour(x,y,con1,[1e-8])
tolfun=10^-1;
tolcon=10^-1;
options=optimset('TolFun', tolfun, 'TolCon',tolcon,'PlotFcns','optimplotfval', 'display' , 'iter');
fun = @(x)x(1).^2+x(1).*x(2)+0.5.*x(2).^2-x(1)-x(2);
x0=[2 -2];
A=[-1 1; 1 1];
b=[-1 ;4];
[x,fval,exitflag,output]=fmincon(fun,x0,A,b,[],[],[],[],[],options)
3.ogr nieliniowe
[x y]=meshgrid(-4:.1:4);
z=x.^2+x.*y+0.5.*y.^2-x-y;
con =-x+y+1;
con1 = x+y-4;
con2=y-x.^2;
contour(x,y,z)
hold on
contour(x,y,con2,[1e-8])
tolcon=10^-1;
options=optimset( 'TolCon',tolcon,'PlotFcns',@optimplotconstrviolation, 'display' , 'iter');
fun = @(x)x(1).^2+x(1).*x(2)+0.5.*x(2).^2-x(1)-x(2);
x0=[0.5 0.2];
[x,fval,exitlog,out] = fmincon(fun,x0,[],[],[],[],[],[],@(x) mycon(x),options)
Funkcja mycon.m
function [c,ceq] = mycon(x)
c=x(2)-x(1)^2;
ceq=[];
17.ogrniczenie liniowe
[x y]=meshgrid(0:.1:6,0:.1:6);
z=4*x.^2-2.1*x.^4+1/3*x.^6+x.*y-4*y.^2+4*y.^4;
con=-x-y+6
figure(2)
contour(x,y,z)
hold on
contour(x,y,con,[1e-8])
x0=[3.2 3.2];
x0=[3.2 3.2];
tolcon=10^-1;
options=optimset('TolCon',tolcon,'PlotFcns',@optimplotfval, 'display' , 'iter');
fun = @(x)4.*x(1).^2-2.1.*x(1).^4+(1/3).*x(1).^6+x(1).*x(2)-4.*x(2).^2+4.*x(2).^4;
[x,fval,exitlog,out] = fmincon(fun,x0,[],[],[],[],[],[],@(x) mycon1(x),options)
Funkcja mycon1.m
function [c,ceq] = mycon1(x)
c=-x(1)-x(2)+6;
ceq=[];
17organiczenie nieliniowe
[x y]=meshgrid(4:.1:6,4:.1:6);
z=4*x.^2-2.1*x.^4+1/3*x.^6+x.*y-4*y.^2+4*y.^4;
con=(x-5).^2+(y-5).^2-1;
figure(2)
contour(x,y,z)
hold on
contour(x,y,con,[1e-8])
x0=[5 5];
tolcon=10^-1;
options=optimset('TolCon',tolcon,'PlotFcns',@optimplotconstrviolation, 'display' , 'iter');
fun = @(x)4.*x(1).^2-2.1.*x(1).^4+(1/3).*x(1).^6+x(1).*x(2)-4.*x(2).^2+4.*x(2).^4;
[x,fval,exitlog,out] = fmincon(fun,x0,[],[],[],[],[],[],@(x) mycon2(x),options)
Funkcja mycon2.m
function [c,ceq] = mycon2(x)
c=(x(1)-5)^2+(x(2)-5)^2-1;
ceq=[];
jeśli chcesz wartości w iteracjach to zamieniasz @optimplotconstrviolation na @optimplotfval