zoukankan      html  css  js  c++  java
  • 牛顿插值求解多项式

    1. #include <iostream>
    2. using namespace std;
    3. //牛顿插值求解多项式
    4. double * xs; //all input x
    5. double * ys; //all input y
    6. int n; //size
    7. //1 2 3 4
    8. //0 -5 -6 3
    9. //1.5
    10. //-2.625
    11. void init()
    12. {
    13. cout << "please input n " << endl;
    14. cin >> n;
    15. xs = new double[n];
    16. ys = new double[n];
    17. //input x
    18. cout << "please input the x's value !" << endl;
    19. for(int i=0; i<n; i++)
    20. {
    21. cin >> xs[i];
    22. }
    23. //input y
    24. cout << "please input the y's value !" << endl;
    25. for(int i=0; i<n; i++)
    26. {
    27. cin >> ys[i];
    28. }
    29. }
    30. double f(int n)
    31. {
    32. double f=0;
    33. double temp=0;
    34. for(int i=0; i<n+1; i++)
    35. {
    36. temp=ys[i];
    37. for(int j=0; j<n+1; j++)
    38. {
    39. if(i!=j)
    40. temp /= (xs[i]-xs[j]);
    41. }
    42. f += temp;
    43. }
    44. return f;
    45. }
    46. double newton(double x)
    47. {
    48. double result=0;
    49. for(int i=0; i<n; i++)
    50. {
    51. double temp=1;
    52. double fi=f(i);
    53. for(int j=0; j<i; j++)
    54. {
    55. temp = temp*(x-xs[j]);
    56. }
    57. result += fi*temp;
    58. }
    59. return result;
    60. }
    61. int main()
    62. {
    63. init();
    64. double x;
    65. cout << "please input the x' value !" << endl;
    66. cin >> x;
    67. cout << "the result of the Newton is " << newton(x);
    68. return 0;
    69. }





    附件列表

    • 相关阅读:
      C#构造函数
      C#析构函数
      C#常量
      C#属性
      checklistbox的用法
      2012快捷键
      查询ORACLE存储关联表
      UltraDropDown
      Linux常用命令大全(非常全!!!)
      infra 仪表盘效果
    • 原文地址:https://www.cnblogs.com/sober-reflection/p/be34a2cebf0db8a639b281de79682a1e.html
    Copyright © 2011-2022 走看看