发布网友 发布时间:2022-10-08 03:29
共1个回答
热心网友 时间:2023-11-02 15:24
求方程 的实根
由零点定理知原方程在 内有实根,
那么迭代公式为:
取初始值 ,迭代序列如下: x01x10.75036386784024x20.73911289091136x30.73908513338528x40.73908513321516x50.73908513321516由此得到方程的实根为x=0.73908513321516...
上例的c语言程序代码为: #include math.h#include stdio.h#define ABS(a) ((a)<0?-(a):(a))static double f(double x){ return x-cos(x);}static double df(double x){ return 1+sin(x);}void main(){ int i; double x0=1, x1, err=1e-14; for(i=1; i<50; i++) { x1=x0-f(x0)/df(x0); printf(x[%d]=%.14f\n, i, x1); if(ABS(x1-x0)<err) break; x0=x1; }} 设 ,那么 ,
从而迭代公式为:
举个栗子:求
对应的迭代公式为:
取初值x0=2,迭代如下: x02x13.75000000000000x23.18740740740741x33.07642248706644x43.07232230248782x53.07231682569561x63.07231682568585x73.07231682568585从上面可以看出,只要迭代6次即可求出15位精度的近似值 // 2015-12-24// By: ChenYu#include math.h#include stdio.h#define ABS(a) ((a)<0?-(a):(a))#ifdef _WIN32 typedef unsigned __int64 uint64;#else typedef unsigned long long uint64;#endif// calculate a approximate valuestatic double calcInitRoot(double x, int n){ const uint64 exptMask=((uint64)1<<11)-1; const uint64 fracMask=((uint64)1<<52)-1; uint64 xInt=*(uint64*)&x; int xExpt=(int)((xInt>>52)&exptMask)-1023; xInt=((uint64)((xExpt+1024*n-1)/n)<<52)+(xInt&fracMask)/n; return *(double*)&xInt;}double calcRoot(double x, int n){ int i, j, s=1-((x<0)<<(n&1)); double a=ABS(x); double x1, x0=calcInitRoot(a, n); double err=x0*1e-14; if(x==0) return 0; for(i=1; i<50; i++) { double xn=1; for(j=0; j<n-1; j++) xn*=x0; x1=((n-1)*x0*xn+a)/(xn*n);// printf(x%d=%.14f\n, i, x1); if(ABS(x1-x0)<=err) break; x0=x1; } return s*x1;}void main(){ double x=-31141.592653589793; int n=11; double y=calcRoot(x, n); printf(root(%g,%d)=%+.14f\n, x, n, y); printf(root(%g,%d)=%+.14f\n, x, n, pow(ABS(x), 1.0/n));}