rtsec.cpp

来自「工程算法 这是一个很有用的工程数值算法集锦」· C++ 代码 · 共 37 行

CPP
37
字号
double rtsec(double x1,double x2,double xacc)
{
	int maxit,j;
	double fl,f,temp,xl,swap,dx;
    maxit = 30;
    fl = func(x1);
    f = func(x2);
    if (fabs(fl) < fabs(f) )
	{
        temp = x1;
        xl = x2;
        swap = fl;
        fl = f;
        f = swap;
	}
    else
	{
        xl = x1;
        temp = x2;
    }
    for (j = 1; j<= maxit; j++)
	{
        dx = (xl - temp) * f / (f - fl);
        xl =temp;
        fl = f;
        temp = temp + dx;
        f = func(temp);
        if ((fabs(dx) < xacc) ||(f == 0)) 
		{
			return temp;
			break;
		}
    }
    cout<< "rtsec exceed maximum iterations"<<endl;
	return 1;
}

⌨️ 快捷键说明

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