matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法

最速下降法迭代步骤:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第1张图片

最速下降法matlab实现:

x0 = [2;2];
e=0.01;
A=[2,0;0,50];
f0=f(x0);
g0=g(x0);
temp=g0(1)*g0(1)+g0(2)*g0(2);
while temp>e*e
    x = x0-(g0.'*g0)/(g0.'*A*g0)*g0;
    g0 = g(x);
    temp=g0(1)*g0(1)+g0(2)*g0(2);
    if(temp<=e*e)
        break
    end
    x0 = x;
end
x
f(x)

function s = f(x)
s=x(1)*x(1)+25*x(2)*x(2);
end

function s = g(x)
s=[2*x(1);50*x(2)];
end

实验结论:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第2张图片

Newton法迭代步骤:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第3张图片

Newton法matlab实现:

x0 = [0;0];
e=0.01;
G=[2,-1;-1,2];
G1=inv(G);
f0=f(x0);
g0=g(x0);
temp=g0(1)*g0(1)+g0(2)*g0(2);
while temp>e*e
    x = x0-G1*g0;
    g0 = g(x);
    temp=g0(1)*g0(1)+g0(2)*g0(2);
    if(temp<=e*e)
        break
    end
    x0 = x;
end
x
f(x)

function s = f(x)
s=60-10*x(1)-4*x(2)+x(1)*x(1)+x(2)*x(2)-x(1)*x(2);
end

function s = g(x)
s=[-10+2*x(1)-x(2);-4+2*x(2)-x(1)];
end

实验结论:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第4张图片

修正Newton法迭代步骤:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第5张图片

修正Newton法matlab实现:

x0 = [0;0];
e=0.01;
G=[8,0;0,4];
G1=inv(G);
f0=f(x0);
g0=g(x0);
temp=g0(1)*g0(1)+g0(2)*g0(2);
while temp>e*e
    p0 = -G1*g0;
    t0 = (-8*x0(1)*p0(1)-8*p0(1)-4*x0(2)*p0(2)+4-(p0(1)+p0(2)))/(8*p0(1)*p0(1)+4*p0(2)*p0(2));
    x = x0+t0*p0;
    g0 = g(x);
    temp=g0(1)*g0(1)+g0(2)*g0(2);
    if(temp<=e*e)
        break
    end
    x0 = x;
end
x
f(x)

function s = f(x)
s=4*(x(1)+1)*(x(1)+1)+2*(x(2)-1)*(x(2)-1)+x(1)+x(2)+10;
end

function s = g(x)
s=[8*x(1)+9;4*x(2)-3];
end

实验结论:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第6张图片

 

共轭梯度法迭代步骤:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第7张图片

共轭梯度法matlab实现:

x0 = [1;1];
e=0.01;
f0=f(x0);
g0=g(x0);
temp=g0(1)*g0(1)+g0(2)*g0(2);
while temp>e*e
    p0 = -g0;
    t0 = (-4*x0(1)*p0(1)-2*x0(2)*p0(2)+x0(1)*p0(2)+x0(2)*p0(1))/(4*p0(1)*p0(1)+2*p0(2)*p0(2)-2*p0(1)*p0(2));
    x = x0+t0*p0;
    gg = g(x);
    temp1=gg(1)*gg(1)+gg(2)*gg(2);
    lamda = gg/g0;
    p=-gg+lamda*p0;
    t = (-4*x(1)*p(1)-2*x(2)*p(2)+x(1)*p(2)+x(2)*p(1))/(4*p(1)*p(1)+2*p(2)*p(2)-2*p(1)*p(2));
    x0 = x+t*p;
    g0=g(x0);
    temp=g0(1)*g0(1)+g0(2)*g0(2);
    if(temp<=e*e)
        break
    end
    t0 = t;
    p0 = p;
end
x0
f(x0)

function s = f(x)
s=2*x(1)*x(1)+x(2)*x(2)-x(1)*x(2);
end

function s = g(x)
s=[4*x(1)-x(2);2*x(2)-x(1)];
end

实验结论:

选取初始点(0,0)

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第8张图片

选取初始点(0,1)

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第9张图片

选取初始点(1,1)

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第10张图片

DFP法迭代步骤:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第11张图片

DFP法matlab实现:

x0 = [8;9];
e=0.01;
f0=f(x0);
g0=g(x0);
t=g(x0);
temp=t(1)*t(1)+t(2)*t(2);
H0=[1,0;0,1];
while temp>e*e
    p0=-g0;
    tk=(-8*x0(1)*p0(1)+40*p0(1)-2*x0(2)*p0(2)+12*p0(2))/(8*p0(1)*p0(1)+2*p0(2)*p0(2));
    x=x0+tk*p0;
    ff=f(x);
    gg=g(x);
    t=g(x);
    temp1=t(1)*t(1)+t(2)*t(2);
    if(temp1<=e*e)
        break
    end
    s=x-x0;
    y=gg-g0;
    H0=H0+(s*s.')/(s.'*y)-(H0*y*y.'*H0)/(y.'*H0*y);
    p0=-H0*gg;
    x0=x;
    tk=(-8*x0(1)*p0(1)+40*p0(1)-2*x0(2)*p0(2)+12*p0(2))/(8*p0(1)*p0(1)+2*p0(2)*p0(2));
    x=x0+tk*p0;
    ff=f(x);
    gg=g(x);
    t=g(x);
    temp1=t(1)*t(1)+t(2)*t(2);
    if(temp1<=e*e)
        break
    end
    s=x-x0;
    y=gg-g0;
    H0=H0+(s*s.')/(s.'*y)-(H0*y*y.'*H0)/(y.'*H0*y);
    p0=-H0*gg;
    x0=x;
    f0=ff;
    g0=gg;
end
x
f(x)

function s = f(x)
s=4*(x(1)-5)*(x(1)-5)+(x(2)-6)*(x(2)-6);
end

function s = g(x)
s=[8*x(1)-40;2*x(2)-12];
end

实验结论:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第12张图片

坐标轮换法迭代步骤:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第13张图片

坐标轮换法matlab实现:

x0 = [0;0];
e=0.1;
e1=[1;0];
e2=[0;1];
while 1
    p=e1;
    t0 = (-2*x0(1)*p(1)-2*x0(2)*p(2)+x0(1)*p(2)+x0(2)*p(1)+10*p(1)+4*p(2))/(2*p(1)*p(1)+2*p(2)*p(2)-2*p(1)*p(2));
    x = x0+t0*p;
    p=e2;
    t0 = (-2*x(1)*p(1)-2*x(2)*p(2)+x(1)*p(2)+x(2)*p(1)+10*p(1)+4*p(2))/(2*p(1)*p(1)+2*p(2)*p(2)-2*p(1)*p(2));
    x = x+t0*p;
    if(x(1)-x0(1))*(x(1)-x0(1))+((x(2)-x0(2))*(x(2)-x0(2))<=e*e)
        break
    end
    x0 = x;
end
x
f(x)

function s = f(x)
s=x(1)*x(1)+x(2)*x(2)-x(1)*x(2)-10*x(1)-4*x(2)+60;
end

实验结论:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第14张图片

单纯形法迭代步骤:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第15张图片

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第16张图片

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第17张图片

单纯形法matlab实现:

x=zeros(2,3);
x(:,1) = [0;0];
x(:,2) = [0.965; 0.259];
x(:,3) = [0.259; 0.965];
e=0.1;
a = 1.1;
b = 0.5;
while 1
    [~,l]=min([f(x(:,1)),f(x(:,2)),f(x(:,3))]);
    [~,h]=min([f(x(:,1)),f(x(:,2)),f(x(:,3))]);
    xl=x(:,l);
    xh=x(:,h);
    for i =1:3
        if (i ~= l) && (i ~= h)
            g = i;
            break;
        end
    end
    xg = x(:,g);
    x1 = 0.5 *(xg+xl-2*xh);
    x2 = 2*x1 - xh;
    if f(x2)< f(xl)
        x3 = x1 + a*(x2-x1);
        if(f(x3)=f(xh)
        x5 = x1+b*(xh-x1);
        if f(x5)>=f(xh)
            for i = 1:3
                if i ~= l
                    x(:,i) = 0.5*(x(:,i)+xl);
                end
            end
        else
            x(:,h)=x5;
        end
    end
    if (f(x(:,1))-f(xl))*(f(x(:,1))-f(xl))+(f(x(:,2))-f(xl))*(f(x(:,2))-f(xl))+(f(x(:,3))-f(xl))*(f(x(:,3))-f(xl))<=e
        x=xl;
        break
    end
end
x
f(x)

function s = f(x)
s=x(1)*x(1)+2*x(2)*x(2)-4*x(1)-8*x(2)+5;
end

实验结论:

matlab-最速下降法,牛顿法,修正牛顿法,共轭梯度法,DFP法,坐标轮换发,单纯形法_第18张图片

你可能感兴趣的:(算法)