直接法一维收索.cpp

来自「C++最优化方法源程序」· C++ 代码 · 共 23 行

CPP
23
字号
#include "mathyw.h"
#include "iostream.h"
#include "stdlib.h"
#include "math.h"

double fun( double * x );

void main ()
{
	double x[4] = { 1, 1, 1, 1 };
	double p[4] = { -2, -4, -6, -8 };
	double landa = line_search1( fun, 4, x, p );
	for( int i=1; i<=4; i++ )
		x[i-1] += landa* p[i-1];
	for( i=1; i<=4; i++ )
		cout<< "x[" << i << "]=" << x[i-1]<< endl;
	cout<< "f(x)=" << fun(x) << endl;
}

double fun( double * x )
{
	return x[0]* x[0] + 2* x[1]* x[1]+ 3* x[2]* x[2] + 4* x[3]* x[3];
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?