用C语言编写一个线性插值程序

Python019

用C语言编写一个线性插值程序,第1张

#include <stdio.h>

double Lerp(double x0,double y0,double x1,double y1,double x)

{

    double dy = y1 - y0

    if(dy == 0){

        printf("除0错误!\n")

        return 0

    }

    return x * (x1 - x0) / dy

}

int main()

{

    double x0,x1,y1,y0,x,y

    printf("Inptu x0 y0 x1 y1 x:")

    scanf("%lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x)

    y = Lerp(x0,y0,x1,y1,x)

    printf("y = %lf\n",y)

    return 0

}

牛顿插值法:

#include<stdio.h>

#include<alloc.h>

float Language(float *x,float *y,float xx,int n)

{

int i,j

float *a,yy=0.0

a=(float *)malloc(n*sizeof(float))

for(i=0i<=n-1i++)

{

a[i]=y[i]

for(j=0j<=n-1j++)

if(j!=i)a[i]*=(xx-x[j])/(x[i]-x[j])

yy+=a[i]

}

free(a)

return yy

}

void main()

{

float x[4]={0.56160,0.5628,0.56401,0.56521}

float y[4]={0.82741,0.82659,0.82577,0.82495}

float xx=0.5635,yy

float Language(float *,float *,float,int)

yy=Language(x,y,xx,4)

printf("x=%f,y=%f\n",xx,yy)

getchar()

}

‍2.牛顿插值法#include<stdio.h>

#include<math.h>

#define N 4

void Difference(float *x,float *y,int n)

{

float *f

int k,i

f=(float *)malloc(n*sizeof(float))

for(k=1k<=nk++)

{

f[0]=y[k]

for(i=0i<ki++)

f[i+1]=(f[i]-y[i])/(x[k]-x[i])

y[k]=f[k]

}

return

}

main()

{

int i

float varx=0.895,b

float x[N+1]={0.4,0.55,0.65,0.8,0.9}

float y[N+1]={0.41075,0.57815,0.69675,0.88811,1.02652}

Difference(x,(float *)y,N)

b=y[N]

for(i=N-1i>=0i--)b=b*(varx-x[i])+y[i]

printf("Nn(%f)=%f",varx,b)

getchar()

}

留下个邮箱,我发给你:牛顿插值法的程序设计与应用

#include<stdio.h>

#include<string.h>

#define N 100

typedef struct tag{

double x

double y

}POINT

void main()

{

int i,j,n

double x,temp,Ln=0

POINT pt[N]

printf("请输入你要输入点的个数,,1<=n<=%d:\n",N)

printf("n=")

scanf("%d",&n)

printf("\n")

printf("\n请输入对应的点数\n")

for(i=0i<ni++)

scanf("%lf,%lf",&pt[i].x,&pt[i].y)

printf("\n")

printf("输入插值点x的值:\n")

scanf("%lf",&x)

printf("\n")

for(i=0i<ni++)

{

for(j=0,temp=1j<nj++)

{

if(j!=i)

temp=temp*(x-pt[j].x)/(pt[i].x-pt[j].x)

}

Ln=Ln+temp*pt[i].y

}

printf("输出:\nLn(%lf)=%lf\n",x,Ln)

}