#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void Lagrange(int n, int m, double x[], double y[], double xs[], double ys[]);
int main(){
int n,m;
scanf("%d %d", &n, &m);
double *x = (double *)malloc(n * sizeof(double));
double *y = (double *)malloc(n * sizeof(double));
double *xs = (double *)malloc(m * sizeof(double));
double *ys = (double *)malloc(m * sizeof(double));
for(int i = 0; i < n; i++){
scanf("%lf%lf", &x[i], &y[i]);
}
for(int i = 0; i < m; i++){
scanf("%lf", &xs[i]);
}
Lagrange(n, m, x, y, xs, ys);
for (int u=0; u < m; u++) {
printf("%lf %lf", xs[u], ys[u]);
printf("\n");
}
free(x);
free(y);
free(xs);
free(ys);
}
void Lagrange(int n, int m, double x[], double y[], double xs[], double ys[]){
for(int k = 0; k < m; k++){
for(int i = 0; i < n; i++){ // 这里是算第 i 个基函数的值
double num = 1;
double fac = 1;
for (int j = 0; j < n; j++) {
if (i != j) {
num *= (xs[k] - x[j]);
fac *= (x[i] - x[j]);
}
}
ys[k] += y[i] * num / fac;
}
}
}
Lagrange插值法
最新推荐文章于 2024-03-18 14:00:28 发布