Newton's iterative method for solving nonlinear equations (with C++ code)

Table of contents

1. Introduction to the formula

2. Application environment

3. C++ code

Examples

C++ compilation environment

C++ code

operation result 

pay attention

Solution


1. Introduction to the formula

The basic formula of Newton's iterative method, iterated out{x^{(k + 1)}}

{x^{(k + 1)}} = {x^{(k)}} - \frac{​{f({x^{(k)}})}}{​{f'({x^{(k)}})}}

Facing the problem of nonlinear equations, the above formula is transformed into

{x^{(k + 1)}} = {x^{(k)}} - F'{(x)^{ - 1}}F(x)

where F'(x):

F'(x) = \left| {\begin{array}{*{20}{c}} {\frac{​{\partial f}}{​{\partial x}}}&{\frac{​{\partial f}}{​{\partial y}}}\\ {\frac{​{\partial g}}{​{\partial x}}}&{\frac{​{\partial g}}{​{\partial y}}} \end{array}} \right|

Note: here {x^{(k + 1)}}no longer represents a value, but represents (x,y)a matrix of variables. F(x)Similarly, it represents a formula matrix of x, y , F(x) = \left| {\begin{array}{*{20}{c}} {f(x,y)}\\ {g(x,y)} \end{array}} \right|( inner x is different from inner x !)F(x){f(x,y)}{g(x,y)}

2. Application environment

       The Newton iterative method is mainly used to solve the unknown variables, the calculation is difficult, and we cannot find the exact solution. Approximate solutions can be obtained by means of special iterations.

3. C++ code

Examples

\left\{ {\begin{array}{*{20}{c}} {​{x^2} - 2x - y + 0.5}\\ {​{x^2} + 4{y^2} - 4} \end{array}} \right.

C++ compilation environment

Program compilation environment: vs 2013

C++ code

#include<iostream>
#include<armadillo>
using namespace std;
using namespace arma;

//牛顿迭代法解非线性方程组
//例题:x^2-2x-y+0.5
//      x^2+4y^2-4
double f(double x0,double y0)
{
	return pow(x0, 2) - 2 * x0 - y0 + 0.5;
}
double g(double x0, double y0)
{
	return pow(x0, 2) + 4 * pow(y0, 2) - 4;
}
double fx(double x, double y)    //记录f求x偏导值
{
	double w=2 * x - 2;
	return w;
}

double fy(double x, double y)    //记录f求x偏导值
{
	double w = -1;
	return w;
}

double gx(double x, double y)    //记录f求x偏导值
{
	double w = 2 * x;
	return w;
}

double gy(double x, double y)    //记录f求x偏导值
{
	double w = 8 * y;
	return w;
}

void join(mat A)      //输入矩阵.x系数
{
	cout << "请输入未知数x个数" << endl;
	int n;
	cin >> n;
	for (int w = 1; w <= n; w++)
	{
		cout << "请输入第" << w << "行的值" << endl;
		for (int i = 0; i < n; i++)
		{
			cout << "请输入第" << i + 1 << "个数据的值" << endl;
			double temp;
			cin >> temp;
			A << temp;
		}
		A << endr;
	}
	
}

void ND(double x0,double y0)   //牛顿迭代法实现,x0,y0为初始值
{
	int n=0;
	double y1;
	mat T1;
	mat T;
	double x1;
	do
	{
		n++;
		y1 = y0;
		x1 = x0;
		mat temp;
		temp << fx(x0, y0) << fy(x0, y0) << endr
			<< gx(x0, y0) << gy(x0, y0) << endr;
		mat b;
		b << f(x0, y0) << endr
			<< g(x0, y0) << endr;

		T << x0 << endr
			<< y0 << endr;
		//mat r = inv(temp);
		T = T - inv(temp)*b;
		x0 = T(0, 0);
		y0 = T(1, 0);
	} while (abs(x0 - x1)> 1e-5||abs(y0 - y1)>1e-5);
	cout << "共迭代" << n << "次" << endl;
	cout << "最终结果为:" << x0 << "    " << y0 << endl;
}
void main()
{
	ND(2, 0.25);
}

operation result 

pay attention

       Since this algorithm has a matrix calculation, in order to make the code more concise and efficient, the above code calls the armadillo library to perform the corresponding matrix calculation (if the armadillo library is not configured, it will not pass when compiling, and an error will be reported!)

Solution

       In the following linked article, it explains in detail how to configure the armadillo library and use it, so that readers can solve the problem that the above c++ code fails to compile. At the same time, the armadillo library is also a very convenient library file for calculating matrices, and it is also very useful in other mathematical matrix calculations.

Link:

http://t.csdn.cn/3WF0I

Guess you like

Origin blog.csdn.net/qq_60811855/article/details/127751313