// 输入: Pos:1 * 2,两端点 // n:阶数 // 输出: tn:精确的零点 // 方法: 二分法(LGR) // 编写: 李兆亭,2019 / 11 / 04 #include "GPM.h" int Dichotomy_LGR(int* n, vec* Pos, double* tn) { double tol = 1e-12; int Iscontinue = 1; double fx1, fx2, fx; double fy1, fy2, fy; double fav1, fav2, fav; double x = (*Pos)(0); double y = (*Pos)(1); double av = (x + y) / 2; int n1 = (*n) + 1; Legendre(&n1, &x, &fx1); Legendre(n, &x, &fx2); fx = fx1 + fx2; // cout << fx << endl; Legendre(&n1, &y, &fy1); Legendre(n, &y, &fy2); fy = fy1 + fy2; Legendre(&n1, &av, &fav1); Legendre(n, &av, &fav2); fav = fav1 + fav2; while (Iscontinue == 1) { if (abs(fx) <= tol) { *tn = x; Iscontinue = 0; } else if (abs(fy) <= tol) { *tn = y; Iscontinue = 0; } else if (abs(fav) <= tol) { *tn = av; Iscontinue = 0; } else { if (fx*fav < 0) { x = x; y = av; av = (x + y) / 2; Legendre(&n1, &y, &fy1); Legendre(n, &y, &fy2); fy = fy1 + fy2; Legendre(&n1, &av, &fav1); Legendre(n, &av, &fav2); fav = fav1 + fav2; } else if (fav*fy < 0) { x = av; y = y; av = (x + y) / 2; Legendre(&n1, &x, &fx1); Legendre(n, &x, &fx2); fx = fx1 + fx2; Legendre(&n1, &av, &fav1); Legendre(n, &av, &fav2); fav = fav1 + fav2; } } } return 1; }