2019年3月16日 星期六

C語言 例題1-10 已知4點座標 , 請建立牛頓向前與向後差除表

C語言 例題1-10 已知4點座標如下

 i     xi     f(xi)
=============
0     1.0    0.0
1     2.0    0.693
2     3.0    1.099
3     4.0    1.386
=============
請建立牛頓向前與向後差除表

#include<stdio.h>
#include<math.h>
int main()

{
    float x[10],y[15][15];
    int n,i,j;
    //no. of items
    //printf("Enter n : ");
    scanf("n=%d",&n);
    //printf("X\tY\n");
    for(i = 0;i<n;i++){
            scanf("%f %f",&x[i],&y[i][0]);
    }
    //forward difference table
    for(j=1;j<n;j++)
        for(i=0;i<(n-j);i++)
            y[i][j] = y[i+1][j-1] - y[i][j-1];
    printf("\n***********Forward Difference Table ***********\n");
    printf(" ================================================\n");
    printf(" i\tx(i)\tf(i)\tf(i,i+1)  f(i,i+1.i+2),  ......\n");
    //display Forward Difference Table
    for(i=0;i<n;i++)
    {
        printf("%2d\t%.2f",i,x[i]);
        for(j=0;j<(n-i);j++)
            printf("\t%.3f",y[i][j]);
        printf("\n");
    }
    //backward difference table
    for(j=1;j<n;j++)
    //for j = 0 initially input is taken so we start from j=1
        for(i=n-1;i>(j-1);i--)
            y[i][j] = y[i][j-1] - y[i-1][j-1];
    printf("\n***********Backward Difference Table ***********\n");
    printf(" ================================================\n");
    printf(" i\tx(i)\tf(i)\tf(i,i+1)  f(i,i+1.i+2),  ......\n");

    //display Backward Difference Table
    for(i=0;i<n;i++)
    {
        printf("%2d\t%.2f",i,x[i]);
        for(j=0;j<=i;j++)
            printf("\t%.3f",y[i][j]);
        printf("\n");
    }
return 0;
}


輸入資料
n=4
1.0  0.0
2.0  0.693
3.0  1.099
4.0  1.386

輸出畫面
***********Forward Difference Table ***********
 ================================================
 i   x(i) f(i) f(i,i+1)     f(i,i+1.i+2),  ......
 0 1.00 0.000 0.693      -0.287 0.168
 1 2.00 0.693    0.406      -0.119
 2 3.00 1.099    0.287
 3 4.00 1.386

***********Backward Difference Table ***********
 ================================================
 i x(i) f(i)        f(i,i+1)   f(i,i+1.i+2),  ......
 0 1.00 0.000
 1   2.00 0.693 0.693
 2 3.00 1.099 0.406 -0.287
 3   4.00 1.386    0.287 -0.119 0.168

沒有留言:

張貼留言

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

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