资源描述:
《共轭梯度法求三元函数极小值》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#includeclassVector{public:Vector(){x1=0;x2=0;x3=0;}Vector(doublexx1,doublexx2,doublexx3){x1=xx1;x2=xx2;x3=xx3;}friendVectoroperator+(Vectorv1,Vectorv2);friendVectoroperator-(Vector&v1,Vector&v2);friendVectoroperator-(Vector&v1);frienddoubleoperator*(Vector&
2、v1,Vector&v2);friendVectoroperator*(doublec,Vector&v);friendVectoroperator*(Vector&v,doublec);~Vector(){}voiddisplay();//private:doublex1,x2,x3;};Vectoroperator+(Vectorv1,Vectorv2){returnVector(v1.x1+v2.x1,v1.x2+v2.x2,v1.x3+v2.x3);}Vectoroperator-(Vector&v1,Vector&v2){returnVector(v1.x
3、1-v2.x1,v1.x2-v2.x2,v1.x3-v2.x3);}Vectoroperator-(Vector&v1){returnVector(-v1.x1,-v1.x2,-v1.x3);}doubleoperator*(Vector&v1,Vector&v2){return(v1.x1*v2.x1+v1.x2*v2.x2+v1.x3*v2.x3);}Vectoroperator*(doublec,Vector&v){returnVector(c*v.x1,c*v.x2,c*v.x3);}Vectoroperator*(Vector&v,doublec){ret
4、urnVector(c*v.x1,c*v.x2,c*v.x3);}voidVector::display(){cout<<"("<5、nVector(2*(p.x1-p.x2)+2,-2*p.x1+4*p.x2-p.x3+3,2*p.x3-p.x2-1);}doubleGetModulus(Vector&p){returnsqrt(p.x1*p.x1+p.x2*p.x2+p.x3*p.x3);}/*doubleGetFValue(doublela)//求函数值f(x[k]+lamda*s[k]){returnValueFunction(x[k]+la*s[k]);}*/voidmain(){intn=3;//本题为三维问题doubleValueFunction(Vector&p);VectorGr
6、adFuction(Vector&p);Vectorstartpoint(0,0,0);Vectorx[10000]={startpoint},g[10000],s[10000];doublelamda[10000],lamdak;intk;cout<<"选取初始点为:x[0]=";x[0].display();doubleprecision=0.000001;step2:{g[0]=GradFuction(x[0]);s[0]=-g[0];k=0;}step3:{doubleGetFValue(doublela);doublef1,f0,la1,la0=1,h=1
7、0,e=0.0000001;step12:la1=la0+h;f1=ValueFunction(x[k]+la1*s[k]);f0=ValueFunction(x[k]+la0*s[k]);if(f1f0"<8、h
9、为:"<10、h
11、