坐标轮换法的C++语言实现

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
X0.A[1][0] = 0;
double a,b,m;
CMatrix X1(2,1);
StepAdding_Search(a,b,X0,e1);
m = Fibonacci(a,b,X0,e1);
X1 = X0 + e2.Matrix_Multiply(m);
int i =1; //限制迭代的次数
while(distance(X1,X0) > eclipse)
if(a>b)
{
return a;
}
else
{
return b;
}
}
double Min_Num(double a,double b)
{
if(a<b)
{
return a;
}
else
{
return b;
}
}
void StepAdding_Search(double &a,double &b,CMatrix &mt1,CMatrix &mt2)
return x1*x1 + x2*x2;
}
CMatrix diff_fun(CMatrix &mt)
{
CMatrix temp(2,1);
temp.A[0][0] = 8*(mt.A[0][0] +1) +1;
temp.A[1][0] = 4*(mt.A[1][0] -1) +1;
return temp;
{
double x1 = X1.A[0][0] + t*X2.A[0][0];
double x2 = X1.A[1][0] + t*X2.A[1][0];
return x1*x1+x2*x2-x1*x2-10*x1-8*x2+5;
}
double Max_Num(double a,double b)
{
t2 = a + beta*(b-a);
result_2 = fun(t2,mt1,mt1);
while(true)
{
t1 = a+b-t2;
result_1 = fun(t1,mt1,mt2);
if(fabs(t1-t2)<eclipse)
{
return (t1+t2)/2;
}
else if(result_1 < result_2)
}
void main()
{
CMatrix e1(2,1);
CMatrix e2(2,1);
e1.A[0][0] = 1;
e2.A[0][0] = 0;
e1.A[1][0] = 0;
e2.A[1][0] = 1;
int n = 2; //二维的方程
CMatrix X0(2,1);
X0.A[0][0] = 0;
{
b = t1;
t1 = t2;
result_1 = result_2;
t2 = a+beta*(b-a);
result_2 = fun(t2,mt1,mt2);
}
else if(result_1 == result_2)
{
a = t1;
b = t2;
result_2 = fun(a,mt1,mt2);
{
t[k+1]=t[k]+h[k];
f[k+1]=fun(t[k+1],mt1,mt2);
if(f[k+1]<f[k])
{
h[k+1]=p*h[k];
result=t[k];
t[k]=t[k+1];
f[k]=f[k+1];
}
else if(k == 0)
{
h[k]=-h[k];
result=t[k+1];
#define eclipse 0.01
CMatrix hesse()
{
CMatrix temp(2,2);
temp.A[0][0] = 8;
temp.A[0][1] = 0;
temp.A[1][0] = 0;
temp.A[1][1] =4;
return temp;
}
double fun(double t,CMatrix &X1,CMatrix &X2)
{
double t[MAX_M]={0};
double h[MAX_M]={0};
double f[MAX_M]={0};
double result=0;
double p=2.055;
t[0]=0;
h[0]=1;
f[0]=fun(t[0],mt1,mt2);
for(int k=0;k<MAX_M-1;k++)
}
else
{
a=Min_Num(result,t[k+1]);
b=Max_Num(result,t[k+1]);
}
}
}
double Fibonacci(double &a,double &b,CMatrix mt1,CMatrix mt2)
{ቤተ መጻሕፍቲ ባይዱ
double t2,t1,result_1,result_2;
m = Fibonacci(a,b,X0,e2);
X1 = X0 + e2.Matrix_Multiply(m);
}
i++;
}
cout<<"求解的结果是:"<<endl;
cout<<"("<<X1.A[0][0]<<","<<X1.A[1][0]<<")"<<endl;
cout<<"最优解是:"<<endl;
cout<<fun(m,X1,e2)<<endl;
}
坐标轮换法的C++语言实现
----------------------基于成都理工大学最优化教材
#include <iostream.h>
#include "Matrix.h"
#include<LIMITS.H>
#define MAX_M 2048
#include<math.h>
#define beta (sqrt(5)-1)/2
}
else
{
a = t2;
t2 = t1;
result_2 = result_1;
}
}
}
double distance(CMatrix &X1,CMatrix &X2)
{
double x1 = X1.A[0][0] - X2.A[0][0];
double x2 = X1.A[1][0] - X2.A[1][0];
{
X0 = X1;
if(i%2 == 0)
{
if(i>3) break;
StepAdding_Search(a,b,X0,e1);
m = Fibonacci(a,b,X0,e1);
X1 = X0 + e1.Matrix_Multiply(m);
}
else
{
StepAdding_Search(a,b,X0,e2);
相关文档
最新文档