c语言构造插值多项式,拉格朗日多项式插值(C语言).docx
拉格朗日多項式插值(C語言)
#include #include #include float lagrange(float *x,float *y,float xx,int n)/*拉¤-格?朗¤¨o日¨?插?值|ì算?法¤?§*/ { int i,j; float *a,yy=0.0;/*a作á??為a臨¢¨′時o?à變à?量¢?,ê?記?錄?拉¤-格?朗¤¨o日¨?插?值|ì多¨¤項?式o?*/ a=(float *)malloc(n*sizeof(float)); for(i=0;i<=n-1;i++) { a[i]=y[i]; for(j=0;j<=n-1;j++) if(j!=i) a[i]*=(xx-x[j])/(x[i]-x[j]); yy+=a[i]; } free(a); return yy;}int main(){ int i; int n; float x[20],y[20],xx,yy; printf("Input n:"); scanf("%d",&n); if(n>=20) { printf("Error!The value of n must in (0,20)."); getch();return 1; } if(n<=0) { printf("Error! The value of n must in (0,20)."); getch(); return 1; } for(i=0;i<=n-1;i++) { printf("x[%d]:",i); scanf("%f",&x[i]); } printf("\n"); for(i=0;i<=n-1;i++) { printf("y[%d]:",i);scanf("%f",&y[i]); } printf("\n"); printf("Input xx:"); scanf("%f",&xx); yy=lagrange(x,y,xx,n); printf("x=%f,y=%f\n",xx,yy); getch(); } 牛頓(Newton)插值公式#include #include#include"math.h"#define N 4using namespace std;void main(){void lin(double x[],double y[],double t,int n); void newton(double a[],double b[],double t,double h,int n);double t,h,d; int i,j,n,k;double x[N]={0.4,0.55,0.65,0.80};double y[N]={0.41075,0.57815,0.69675,0.88811};double a[N],b[N];h=0.1;//h為a等쨨距¨¤節¨2點ì?寬¨a度¨¨,ê?t為a插?值|ì點ì? t=0.596; if(!h) cout<x[N-1]) cout<x[i]){ w=y[i]*(t-x[i+1])/(x[i]-x[i+1])+y[i+1]*(t-x[i])/(x[i+1]-x[i]); c=fabs(cos(x[i])) > fabs(cos(x[i+1])) ? fabs(cos(x[i])) : fabs(cos(x[i+1])); e=c*(t-x[i])*(t-x[i+1]); cout<
總結
以上是生活随笔為你收集整理的c语言构造插值多项式,拉格朗日多项式插值(C语言).docx的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言数组算n阶乘,c语言中如何计算n的
- 下一篇: 线性表C语言locate和ETget,线