c3.cpp
上传用户:zhdd911129
上传日期:2007-05-11
资源大小:722k
文件大小:1k
源码类别:

matlab例程

开发平台:

Matlab

  1. //C3
  2. //Interpolation,using Newton Methom
  3. #include <iostream.h>
  4. const int N=5;
  5. //Divied Difference
  6. void fd(double *ax,double *ay,int n,double f[N][N])
  7. {
  8. int i,j;
  9. for (i=0;i<n;i++)
  10. f[i][0]=ay[i];
  11. for (i=1;i<n;i++)
  12. {
  13. for (j=1;j<=i;j++)
  14. {
  15. f[i][j]=(f[i][j-1]-f[i-1][j-1])/(ax[i]-ax[i-j]);
  16. }
  17. }
  18. }
  19.    
  20. //Newton polynomial
  21. double NP(double x,double f[N][N],double *ax,int n)
  22. {
  23. double sum=0,Tsum=1;
  24. int i,j;
  25. for (i=0;i<n;i++)
  26. {
  27. for (j=0;j<i;j++)
  28. {
  29. Tsum=Tsum*(x-ax[j]);
  30. }
  31. sum=sum+Tsum*f[i][i];
  32. Tsum=1;
  33. }
  34. return sum;
  35. }
  36. void main()
  37. {
  38. double ax[]={0.4,0.55,0.65,0.8,0.9},ay[]={0.41075,0.57815,0.69675,0.88811,1.02652},x1=0.596,x2=0.895;
  39. double func[N][N]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  40. int i;
  41. fd(ax,ay,N,func);
  42. cout<<"The value at x1 is: "<<NP(x1,func,ax,N)<<endl;
  43. cout<<"The value at x2 is: "<<NP(x2,func,ax,N)<<endl;
  44. cin>>i;
  45. }
  46. //F(x1)=0.631918,F(x2)=1.01937.Newton法运算时间与Lagrange基本一致
  47. //但可变更性远强于前者