• 数值计算实验三——拉格朗日插值和牛顿插值


    //拉格朗日插值
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=100;
    double x[maxn],y[maxn];
    double l[maxn];
    int main(){
    	x[0]=0.56160;x[1]=0.56280;
    	x[2]=0.56401;x[3]=0.56521;
    	y[0]=0.82741;y[1]=0.82659;
    	y[2]=0.82577;y[3]=0.82495;
    	double resx=0.56350,resy=0;
    	double cnt1=1,cnt2=1;
    	int n=3;
    	for(int i=0;i<=n;i++){
    		cnt1=1,cnt2=1;
    		for(int j=0;j<=n;j++){
    			if(i==j) continue;
    			cnt1=cnt1*(resx-x[j]);
    			cnt2=cnt2*(x[i]-x[j]);
    		}
    		l[i]=cnt1/cnt2;
    		resy=resy+y[i]*l[i];
    	}
    	cout<<resy<<endl;
    	return 0;
    }
    //牛顿插值
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=100;
    double x[maxn],y[maxn];
    double a[maxn][maxn];
    int main(){
    	x[0]=0.56160;x[1]=0.56280;
    	x[2]=0.56401;x[3]=0.56521;
    	y[0]=0.82741;y[1]=0.82659;
    	y[2]=0.82577;y[3]=0.82495;
    	double resx=0.56350,resy=0;
    	double cnt1=1,cnt2=1;
    	int n=4;
    	for(int j=0;j<=n-1;j++)///列
    		for(int i=j;i<=n-1;i++)
    			if(!j) a[i][j]=y[i];
    			else a[i][j]=(a[i][j-1]-a[i-1][j-1])/(x[i]-x[i-j]);	 
    	double tmp=1;
    	for(int i=0;i<=n;i++){
    		if(i) tmp=tmp*(resx-x[i]);
    		resy=resy+a[0][i]*tmp;
    	}
    	cout<<resy<<endl;
    	return 0;
    }
    
  • 相关阅读:
    矩阵十题(7)
    线段树成段更新 poj 3468 A Simple Problem with Integers
    线段树单点更新 hdu 2795 Billboard
    线段树成段更新 poj 2528 Mayor's posters
    矩阵十题(10)
    矩阵十题(8)
    矩阵十题(5)
    矩阵十题(6)
    矩阵十题(9)
    矩阵十题(4)
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853109.html
Copyright © 2020-2023  润新知