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

沒有留言:

張貼留言

2024產專班 作業2

 2024產專班 作業2   1. 系統圖       ESP32+MFRC522 組成RFID Reader 可以將RFID卡片的UID 透過 MQTT協定    上傳(發行 主題 (:topic) alex9ufo/2024/RFID/RFID_UID  ,, Payload...