您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 拉格朗日插值方法C语言编程
#includestdio.h#includestdlib.h#defineM20;intn=0;intp=1;intnum=0;double*x;double*y;doubleCalculate(doublett);voidInsert(intm);voidPrint();voidNewTon(intm){doublett;Insert(m);Print();printf(是否继续进行插值、计算还是结束?继续插值请输入1,结束请输入0,求值计算请输入2;p=);scanf(%d,&p);printf(\n);while(p!=0){if(p==1){printf(请输入再次插值点个数num=);scanf(%d,&num);NewTon(num);}elseif(p==2){printf(请输入x=);scanf(%lf,&tt);tt=Calculate(tt);printf(Q(x)=%lf,tt);printf(\n);printf(是否继续进行插值、计算还是结束?继续插值请输入1,结束请输入0,求值计算请输入2;p=);scanf(%d,&p);printf(\n);}elsebreak;}}voidPrint(){intk,j;printf(插值多项式为:Q(x)=%lf,y[0]);for(j=1;jn;j++){if(y[j]=0)printf(+);printf(%lf,y[j]);for(k=0;kj;k++)printf(*(x-%lf),x[k]);}printf(\n);}voidInsert(intm){intj,k,t,N;N=n;n+=m;double*x1;double*y1;x1=(double*)malloc(n*sizeof(double));y1=(double*)malloc(n*sizeof(double));for(j=0;jN;j++){x1[j]=x[j];y1[j]=y[j];}for(j=0;jm;j++){printf(请输入第%d个插值点x[%d]=,j+1,N+j);scanf(%lf,&x1[N+j]);printf(请输入第%d个插值点y[%d]=,j+1,N+j);scanf(%lf,&y1[N+j]);}x=x1;y=y1;printf(\n);if(N1)for(j=0;jm;j++){doubless=1;for(k=0;kN+j;k++)ss*=x[N+j]-x[k];for(k=0;kN+j;k++){doublerr=y[k];for(t=0;tk;t++)rr*=x[N+j]-x[t];y[N+j]-=rr;}y[N+j]/=ss;}elsefor(j=1;jm;j++){doubless=1;for(k=0;kN+j;k++)ss*=x[N+j]-x[k];for(k=0;kN+j;k++){doublerr=y[k];for(t=0;tk;t++)rr*=x[N+j]-x[t];y[N+j]-=rr;}y[N+j]/=ss;}}doubleCalculate(doublett){inti,j;doubleyy=0;doublexx;for(i=0;in;i++){xx=y[i];for(j=0;ji;j++){xx=xx*(tt-x[j]);}yy+=xx;}returnyy;}voidmain(){printf(请输入插值点个数num=);scanf(%d,&num);NewTon(num);printf(结束);}
本文标题:拉格朗日插值方法C语言编程
链接地址:https://www.777doc.com/doc-3746819 .html