2018年12月29日 星期六

利用 正割法 解 非線性方程式 f(x) (exp(x)+1/pow(2,x)+2*cos(x)-6) C語言

利用 正割法 解 非線性方程式 f(x)   (exp(x)+1/pow(2,x)+2*cos(x)-6) C語言

程式
#include <stdio.h>
#include <math.h>
#define  MAX  50
#define  PI  3.14159
#define  TOL  0.00001
#define  f(x)   (exp(x)+1/pow(2,x)+2*cos(x)-6)
void main()
{
   int i=2;
   double x0,x1,x,q0,q1;
   x0=1.8;
   x1=2.0;
   q0=f(x0);
   q1=f(x1);
   printf("i       xi           f(x)\n");
   printf("%-2d   %10.6lf   %10.6lf\n",0,x0,q0);
   printf("%-2d   %10.6lf   %10.6lf\n",1,x1,q1);
   while(i<=MAX)
   {
      x=x1-q1*(x1-x0)/(q1-q0);
      printf("%-2d   %10.6lf   %10.6lf\n",i,x,f(x));
      if(fabs(x-x1) < TOL)
      {
printf("The Root=%10.6lf f(%10.6lf)=%10.6lf\n",x,x,f(x));
exit(0);
      }
      else
      {
i++;
x0=x1;
q0=q1;
x1=x;
q1=f(x);
      }
   }
   if(i>MAX)
     printf("Secant Method faileds!!!\n");
   return;
}
===================
輸出畫面
===================
i       xi           f(x)
0      1.800000    -0.117582
1      2.000000     0.806762
2      1.825441    -0.016116
3      1.828860    -0.002147
4      1.829385     0.000007
5      1.829384    -0.000000
The Root=  1.829384 f(  1.829384)= -0.000000

沒有留言:

張貼留言

2024產專班 作業2

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