2019年3月16日 星期六

C語言 習題1-1-d 根據下面的已知點撰寫Lagrange 內差法程式 並計算 xa=[150 ,250 ,350 ,450 ,550 , 650 ]之值

 C語言 習題1-1-d 根據下面的已知點撰寫Lagrange 內差法程式

   x                 f(x)
=======================
  100             1700
  200             1000  
  300             833
  400             800   
  500             820   
  600             867
=======================

x= [100, 200, 300 ,400, 500 ,600]
f(x)=[1700 , 1000 ,833 , 800 ,820 , 867]
並計算 xa=[150 ,250 ,350 ,450 ,550 , 650 ]之值


程式
// C program for implementation of Lagrange's Interpolation
#include <stdio.h>
#include <math.h>

// To represent a data point corresponding to x and y = f(x)
typedef struct f
{
double x , y;
} Data;

// function to interpolate the given data points using Lagrange's formula
// xi corresponds to the new data point whose value is to be obtained
// n represents the number of known data points
double interpolate(Data f[], double xi, int n)
{
double result = 0; // Initialize result

for (int i=0; i<n; i++)
{
// Compute individual terms of above formula
double term = f[i].y;
//printf("%0.3lf\n",term);
for (int j=0;j<n;j++)
{
if (j !=i )
{
term = term* (xi - f[j].x)/ (f[i].x - f[j].x);

//printf("%0.1lf %0.1lf \n",f[i].x , f[j].x);
//printf("%0.3lf\n",term);
}
}

// Add current term to result
result += term;
}

return result;
}

// driver function to check the program
int main()
{
// creating an array of 4 known data points

    Data f[]={ {100, 1700} , {200 , 1000} , {300 , 833} ,{400, 800},{500, 820},{600, 867} };
    double xb[]= {150 , 250 , 350 , 450 ,550 , 650};
// Using the interpolate function to obtain a data point
// corresponding to x=xa

int n1=6;
for (int k=0; k<n1; k++)
{
    double xa=xb[k];
    printf("%3.1lf----",xa);
    double result1= interpolate(f, xa, 6);    // 6 data
    double result2= xa + (160000/xa);
    printf("Value of Pn(%3.1lf) is : %3.1lf\n",xa,result1);
    printf("Value of f(%3.1lf) is : %3.1lf\n",xa, result2);
    printf("The e(x) = f(x)-Pn(x) is : %3.1lf\n\n", (result2-result1));
   
   
}
return 0;
}




輸出畫面
150.0----Value of Pn(150.0) is : 1238.8                                                                                                                          
Value of f(150.0) is : 1216.7                                                                                                                                    
The e(x) = f(x)-Pn(x) is : -22.2                                                                                                                                 
                                                                                                                                                                 
250.0----Value of Pn(250.0) is : 885.3                                                                                                                           
Value of f(250.0) is : 890.0                                                                                                                                     
The e(x) = f(x)-Pn(x) is : 4.7                                                                                                                                   
                                                                                                                                                                 
350.0----Value of Pn(350.0) is : 809.2                                                                                                                           
Value of f(350.0) is : 807.1                                                                                                                                     
The e(x) = f(x)-Pn(x) is : -2.0                                                                                                                                  
                                                                                                                                                                 
450.0----Value of Pn(450.0) is : 803.2                                                                                                                           
Value of f(450.0) is : 805.6                                                                                                                                     
The e(x) = f(x)-Pn(x) is : 2.3                                                                                                                                   
                                                                                                                                                                 
550.0----Value of Pn(550.0) is : 846.8                                                                                                                           
Value of f(550.0) is : 840.9                                                                                                                                     
The e(x) = f(x)-Pn(x) is : -5.9                                                                                                                                  
                                                                                                                                                                 
650.0----Value of Pn(650.0) is : 842.8                                                                                                                           
Value of f(650.0) is : 896.2                                                                                                                                     
The e(x) = f(x)-Pn(x) is : 53.3                                                                                                                                  
   
...Program finished with exit code 0                                                                                                                             
Press ENTER to exit console.       

沒有留言:

張貼留言

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

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