Optimering

Exempel från föreläsningen

Exempel 1

Remove["Global`*"]
f[x_,y_]=x*y^2;
Maximize[{f[x,y],x^2+y^2<=1,x>=0,y>=0},{x,y} ]

Exempel 2

Remove["Global`*"]
f[x_,y_]=x^2+2x*y-3y^2;
v={1,-1}/Norm[{1,-1}];
G=Grad[f[x,y], {x,y}]
G.v/.{x->1,y->2}

Polynomapproximation

Remove["Global`*"]
xv={x1,x2,x3};
xv2=xv^2;
fv={f1,f2,f3};
const=Table[c[i],{i,0,2}];
p={1,x,x^2}.const
a=Transpose[ {{1,1, 1},xv,xv^2}];
sol=Solve[a.const==fv,const];
x4=Collect[FullSimplify[-c[1]/(2c[2])/.sol[[1]]],{f1,f2,f3}]

Exempel 3

Remove["Global`*"]
xv={0.,1,2};
fv={3,2,6};
xv2=xv^2;
data=Transpose[{xv,fv}];
lp=ListPlot[data,PlotStyle->{Red,PointSize[Large]}];
const=Table[c[i],{i,0,2}];
a=Transpose[ {{1,1, 1},xv,xv^2}];
sol=Solve[a.const==fv,const][[1]];
p={1,x,x^2}.const/.sol;
x4=-c[1]/(2c[2])/.sol;
Print["p2(x) = ",p,", x4 = ",x4,", p2_min = ",p/.x->x4];
pl=Plot[p,{x,xv[[1]],xv[[3]]}, PlotRange->{0,6.3}];
Show[pl,lp]

Exempel 4

Remove["Global`*"]
f[x_]=x^2-4x/3+1;
NSolve[f'[x]==0,x]
eps=0.001;
{a,b}={0.,1};
Plot[f[x],{x,a,b}]
eps=0.0001;
c=(a+b)/2;
While[Abs[f'[c]]>eps,
c=(b+a)/2;
Print[{a,c,b}];
If [f'[c]>eps,b=c,a=c]
]
N[Minimize[f[x],x]]

Exempel 5

Remove["Global`*"]
f[x_]=x^2-4x/3+1;
epsilon=0.000001;
diff=1.;
xold=1.;
While[diff>epsilon,
xnew=xold-f'[xold]/f''[xold];
diff=Abs[xnew-xold];
Print[{xnew,diff}];
xold=xnew;
]
f[xnew]
NMinimize[f[x],x]

Exempel 6

Remove["Global`*"]
f[x_,y_]:=(x+0.3y)^2+(2(x^2+y^2-1)-1)^2;
Plot3D[f[x,y],{x,-1.3,1.8},{y,0.5,1.7},ColorFunction->Hue]
FindMinimum[f[x,y],{{x,-1.1},{y,0.8}}]

Newton

Remove["Global`*"]
f=(x+0.3y)^2+(2(x^2+y^2-1)-1)^2;
alpha=0.8;
Print["∇f = ",G=FullSimplify[D[f,{{x,y}}]]];
Print["H = ",H=FullSimplify[D[f,{{x,y},2}]]];
{x,y}={-1.1,0.8};
route={{x,y}};
Do[{x,y}={x,y}-alpha*Inverse[H].G;AppendTo[route,{x,y}],{10}];
Print["Route = ",Transpose[route]];
ContourPlot[f,{x,-1.3,-0.2},{y,0.75,1.25},
Contours-> 6(Range[0,6,0.25]/6)^4,
PlotPoints->50, 
ColorFunction->Hue,
Epilog->{Blue,Line[route],PointSize[0.02],Point/@ route}
]
Print["fmin = ",f]

Steepest descend (känsligare för val av (x^((0))) och α än Newton!)

Remove["Global`*"]
f=(x+0.3y)^2+(2(x^2+y^2-1)-1)^2;
alpha=0.02;
Print["∇f = ",G=FullSimplify[D[f,{{x,y}}]]];
{x,y}={-0.5,1.};
route={{x,y}};
Do[{x,y}={x,y}-alpha*G;AppendTo[route,{x,y}],{80}];
Print["Route = ",Transpose[route]];
ContourPlot[f,{x,-0.6,-0.3},{y,0.95,1.2},
Contours-> 6(Range[0,6,0.25]/6)^4,
PlotPoints->50, 
ColorFunction->Hue,
Epilog->{Blue,Line[route],PointSize[0.02],Point/@ route}
]
Print["fmin = ",f]

Exempel 7

Remove["Global`*"]
f=x^2+y^2+z^2;
g=x-y+2z-6;
L=f+λ*g
G=D[L,{{x,y,z,λ}}]
sol=Solve[G==0,{x,y,z,λ}]
f/.sol[[1]]
Minimize[{f,g==0},{x,y,z}]

Exempel 8

Remove["Global`*"]
f=x^2+y^2+z^2;
g1=x+y+z+3;
g2=x+2y+3z-6;
vars={x,y,z,λ1,λ2};
L=f+λ1*g1+λ2*g2;
G=D[L,{vars}];
H=D[L,{vars,2}];
invH=Inverse[H] ;(*H konstant, invH behöver bara beräknas 1 gång!*)
epsilon=0.000001;
diff=1;
xold={1,1,1,1,1};
While[diff>epsilon,
xnew=xold-invH.G/.Thread[vars->xold];
diff=Norm[xnew-xold];
Print[{xnew,diff}];
xold=xnew;
]
Print["(x,y,z) = ",xnew,", f_min = ",f/.Thread[vars->xold]]
Minimize[{f,g1==0,g2==0},{x,y,z}]

Exempel 10

Remove["Global`*"]
f=x^2+y^2+z^2;
g1=x+y+z+3;
g2=x+2y+3z-6.;
vars={x,y,z};
P=f+1/r(g1^2+g2^2)/.r->10^(-5)
G=D[P,{vars}];
H=D[P,{vars,2}];
sol=Solve[G==0,vars]
f/.sol[[1]]
Minimize[{f,g1==0,g2==0},{x,y,z}]