2019年3月7日 星期四

例題1-1 若已知下面二點 請問 x=1.5時 則f(1.5)= ??

例題1-1 若已知下面二點
   x              y=f(x)
=================
  1.0            0.000
  2.0            0.693   
請問 x=1.5時 則f(1.5)= ??



/* ex1-4.c: Lagrange Interpolation Algorithm
 * Read in data file of ex1-4.dat which has n point values
 * and the value of interpolating point xa. Based on Lagrange
 * Interpolation algorithm to compute p(xa) and output its value.
 * (x[i],f[i]):given points and n+1 are number of points
 * Ln,k(x)=l=summation of (x-x[i])/(x[k]-x[i]).
 * p(x)=ff=L(x)*f(x[k])
 */
#include <stdio.h>
#include <conio.h>
int main()
{
   double x[30],f[30],l,ff,xa;
   int i,k,n;
   scanf("n=%d xa=%lf",&n,&xa);
    getch();
   for(k=0;k<=n;k++)
   {
      scanf("%lf %lf",&x[k],&f[k]);
      getch();
   }
   ff=0.0;
   for(k=0;k<=n;k++)
   {
      l=1.0;
      for(i=0;i<=n;i++)
      {
if(i !=k)
{
    l=l*(xa-x[i])/(x[k]-x[i]);
     getch();
}
      }
      ff=ff+l*f[k];
   }
   printf("The value of p(%.4lf)=%.4lf\n",xa,ff);
   return 0;
}


輸入資料
n=1 xa=1.5
1.0  0.0
2.0  0.693


輸出畫面
The value of p(1.5000)=0.3465

沒有留言:

張貼留言

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

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