/* ATK IV - Numerical Programming (2006) */ /* Excercise 6.3 - General Iteration Formula */ #include #include #define Pi 3.141592654 /* We have to transfer the equation tan(x)=x => x = arctan(x) (+ n*Pi) */ /* because in the general iteration formula the necessary condition for */ /* convergence is that f(x) is defined and differentiable in such way */ /* that the values of the function are bounded in a given interval. */ /* Additionally, in the neighbourhood of the root, one has to have */ /* |f'(x)| = k < 1. For tan(x) this is not the case, but for arctan(x), */ /* however, it is! */ float f(float x, int n){ return atan(x)+n*Pi;} float giter(float x,float acc); int n; int main(void){ float acc=1.0e-5; float x=0.0; float y; int i; for(i=0;i<3;i++){ n=i; y=giter(x,acc); printf("%d %f\n",i+1,y); } return 0; } float giter(float x,float acc){ float x1=x; float x2=f(x1,n); while(fabs(x1-x2)>acc){ x1=x2; x2=f(x1,n); } return x2; }