Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- double func(double x_p, int n_s, double *x , double *y);
- double (*f_u) (double x_p, int n, double *x , double *y);
- int main(void)
- {
- double *x,*y, x_p, rez;
- int n, i;
- f_u = &func;
- scanf("%d", &n);
- x = (double*) malloc (sizeof(double)*n);
- y = (double*) malloc (sizeof(double)*n);
- for(i=0; i<n; i++){
- scanf ("%lf %lf", &x[i], &y[i]);
- }
- printf("x:"); scanf("%lf", &x_p);
- rez = f_u(x_p, n, x , y);
- printf ("%lf", rez);
- return 0;
- }
- double func(double x_p, int n_s, double *x , double *y){
- double x1, x2, x3, y1, y2, y3, res, a0, a1, a2;
- int n, i;
- n = 0;
- while (x_p > x[n]) n++;
- x1 = x[n-1];
- y1 = y[n-1];
- x2 = x[n];
- y2 = y[n];
- if (n == 1){
- x3 = x[2];
- y3 = y[2];
- }
- if (n == n_s-1){
- x3 = x[n-2];
- y3 = y[n-2];
- }
- else{
- if((x_p-x[n-2]) > (x[n+1]-x_p)){
- x3 = x[n+1];
- y3 = y[n+1];
- }
- else{
- x3 = x[n-2];
- y3 = y[n-2];
- }
- }
- a2 = (y3-y1)/(x3-x1)/(x3-x2)-(y2-y1)/(x2-x1)/(x3-x2);
- a1 = (y2-y1)/(x2-x1)-a2*(x1+x2);
- a0 = y1-a1*x1-a2*x1*x1;
- res=a0+a1*x_p+a2*x_p*x_p;
- return res;
- }
Add Comment
Please, Sign In to add comment