2019年4月12日 星期五

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

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


/*
 g1(x) = 10 / (x^3-1)
 g2(x) = (x + 10)^1/4  
 g3(x) = ((x+10)^1/2) / x

*/
#include<conio.h>
#include<stdio.h>
#include<math.h>
#define f(x) (10 / (x*x*x-1))
#define e 0.0001
#define MAX 30


int main(void)
{
    float x0,x1,f1 ;
    int i=1;
    clrscr();
    printf("Using Fixed Point Method / Iterative Method to find root of  x4-x-10 = 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();

}

輸出畫面
Using Fixed Point Method / Iterative Method to find root of  x4-x-10 = 0
Enter value for x0
x0: -5
steps x0 x1
1 -5.0000 -0.0794
2 -0.0794 -9.9950
3 -9.9950 -0.0100
4 -0.0100 -10.0000
5 -10.0000 -0.0100
6 -0.0100 -10.0000
7 -10.0000 -0.0100
8 -0.0100 -10.0000
9 -10.0000 -0.0100
10 -0.0100 -10.0000
11 -10.0000 -0.0100
12 -0.0100 -10.0000
13 -10.0000 -0.0100
14 -0.0100 -10.0000
15 -10.0000 -0.0100
16 -0.0100 -10.0000
17 -10.0000 -0.0100
18 -0.0100 -10.0000
19 -10.0000 -0.0100
20 -0.0100 -10.0000
21 -10.0000 -0.0100
22 -0.0100 -10.0000
23 -10.0000 -0.0100
24 -0.0100 -10.0000
25 -10.0000 -0.0100
26 -0.0100 -10.0000
27 -10.0000 -0.0100
28 -0.0100 -10.0000
29 -10.0000 -0.0100
30 -0.0100 -10.0000
無法收斂


/*
 g1(x) = 10 / (x^3-1)
 g2(x) = (x + 10)^1/4
 g3(x) = ((x+10)^1/2) / x

*/
#include<conio.h>
#include<stdio.h>
#include<math.h>
#define f(x) pow(x+10,0.25)
#define e 0.0001
#define MAX 30


Using Fixed Point Method / Iterative Method to find root of  x4-x-10 = 0
Enter value for x0
x0: -5
 steps x0 x1
1 -5.0000 1.4953
2 1.4953 1.8413
3 1.8413 1.8550
4 1.8550 1.8556
5 1.8556 1.8556
The root is 1.8556




另一個程式

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

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

double g(double x)
{
//return 10 / (x*x*x-1);  //change equation for each problem
//return  pow ( (x+10), 0.25);  //change equation for each problem
  return  (1/x)*pow((x+10), 0.5);  //change equation for each problem


/*
 g1(x) = 10 / (x^3-1)
 g2(x) = (x + 10)^1/4 
 g3(x) = ((x+10)^1/2) / x

*/
}

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");
        }

    }

}


-5
0.0001
20

Enter approximate p: Desired Tolerance: Maximum Iterations:

Iteration 1: Current value = -0.079365 , the root value = -9.920595
Iteration 2: Current value = -9.995003 , the root value = 9980.023728
Iteration 3: Current value = -0.010005 , the root value = -9.989995
Iteration 4: Current value = -9.999990 , the root value = 9999.959930
Iteration 5: Current value = -0.009990 , the root value = -9.990010
Iteration 6: Current value = -9.999990 , the root value = 9999.960110
Iteration 7: Current value = -0.009990 , the root value = -9.990010
Iteration 8: Current value = -9.999990 , the root value = 9999.960110
Iteration 9: Current value = -0.009990 , the root value = -9.990010
Iteration 10: Current value = -9.999990 , the root value = 9999.960110
Iteration 11: Current value = -0.009990 , the root value = -9.990010
Iteration 12: Current value = -9.999990 , the root value = 9999.960110
Iteration 13: Current value = -0.009990 , the root value = -9.990010
Iteration 14: Current value = -9.999990 , the root value = 9999.960110
Iteration 15: Current value = -0.009990 , the root value = -9.990010
Iteration 16: Current value = -9.999990 , the root value = 9999.960110
Iteration 17: Current value = -0.009990 , the root value = -9.990010
Iteration 18: Current value = -9.999990 , the root value = 9999.960110
Iteration 19: Current value = -0.009990 , the root value = -9.990010
Iteration 20: Current value = -9.999990 , the root value = 9999.960110
Method Failed after 20 iterations


-5
0.0001
20

Enter approximate p: Desired Tolerance: Maximum Iterations:

Iteration 1: Current value = 1.495349 , the root value = -6.495349
Iteration 2: Current value = 1.841325 , the root value = -0.345977
Iteration 3: Current value = 1.855026 , the root value = -0.013701
Iteration 4: Current value = 1.855563 , the root value = -0.000536



沒有留言:

張貼留言

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

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