當前位置:首頁 » 編程語言 » c語言雙線性插值

c語言雙線性插值

發布時間: 2022-08-10 21:25:41

① 用c語言編寫一個線性插值程序

#include<stdio.h>

doubleLerp(doublex0,doubley0,doublex1,doubley1,doublex)
{
doubledy=y1-y0;
if(dy==0){
printf("除0錯誤! ");
return0;
}
returnx*(x1-x0)/dy;
}
intmain()
{
doublex0,x1,y1,y0,x,y;
printf("Inptux0y0x1y1x:");
scanf("%lf%lf%lf%lf%lf",&x0,&y0,&x1,&y1,&x);
y=Lerp(x0,y0,x1,y1,x);
printf("y=%lf ",y);
return0;

}

② 用C語言編一個線性插值的小程序,很著急

#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
typedef struct data
{
float x;
float y;
}Data;//變數x和函數值y的結構
Data d[20];//最多二十組數據
float f(int s,int t)//牛頓插值法,用以返回插商
{
if(t==s+1)
return (d[t].y-d[s].y)/(d[t].x-d[s].x);
else
return (f(s+1,t)-f(s,t-1))/(d[t].x-d[s].x);
}
float Newton(float x,int count)
{
int n;
while(1)
{
cout<<"請輸入n值(即n次插值):";//獲得插值次數
cin>>n;
if(n<=count-1)// 插值次數不得大於count-1次
break;
else
system("cls");
}
//初始化t,y,yt。
float t=1.0;
float y=d[0].y;
float yt=0.0;
//計算y值
for(int j=1;j<=n;j++)
{
t=(x-d[j-1].x)*t;
yt=f(0,j)*t;
//cout<<f(0,j)<<endl;
y=y+yt;
}
return y;
}
float lagrange(float x,int count)
{
float y=0.0;
for(int k=0;k<count;k++)//這兒默認為count-1次插值
{
float p=1.0;//初始化p
for(int j=0;j<count;j++)
{//計算p的值
if(k==j)continue;//判斷是否為同一個數
p=p*(x-d[j].x)/(d[k].x-d[j].x);
}
y=y+p*d[k].y;//求和
}
return y;//返回y的值
}
void main()
{
float x,y;
int count;
while(1)
{
cout<<"請輸入x[i],y[i]的組數,不得超過20組:";//要求用戶輸入數據組數
cin>>count;
if(count<=20)
break;//檢查輸入的是否合法
system("cls");
}
//獲得各組數據
for(int i=0;i<count;i++)
{
cout<<"請輸入第"<<i+1<<"組x的值:";
cin>>d[i].x;
cout<<"請輸入第"<<i+1<<"組y的值:";
cin>>d[i].y;
system("cls");
}
cout<<"請輸入x的值:";//獲得變數x的值
cin>>x;
while(1)
{
int choice=3;
cout<<"請您選擇使用哪種插值法計算:"<<endl;
cout<<" (0):退出"<<endl;
cout<<" (1):Lagrange"<<endl;
cout<<" (2):Newton"<<endl;
cout<<"輸入你的選擇:";
cin>>choice;//取得用戶的選擇項
if(choice==2)
{
cout<<"你選擇了牛頓插值計算方法,其結果為:";
y=Newton(x,count);break;//調用相應的處理函數
}
if(choice==1)
{
cout<<"你選擇了拉格朗日插值計算方法,其結果為:";
y=lagrange(x,count);break;//調用相應的處理函數
}
if(choice==0)
break;
system("cls");
cout<<"輸入錯誤!!!!"<<endl;
}
cout<<x<<" , "<<y<<endl;//輸出最終結果

}

③ 求C語言實現圖像縮放的程序,最好是雙線性插值的

directX 3d 和opengl都可實現,對紋理采樣進行設置.
directx3d 9的
IDirect3DDevice9::SetSamplerState
這個方法可實現這個功能.

④ 求C語言編程 編程二次插值法求fx=(X-3)^2最優解初始區間[a,b]=[1,7]精度0.01

#includedouble f(double x)
{//多項式
double a[]={3,-7,-2,8};
int n=4;
n--;
double sum=a[n];
while(--n >= 0)
sum=sum*x+a[n];
return sum;
}

double cal(double a,double b,double e)
{
double a1,a2,a3,t_a2,t;

a1=a;a3=b;a2=(a+b)/2;
while(1)
{
t=((f(a3)-f(a2))/(a3-a2)-(f(a2)-f(a1))/(a2-a1))/(a3-a1);
t_a2=((a1+a2)*t-(f(a2)-f(a1))/(a2-a1))/(2*t);
if(t_a2-a2 < e && t_a2-a2 > -e)break;
if(t_a2 > a2)a1=a2;
else a3=a2;
a2=t_a2;
}
return f(t_a2);
}

void main()
{
printf("%lf\n",cal(0,2,0.01));
}

⑤ 求雙線性插值法的C語言程序!幫幫忙!拜託各位了!

ab
t
cd
就是兩次線性插值,先在x方向插出t上下方的_t1、_t2,然後再用它們插出t來
floattest(floatx,floaty)
{
float_t1,_t2,t;
_t1=a+(b-a)*(x-ax)/(bx-ax);
_t2=c+(d-c)*(x-cx)/(dx-cx);
t=_t1+(_t2-_t1)*(y-ay);
returnt;
}

⑥ 求用c語言編寫牛頓插值法

牛頓插值法:

#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=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;
}
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=1;k<=n;k++)
{
f[0]=y[k];
for(i=0;i<k;i++)
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-1;i>=0;i--)b=b*(varx-x[i])+y[i];
printf("Nn(%f)=%f",varx,b);
getchar();
}
留下個郵箱,我發給你:牛頓插值法的程序設計與應用

熱點內容
玩戰地5配置不行怎麼辦 發布:2024-03-29 22:10:28 瀏覽:981
javaice 發布:2024-03-29 21:56:37 瀏覽:355
編譯圖書 發布:2024-03-29 21:56:36 瀏覽:332
linux全選vi 發布:2024-03-29 21:55:11 瀏覽:774
艾譜保險箱初始密碼一般是什麼 發布:2024-03-29 21:48:11 瀏覽:825
商家粉腳本 發布:2024-03-29 21:34:57 瀏覽:151
我的世界ec伺服器怎麼獲得 發布:2024-03-29 21:21:44 瀏覽:709
小米4設置限制的訪問 發布:2024-03-29 21:21:10 瀏覽:406
linux向伺服器上傳文件 發布:2024-03-29 21:17:20 瀏覽:929
腳本健康cpu佔用率報警 發布:2024-03-29 21:16:42 瀏覽:255