/* ex2-9.c is used for solving nonlinear equation
* based on Fixed-Point Algorithm g(x)=x with initial
* approximation P0.
*/
#include <stdio.h>
#include <math.h>
#define MAX 50
#define TOL 0.0001
#define g(x) (1/pow(5,x))
void main()
{
int i=1;
double x0,x;
x0=0.45;
while(i<=MAX)
{
x=g(x0);
printf("%-2d %10.7lf\n",i-1,x0);
if(fabs(x-x0) < TOL)
{
printf("The Root=%10.7lf x-x0=%10.7lf\n",x,fabs(x-x0));
exit(0);
}
i++;
x0=x;
}
printf("Fixed-point failed after %d iteration.\n",i);
return;
}
輸入畫面 : 無
輸出畫面
0 0.4500000
1 0.4846894
2 0.4583705
3 0.4782035
4 0.4631803
5 0.4745160
6 0.4659374
7 0.4724151
8 0.4675155
9 0.4712167
10 0.4684181
11 0.4705327
12 0.4689340
13 0.4701421
14 0.4692289
15 0.4699191
16 0.4693974
17 0.4697917
18 0.4694936
19 0.4697189
20 0.4695486
21 0.4696773
The Root= 0.4695801 x-x0= 0.0000973
沒有留言:
張貼留言