Skip to content

Instantly share code, notes, and snippets.

@chaonan99
Created May 15, 2016 13:32
Show Gist options
  • Save chaonan99/b313bbe8f852ead8cf075dd9771bf991 to your computer and use it in GitHub Desktop.
Save chaonan99/b313bbe8f852ead8cf075dd9771bf991 to your computer and use it in GitHub Desktop.
function []=bs()
x=[0,0];
dfx_val = dfx(x);
normdfx = norm(dfx_val);
k = 0;
f_val = zeros(1000,1);
dfx_val_pre = [1,1];
p_pre = [0,0];
while normdfx>1e-10
if mod(k,2) == 0
p = -dfx_val;
else
p = -dfx_val + dfx_val*(dfx_val-dfx_val_pre)'/...
(p_pre*(dfx_val-dfx_val_pre)')*p_pre;
end
t = newton(x,p,1e-10);
p_pre = p;
dfx_val_pre = dfx_val;
x = x+t*p;
dfx_val = dfx(x);
normdfx = norm(dfx_val);
k = k+1;
f_val(k) = f(x);
end
format long;
disp('最优解:');x
disp('最优函数值:');f(x)
fprintf('总迭代次数:%d\n',k);
plot((1:k),f_val(1:k));title('函数值与迭代次数关系图');
end
function [val] = f(x)
val=(1-x(1))^2+2*(x(2)-x(1)^2)^2;
end
function [val] = dfx(x)
val(1)=-2*(1-x(1))-8*x(1)*(x(2)-x(1)^2);
val(2)=4*(x(2)-x(1)^2);
end
function []=fr()
x=[0,0];
dfx_val = dfx(x);
normdfx = norm(dfx_val);
k = 0;
f_val = zeros(1000,1);
normdfx_pre = normdfx;
p_pre = [0,0];
while normdfx>1e-10
if mod(k,2) == 0
p = -dfx_val;
else
p = -dfx_val + normdfx^2/normdfx_pre^2*p_pre;
end
t = goldstein(x,p,[0.3,0.7]);
p_pre = p;
normdfx_pre = normdfx;
x = x+t*p;
dfx_val = dfx(x);
normdfx = norm(dfx_val);
k = k+1;
f_val(k) = f(x);
end
format long;
disp('最优解:');x
disp('最优函数值:');f(x)
fprintf('总迭代次数:%d\n',k);
plot((1:k),f_val(1:k));title('函数值与迭代次数关系图');
end
function [val] = f(x)
val=(1-x(1))^2+2*(x(2)-x(1)^2)^2;
end
function [val] = dfx(x)
val(1)=-2*(1-x(1))-8*x(1)*(x(2)-x(1)^2);
val(2)=4*(x(2)-x(1)^2);
end
function [ a ] = goldstein( x,p,m )
syms t;
alpha = 1.5;
ff = (1-(x(1)+t*p(1)))^2+2*((x(2)+t*p(2))-(x(1)+t*p(1))^2)^2;
ff0 = double(subs(ff,t,0));
df = diff(ff);
df0 = double(subs(df,t,0));
a = 0; b = Inf; k = 0; q = 1;
while (1)
phit = double(subs(ff,t,q));
if phit<= ff0+m(1)*q*df0
if phit>= ff0+m(2)*q*df0
a = q;
break;
else
a = q;
end
else
b = q;
end
if b == Inf
q = alpha*q;
else
q = (a+b)/2;
end
k = k + 1;
end
end
function []=gradient()
x=[0,0];
dfx_val = dfx(x);
normdfx = norm(dfx_val);
k = 0;
f_val = zeros(1000,1);
while normdfx>1e-10
t = newton(x,-dfx_val,1e-10);
x = x-t*dfx_val;
dfx_val = dfx(x);
normdfx = norm(dfx_val);
k = k+1;
f_val(k) = f(x);
end
format long;
disp('最优解:');x
disp('最优函数值:');f(x)
fprintf('总迭代次数:%d\n',k);
plot((1:k),f_val(1:k));title('函数值与迭代次数关系图');
end
function [val] = f(x)
val=(1-x(1))^2+2*(x(2)-x(1)^2)^2;
end
function [val] = dfx(x)
val(1)=-2*(1-x(1))-8*x(1)*(x(2)-x(1)^2);
val(2)=4*(x(2)-x(1)^2);
end
function [a] = newton(x,p,eps)
syms t;
ff = (1-(x(1)+t*p(1)))^2+2*((x(2)+t*p(2))-(x(1)+t*p(1))^2)^2;
df = diff(ff);
phi = t-df/diff(df);
a=10;nexta=0;
while abs(a-nexta)>eps
a=nexta;
nexta = double(subs(phi,t,a));
end
end
function []=pr()
x=[0,0];
dfx_val = dfx(x);
normdfx = norm(dfx_val);
k = 0;
f_val = zeros(1000,1);
dfx_val_pre = [0,0];
normdfx_pre = normdfx;
p_pre = [0,0];
while normdfx>1e-10
if mod(k,2) == 0
p = -dfx_val;
else
p = -dfx_val + dfx_val*(dfx_val-dfx_val_pre)'/normdfx_pre^2*p_pre;
end
t = newton(x,p,1e-10);
p_pre = p;
normdfx_pre = normdfx;
dfx_val_pre = dfx_val;
x = x+t*p;
dfx_val = dfx(x);
normdfx = norm(dfx_val);
k = k+1;
f_val(k) = f(x);
end
format long;
disp('最优解:');x
disp('最优函数值:');f(x)
fprintf('总迭代次数:%d\n',k);
plot((1:k),f_val(1:k));title('函数值与迭代次数关系图');
end
function [val] = f(x)
val=(1-x(1))^2+2*(x(2)-x(1)^2)^2;
end
function [val] = dfx(x)
val(1)=-2*(1-x(1))-8*x(1)*(x(2)-x(1)^2);
val(2)=4*(x(2)-x(1)^2);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment