2018年12月29日 星期六

例題 EX2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0 C語言

例題 EX2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0 C語言

y= x
y=1/5^x




/* 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

沒有留言:

張貼留言

Adam DO*5 DI*12 + Python + Node-Red

 Adam DO*5 DI*12 + Python + Node-Red 為了讓您的系統穩定運行,建議的最終接線如下: 功能 ADAM 端子 對接目標 DI 輸入 (乾接點) DI0 ~ DI11 開關的一端 DI 共同端 DI.COM 開關的另一端 (接 GND) DO 輸出...