2019年4月13日 星期六

C語言例題2-9 利用定點迴路法Fixed point method 求 x^4- 3 x^2 - 3 = 0

C語言例題2-9 利用定點迴路法Fixed point method 求 x^4-3x^2 - 3 = 0

x^4= 3x^2+3
x = pow (  (3x^2+3) , (1/4))


f(x)=x , g(x)= pow (  (3x^2+3) , (1/4))


程式
#include <stdio.h>
#include <math.h>

double f(double x)
{
return x*x*x*x-3*x*x-3;  //change equation for each problem
}

double g(double x)
{
return pow((3*x*x+3),.25);  //change equation for each problem
}

int main()
{
    double p, p0, Tol;
    int i=1;
    int No;

    printf("Enter approximate p: ");
    scanf ("%lf", &p0);

    printf("Desired Tolerance: ");
    scanf ("%lf", &Tol);

    printf("Maximum Iterations: ");
    scanf ("%d", &No);

    printf("\n\n");
   
    while (i<=No)
    {
        p = g(p0);

        if((fabs(p-p0))<Tol)
        {
            //printf("%lf", &p);
            break;
        }
        printf("Iteration %d: Current value = %lf , the root value = %lf\n", i, p , f(p));

        i++;  //i=i+1
        p0=p;

        if (i>No)
        {
        printf("Method Failed after %d", No);
        printf(" iterations");
        }

    }

}

STDIN輸入資料
1
0.0001
20


輸出畫面
Enter approximate p: Desired Tolerance: Maximum Iterations:

Iteration 1: Current value = 1.565085 , the root value = -4.348469
Iteration 2: Current value = 1.793573 , the root value = -2.302242
Iteration 3: Current value = 1.885944 , the root value = -1.019640
Iteration 4: Current value = 1.922848 , the root value = -0.421680
Iteration 5: Current value = 1.937508 , the root value = -0.169775
Iteration 6: Current value = 1.943317 , the root value = -0.067636
Iteration 7: Current value = 1.945617 , the root value = -0.026833
Iteration 8: Current value = 1.946527 , the root value = -0.010628
Iteration 9: Current value = 1.946887 , the root value = -0.004207
Iteration 10: Current value = 1.947030 , the root value = -0.001665

沒有留言:

張貼留言

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

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