Chapter 39 gsl(Multidimensional minimization)

本文介绍了一种解决多维最小化问题的方法,通过使用GSL库中的搜索算法从初始猜测开始,寻找使标量函数值低于任何邻近点的点。文中详细解释了算法的步骤,并提供了一个具体的C++代码示例,演示了如何实现并找到函数的最小值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Overview

The problem of multidimensional minimization requres finding a point x such that the scalar function,

f(x_1, x_2,...,x_n)

takes a value which is lower than at any neighboring point. For smooth functions the gradient g = \bigtriangledown f, vanishes at the minimum. In general there are no bracketing methods available for the minimization of n-dimensional functions. The algorithms proceed from an initial guess using a search algorithm which attempts to move in a downhill direction.

使用步骤

Steps

1、initialize minimizer state, s,  for algoritm T

2、update s using the iteration T

3、test s for convergence, and repeat iteration if necessary

下面是gsl第39章的例子,这里贴出来。

#include<stdio.h>
#include<iostream>
#include<gsl/gsl_multimin.h>
#include<gsl/gsl_vector.h>
#include<iomanip>
using namespace std;

/*	Paraboloid centered on (p[0], p[1]), with 
	scale factors (p[2], p[3]) and minimum p[4]	*/
double my_f(const gsl_vector *v, void *params)
{
	double x, y;
	double *p = (double *)params;

	x = gsl_vector_get(v, 0);
	y = gsl_vector_get(v, 1);
	
	return p[2] * (x - p[0]) * (x - p[0]) +
		p[3] * (y - p[1]) * (y - p[1]) + p[4];
}

/*	The gradient of f, df = (df/dx, df/dy).	*/
void my_df(const gsl_vector *v, void *params, gsl_vector *df)
{
	double x, y;
	double *p = (double *)params;

	x = gsl_vector_get(v, 0);
	y = gsl_vector_get(v, 1);

	gsl_vector_set(df, 0, 2.0 * p[2] * (x - p[0]));		//将2.0 * p[2] * (x - p[0])计算的值放在向量df第0个位置
	gsl_vector_set(df, 1, 2.0 * p[3] * (y - p[1]));		//将2.0 * p[3] * (y - p[1])计算的值放在向量df第1个位置
}

/*	Compute both f and df together.	*/
void my_fdf(const gsl_vector *x, void *params, double *f, gsl_vector *df)
{
	*f = my_f(x, params);
	my_df(x, params, df);
}


int main()
{
	size_t iter = 0;
	int status;

	const gsl_multimin_fdfminimizer_type *T;	//this type specifies a minimization algorithm using gradients
	gsl_multimin_fdfminimizer *s;				//this is a workspace for minimizing functions using derivatives

	gsl_vector *x;
	gsl_multimin_function_fdf my_func;			//this data type defines a general function of n variables with parameters
												// and the correspongding gradient vector of derivatives
	/*	Paraboloid center at (1, 2), scale factors (10, 20),
	minimum value 30	*/
	double par[5] = { 1.0, 2.0, 10.0, 20.0, 30.0 };
	my_func.n = 2;	/*	number of function components	*/
	my_func.f = &my_f;
	my_func.df = &my_df;
	my_func.fdf = &my_fdf;
	my_func.params = par;

	/*	Starting point, x = (5, 7)*/
	x = gsl_vector_alloc(2);
	gsl_vector_set(x, 0, 5.0);
	gsl_vector_set(x, 1, 7.0);

	T = gsl_multimin_fdfminimizer_conjugate_fr;
	s = gsl_multimin_fdfminimizer_alloc(T, 2);		//this function returns a pointer to a newly allocated instance of a minimizer of type
													// T for an n-dimension function
	gsl_multimin_fdfminimizer_set(s, &my_func, x, 0.01, 1e-4);	//initialize the minimizer s to minimize the function fdf starting from the initial
																//point x. the size of the first trial step is given bu step_size. The accuracy of the line
																//minimization is specified bu tol.
	do
	{
		iter++;
		status = gsl_multimin_fdfminimizer_iterate(s);

		if (status)
			break;

		status = gsl_multimin_test_gradient(s->gradient, 1e-3);
		if (status == GSL_SUCCESS)
			cout << "Minimum found at: \n";
		cout<< setw(5) <<fixed<< setprecision(5) << iter <<
			setw(10)<< setprecision(5) << gsl_vector_get(s->x, 0) <<
			setw(10)<< setprecision(5) << gsl_vector_get(s->x, 1) <<
			setw(12)<< setprecision(5) << s->f << endl;
	} while (status == GSL_CONTINUE && iter < 100);
	gsl_multimin_fdfminimizer_free(s);
	gsl_vector_free(x);




	/*
	gls库vector使用
	int i;
	gsl_vector *v = gsl_vector_alloc(3);
	for (int i = 0; i < 3; i++)
	{
		gsl_vector_set(v, i, 1.23 + i);
	}
	for (int i = 0; i < 100; i++)
	{
		cout << gsl_vector_get(v, i) << endl;
	}
	gsl_vector_free(v);
	*/

	/*
	double params[5] = { 1, 2, 3, 4, 5 };
	double *p = params;
	cout << p[1] << endl;
	*/
	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值