今天编程学习网为大家讲解c语言编程:拉格朗日插值法,有需要的小伙伴可以参考一下:
题目
拉格朗日插值法流程图
(1)二次插值:
#include<stdio.h> float lagelangri(float x[],float y[],float xx,int n) { int i,j; float *a,yy=0; a=new float[n]; 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]; } delete a; return yy; } void main() { float x[5]={-3.0,-1.0,1.0,2.0,3.0}; float y[5]={1.0,1.5,2.0,2.0,1.0}; float xx1=-2,xx2=0,xx3=2.75,yy1,yy2,yy3; yy1=lagelangri(x,y,xx1,3); yy2=lagelangri(x,y,xx2,3); yy3=lagelangri(x,y,xx3,3); printf("x1=%-20f,y1=%f\n",xx1,yy1); printf("x2=%-20f,y2=%f\n",xx2,yy2); printf("x3=%-20f,y3=%f\n",xx3,yy3); }(2)五次插值:
#include<stdio.h> float lagelangri(float x[],float y[],float xx,int n) { int i,j; float *a,yy=0; a=new float[n]; 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]; } delete a; return yy; } void main() { float x[6]={0.30,0.42,0.50,0.58,0.66,0.72}; float y[6]={1.04403,1.08462,1.11803,1.15603,1.19817,1.23223}; float xx1=0.46,xx2=0.55,xx3=0.60,yy1,yy2,yy3; yy1=lagelangri(x,y,xx1,6); yy2=lagelangri(x,y,xx2,6); yy3=lagelangri(x,y,xx3,6); printf("x1=%-20f,y1=%f\n",xx1,yy1); printf("x2=%-20f,y2=%f\n",xx2,yy2); printf("x3=%-20f,y3=%f\n",xx3,yy3); }以上就是“c语言编程:拉格朗日插值法!”的详细内容,想要了解更多C语言教程欢迎持续关注编程学习网
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/10327/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取