2019年1月16日 星期三

EX2-7 正割法求 f(x)= exp(x) + 1/pow(2,x) + 2*cos(x)-6 的根 , 誤差=0.00001

範例EX2-7  依據正割法求
 f(x)= exp(x) + 1/pow(2,x) + 2*cos(x)-6 的根 , 誤差=0.00001 
 x0=1.8 , x1=2.0



輸入資料
1.8   2.0
0.00001  50

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

float f(float x)
{
    return(exp(x) + 1/pow(2,x) + 2*cos(x)-6); // f(x)= exp(x) + 1/pow(2,x) + 2*cos(x)-6
}

float main()
{
    float a,b,c,d,e;
    int count=1,n;
    printf("\n\nEnter the values of a and b:\n"); //(a,b) must contain the solution.
    scanf("%f%f",&a,&b);
    printf("Enter the values of allowed error and maximun number of iterations:\n");
    scanf("%f %d",&e,&n);
    do
    {
        if(f(a)==f(b))
        {
            printf("\nSolution cannot be found as the values of a and b are same.\n");
        return;
        }
        c=(a*f(b)-b*f(a))/(f(b)-f(a));
        a=b;
        b=c;
        printf("Iteration No-%d    x=%f\n",count,c);
        count++;
        if(count==n)
        {
        break;
        }
    } while(fabs(f(c))>e);
    printf("\n The required solution is %f\n",c);

}


Enter the values of a and b:
Enter the values of allowed error and maximun number of iterations:
Iteration No-1    x=1.825441
Iteration No-2    x=1.828860
Iteration No-3    x=1.829385


 The required solution is 1.829385

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...