Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <time.h>
  7. using namespace std;
  8. //u=(x-a)/h
  9. float u_cal(float u, int n)
  10. {
  11. float temp = u;
  12. for (int i = 1; i < n; i++)
  13. temp = temp * (u - i);
  14. return temp;
  15. }
  16. int fact(int n)
  17. {
  18. int f = 1;
  19. for (int i = 2; i <= n; i++)
  20. f *= i;
  21. return f;
  22. }
  23. void newt(float **a,float *x,int n,float value) {
  24.  
  25. for (int i = 1; i < n; i++) {
  26. for (int j = 0; j < n - i; j++)
  27. a[j][i] = a[j + 1][i - 1] - a[j][i - 1]; //
  28. }
  29. float sum = a[0][0];
  30. float u = (value - x[0]) / (x[1] - x[0]);
  31. for (int i = 1; i < n; i++) {
  32. sum = sum + (u_cal(u, i) * a[0][i]) /
  33. fact(i);
  34. }
  35. cout << "\n Value at " << value << " is "
  36. << sum << endl;
  37. }
  38.  
  39. int main()
  40. {
  41. srand(time(NULL));
  42.  
  43. int rowCount = 5, colCount = 5;
  44.  
  45. float *x = new float[rowCount];
  46. float** a = new float*[rowCount];
  47.  
  48. for (int i = 0; i < rowCount; ++i)
  49. a[i] = new float[colCount];
  50.  
  51. for (int i = 0,j=0; i < rowCount; ++i) {
  52. cout << " x" << i << ": ";
  53. cin >> x[i];
  54. cout << " y" << i << ": ";
  55. cin >> a[i][j];
  56. cout << endl;
  57.  
  58. }
  59. float value;
  60. cout << "interpolate to value: "; cin >> value;
  61.  
  62.  
  63. newt(a, x, 5,value);
  64.  
  65. for (int i = 0; i < rowCount; ++i) {
  66. delete[] a[i];
  67. }
  68. delete[] a;
  69.  
  70.  
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement