2019年4月12日 星期五

C語言 例題2-11定點迴路法Fixed point Method 求x^2-5=0

C語言 例題2-11定點迴路法Fixed point Method 求x^2-5=0

f(x)= x^2-5=0
g(x)=x  , g(x)= ((5/x)+x)/2

#include<conio.h>
#include<stdio.h>
#include<math.h>
#define f(x) ((5/x)+x)/2
#define e 0.0001
#define MAX 100

int main(void)
{
    float x0,x1,f1 ;
    int i=1;
    clrscr();
    printf("Using Fixed Point Method / Iterative Method to find root of x^2-5=0\n");
    printf("Enter value for x0\n");
    printf("x0: ");
    scanf("%f",&x0);
   
    printf("steps\tx0\tx1\n");
   
    while (i<=MAX)
    {
        x1=f(x0);
        printf("%d\t%.4f\t%.4f\n",i,x0,x1);
        if(fabs((x1-x0)/x1)<=e)
        {
            printf("The root is %.4f",x1);
            exit(0);
        }
        else
        {
            x0=x1;
            i++;
        }
    }
    getch();

}

SDTID輸入資料
0.5 或 -5

輸出畫面
Using Fixed Point Method / Iterative Method to find root of x^2-5=0
Enter value for x0
x0:  0.5

steps x0 x1
1 0.5000 5.2500
2 5.2500 3.1012
3 3.1012 2.3567
4 2.3567 2.2392
5 2.2392 2.2361
6 2.2361 2.2361
The root is 2.2361


輸出畫面
Using Fixed Point Method / Iterative Method to find root of x^2-5=0
Enter value for x0
x0: -5

steps x0 x1
1 -5.0000 -3.0000
2 -3.0000 -2.3333
3 -2.3333 -2.2381
4 -2.2381 -2.2361
5 -2.2361 -2.2361

The root is -2.2361

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...