xianxing.c
上传用户:cbxwin
上传日期:2008-06-12
资源大小:1k
文件大小:1k
开发平台:

C/C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. double LIP(int n,double t,double *x,double *y)
  5. {
  6.     int i,j,k;
  7.     double u,f;
  8.     for(i=0;i<=n-2;i++)
  9.     {
  10.         if(t<=x[i+1])
  11.         {
  12.             k=i;
  13.             break;
  14.         }
  15.         else
  16.             k=n-2;
  17.     }
  18.     u=(t-x[k])/(x[k+1]-x[k]);
  19.     f=y[k]+u*(y[k+1]-y[k]);
  20.     return (f);
  21. }
  22. double LIP(int ,double,double*,double*);
  23. main()
  24. {
  25.     unsigned i,n;
  26.     double *x,*y,t,f;
  27.     n=6;
  28.     t=75.5;
  29.     x=(double*)calloc(n,sizeof(double));
  30.     if(x==NULL)
  31.         exit(1);
  32.     y=(double*)calloc(n,sizeof(double));
  33.     if(y==NULL)
  34.         exit(1);
  35.     x[0]=75;
  36.     x[1]=76;
  37.     x[2]=77;
  38.     x[3]=78;
  39.     x[4]=79;
  40.     x[5]=80;
  41.     y[0]=2.768;
  42.     y[1]=2.833;
  43.     y[2]=2.903;
  44.     y[3]=2.979;
  45.     y[4]=3.062;
  46.     y[5]=3.153;
  47.     printf("使用线性插值算法:n");
  48.     printf("当x=75.7时,结果为 %f n",LIP(n,t,x,y));
  49.     t=78.3;
  50.     printf("当x=78.3时,结果为 %f n",LIP(n,t,x,y));
  51.     free(x);
  52.     free(y);
  53. }