Guest User

Untitled

a guest
May 3rd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. double func(double x_p, int n_s, double *x , double *y);
  5. double (*f_u) (double x_p, int n, double *x , double *y);
  6.  
  7. int main(void)
  8. {
  9. double *x,*y, x_p, rez;
  10. int n, i;
  11. f_u = &func;
  12. scanf("%d", &n);
  13.  
  14. x = (double*) malloc (sizeof(double)*n);
  15. y = (double*) malloc (sizeof(double)*n);
  16.  
  17. for(i=0; i<n; i++){
  18. scanf ("%lf %lf", &x[i], &y[i]);
  19. }
  20.  
  21. printf("x:"); scanf("%lf", &x_p);
  22.  
  23. rez = f_u(x_p, n, x , y);
  24.  
  25. printf ("%lf", rez);
  26. return 0;
  27. }
  28.  
  29. double func(double x_p, int n_s, double *x , double *y){
  30. double x1, x2, x3, y1, y2, y3, res, a0, a1, a2;
  31. int n, i;
  32.  
  33. n = 0;
  34.  
  35. while (x_p > x[n]) n++;
  36.  
  37. x1 = x[n-1];
  38. y1 = y[n-1];
  39. x2 = x[n];
  40. y2 = y[n];
  41.  
  42. if (n == 1){
  43. x3 = x[2];
  44. y3 = y[2];
  45. }
  46.  
  47. if (n == n_s-1){
  48. x3 = x[n-2];
  49. y3 = y[n-2];
  50. }
  51.  
  52. else{
  53. if((x_p-x[n-2]) > (x[n+1]-x_p)){
  54. x3 = x[n+1];
  55. y3 = y[n+1];
  56. }
  57. else{
  58. x3 = x[n-2];
  59. y3 = y[n-2];
  60. }
  61. }
  62. a2 = (y3-y1)/(x3-x1)/(x3-x2)-(y2-y1)/(x2-x1)/(x3-x2);
  63. a1 = (y2-y1)/(x2-x1)-a2*(x1+x2);
  64. a0 = y1-a1*x1-a2*x1*x1;
  65.  
  66. res=a0+a1*x_p+a2*x_p*x_p;
  67.  
  68. return res;
  69. }
Add Comment
Please, Sign In to add comment