Primjeri jednodimenzionalne i visedimenzionalne minimizacije

Konveksna kombinacija

n = 3; 
    x = Table[{Random[], Random[]}, {i, n}];
x = {{0.6, 0.05}, {0.1, 0.6}, {0.5, 
            0.7}};
slx = 
        ListPlot[x, Axes -> None, PlotStyle -> AbsolutePointSize[3], 
          DisplayFunction -> Identity];
lam = Table[Random[], {i, n}]; 
    s = Apply[Plus, lam];
lam = lam/s;
point = 
        Sum[lam[[i]] x[[i]], {i, n}];
slpoint = 
        ListPlot[{point}, PlotStyle -> AbsolutePointSize[5], 
          DisplayFunction -> Identity]; 
Show[slx, slpoint, 
        DisplayFunction -> $DisplayFunction];

n = 3;

      
slx = 
        ListPlot[x, Axes -> None, PlotStyle -> AbsolutePointSize[5], 
          DisplayFunction -> Identity];
m = 500; points = Table[{0, 0}, {i, m}];
Do[
lam = Table[Random[], {i, n}]; 
        s = Apply[Plus, lam]; 
lam = 
          lam/s; 
points[[j]] = 
          Sum[lam[[i]] x[[i]], {i, 
              n}], 
    {j, 
          m}];
slpoints = 
        ListPlot[points, PlotStyle -> AbsolutePointSize[2], 
          DisplayFunction -> Identity]; 
Show[slx, slpoints, 
        DisplayFunction -> $DisplayFunction];

Udaljenost tocke do krivulje

q[x_]:=.5 x^3-6 x+2;
x0=3 Random[];y0=4 Random[];a=-4;b=5;
f[x_]:=(x-x0)^2+ (q[x] - y0)^2
slq=Plot[q[x],{x,a,b},DisplayFunction->Identity];
slp=ListPlot[{{x0,y0}},PlotStyle->AbsolutePointSize[4],DisplayFunction->Identity];
sl1=Show[slp,slq,Axes->True,DisplayFunction->Identity];
sld2=Plot[Sqrt[f[x]],{x,a,b},PlotLabel->"d(q,T)",DisplayFunction->Identity];
slf=Plot[f[x],{x,a,b},PlotLabel->"d^2(q,T)", DisplayFunction->Identity];
sld1=Plot[Abs[x-x0]+Abs[q[x]-y0],{x,a,b},PlotLabel->"d_1(q,T)",DisplayFunction->Identity];
Show[GraphicsArray[{{sl1,sld2},{slf,sld1}}],DisplayFunction->$DisplayFunction];

Primjeri funkcija za minimizaciju

f[x_, 
        y_] := -x^2 
          y +  .25 (2  x^2 - y^2) -  .5 (2 - x^2 - y^2)^2;  
    a = -2; b = 2; c = -2; 
    d = 2;
  
sl1 = 
        Plot3D[f[x, y], {x, a, b}, {y, c, d}, Shading -> False, 
          ViewPoint -> {1. , -2.9, 1.5}, 
          BoxRatios -> {1, 1, 0.7}, PlotPoints -> 15, 
          DisplayFunction -> Identity];
sl2 = 
        ContourPlot[f[x, y], {x, a, b}, {y, c, d}, 
          ContourShading -> False, Contours -> 20, 
          DisplayFunction -> Identity];
Show[GraphicsArray[{sl1, sl2}]];

Eksponencijalna regresija

<< Statistics`Master`

m = 25; phi[x_] :=  .1  Exp[ .25 x];
SeedRandom[8];
x = Table[ .6  i, {i, m}];
y = 
        phi[x] + 
          Table[Random[NormalDistribution[0,  .3]], {i, m}];
pod = Table[{x[[i]], y[[i]]}, {i, m}];
slphi = 
        Plot[phi[t], {t, 0, Max[x] + 1}, 
          DisplayFunction -> Identity];
slpod = 
        ListPlot[pod, PlotStyle -> AbsolutePointSize[3], 
          DisplayFunction -> Identity];
sl1 = 
        Show[slphi, slpod, 
          DisplayFunction -> $DisplayFunction];

Newtonova metoda

NewtonMin1[f_,x0_,eps_,it_]:=Module[{x=N[x0],k=0},
While[ Abs[f'[x]] > eps && k < it, 
			Print["x",k,"=",x,",   f",k,"=",f[x]];
			x=x-f'[x]/f''[x];
			k=k+1
		];
		If[f''[x]<0,Print["Pronadjena je stacionarna tocka"]];{x,f[x]}
	];

q[x_]:=.5 x^3-6 x+2; x0=3;y0=4;
f[x_]:=(x-x0)^2+ (q[x] - y0)^2;
NewtonMin1[f,4,0.005,10]

FindMinimum[f[t],{t,4}]

NMinimize[f[t], {t, 4}]

Ilustracija metode parabole

\(Clear[x]; 
    MParabole[f_, a_, b_, c_] := 
      Module[{}, 
p2 = 
          InterpolatingPolynomial[{{a, f[a]}, {b, f[b]}, {c, f[c]}}, x] // 
            Expand; 

        koef = CoefficientList[p2, x]; 
P[x_] := 
          koef[\([1]\)] + koef[\([2]\)] x + 
            koef[\([3]\)] 
              x^2; 
xt = \(-koef[\([2]\)]\)/\((2 
                 koef[\([3]\)])\); 
cv = 
          Sort[N[{a, b, c, xt}]]; 
Print["Cvorovi= ", 
          cv]; 
Print["Fun= ", 
          t = f[cv]]; 
Print[
          Min[t]]; 
graf = 
          Plot[{f[x], P[x]}, {x, \(-2\), 2}, 
            DisplayFunction -> Identity]; 
tocke = 
          ListPlot[{{a, 0}, {b, 0}, {c, 0}, {xt, 0}}, 
            PlotStyle -> AbsolutePointSize[4], 
            DisplayFunction -> Identity]; 
Show[tocke, 
          graf, DisplayFunction -> $DisplayFunction];
]\)

f[x_] := 
      5  Abs[x + 1] - Abs[3  x + 2] + 
        Abs[5  x - 2]
a = -2; b = 2; c = 0;
MParabole[f, a, b, c]
k = 3;
c = cv[[k]];  a = cv[[k - 1]]; 
    b = cv[[k + 1]];
MParabole[f, a, b, c]