MATLAB实现最速下降法_和牛顿法和共轭梯度法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MATLAB实现最速下降法_和牛顿法和共轭梯度法最速下降法:
题目:f=(x-2)^2+(y-4)^2
M文件:
function [R,n]=steel(x0,y0,eps) syms x;
syms y;
f=(x-2)^2+(y-4)^2;
v=[x,y];
j=jacobian(f,v);
T=[subs(j(1),x,x0),subs(j(2),y,y0)]; temp=sqrt((T(1))^2+(T(2))^2); x1=x0;y1=y0;
n=0;
syms kk;
while (temp>eps)
d=-T;
f1=x1+kk*d(1);f2=y1+kk*d(2);
fT=[subs(j(1),x,f1),subs(j(2),y,f2)];
fun=sqrt((fT(1))^2+(fT(2))^2);
Mini=Gold(fun,0,1,0.00001);
x0=x1+Mini*d(1);y0=y1+Mini*d(2);
T=[subs(j(1),x,x0),subs(j(2),y,y0)];
temp=sqrt((T(1))^2+(T(2))^2);
x1=x0;y1=y0;
n=n+1;
end
R=[x0,y0]
调用黄金分割法:
M文件:
function Mini=Gold(f,a0,b0,eps) syms x;format long; syms kk;
u=a0+0.382*(b0-a0);
v=a0+0.618*(b0-a0);
k=0;
a=a0;b=b0;
array(k+1,1)=a;array(k+1,2)=b; while((b-a)/(b0-a0)>=eps) Fu=subs(f,kk,u);
Fv=subs(f,kk,v);
if(Fu<=Fv)
b=v;
v=u;
u=a+0.382*(b-a);
k=k+1;
elseif(Fu>Fv)
a=u;
u=v;
v=a+0.618*(b-a);
k=k+1;
end
array(k+1,1)=a;array(k+1,2)=b; end
Mini=(a+b)/2;
输入:
[R,n]=steel(0,1,0.0001)
R = 1.99999413667642 3.99999120501463 R = 1.99999413667642
3.99999120501463 n = 1
牛顿法:
题目:f=(x-2)^2+(y-4)^2
M文件:
syms x1 x2;
f=(x1-2)^2+(x2-4)^2;
v=[x1,x2];
df=jacobian(f,v);
df=df.';
G=jacobian(df,v);
epson=1e-12;x0=[0,0]';g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs (G,{x1,x2},{x0(1,1),x0(2,1)});k=0;mul_count=0;sum_count=0;
mul_count=mul_count+12;sum_count=sum_count+6; while(norm(g1)>epson) p=-G1\g1;
x0=x0+p;
g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});
G1=subs(G,{x1,x2},{x0(1,1),x0(2,1)});
k=k+1;
mul_count=mul_count+16;sum_count=sum_count+11;
end;
k
x0
mul_count
sum_count
结果::k = 1
x0 =
2
4
mul_count = 28
sum_count = 17 共轭梯度法:
题目:f=(x-2)^2+(y-4)^2
M文件:
function f=conjugate_grad_2d(x0,t)
x=x0;
syms xi yi a
f=(xi-2)^2+(yi-4)^2; fx=diff(f,xi);
fy=diff(f,yi);
fx=subs(fx,{xi,yi},x0); fy=subs(fy,{xi,yi},x0); fi=[fx,fy]; count=0;
while double(sqrt(fx^2+fy^2))>t
s=-fi;
if count<=0
s=-fi;
else
s=s1;
end
x=x+a*s;
f=subs(f,{xi,yi},x);
f1=diff(f);
f1=solve(f1);
if f1~=0
ai=double(f1);
else
break
x,f=subs(f,{xi,yi},x),count end
x=subs(x,a,ai);
f=xi-xi^2+2*xi*yi+yi^2;
fxi=diff(f,xi);
fyi=diff(f,yi);
fxi=subs(fxi,{xi,yi},x);
fyi=subs(fyi,{xi,yi},x);
fii=[fxi,fyi];
d=(fxi^2+fyi^2)/(fx^2+fy^2); s1=-fii+d*s;
count=count+1;