您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 汽车理论 > 数值分析_算法程序_汇总(部分)
//============================================================//====================Lagrange²åÖµ·¨=======================//============================================================#includestdio.h#defineN5doublex[]={0.4,0.55,0.65,0.80,0.95,1.05};doubley[]={0.41075,0.57815,0.69675,0.90,1.00,1.25382};doublep(doublexx){inti,k;doublepp=0,m1,m2;for(i=0;i=N;i++){m1=1;m2=1;for(k=0;k=N;k++)if(k!=i){m1*=xx-x[k];m2*=x[i]-x[k];}pp+=y[i]*m1/m2;}returnpp;}voidmain(void){printf(f(0.596)=%lf\n,p(0.596));printf(f(0.99)=%lf\n,p(0.99));}//--------------------------end-------------------------------//============================================================//======================·Ö¶ÎÏßÐÔ²åÖµ=========================//============================================================#includestdio.h#defineN5doublex[]={0.4,0.55,0.65,0.80,0.95,1.05};doubley[]={0.41075,0.57815,0.69675,0.90,1.00,1.25382};doubleline(doublexx){inti;doubleyxx=0;for(i=0;i=N;i++){if(xx=x[i])returny[i]*(xx-x[i+1])/(x[i]-x[i+1])+y[i+1]*(xx-x[i])/(x[i+1]-x[i]);}printf(Thevariablexisnotintheregion[x0,xn]);return0;}voidmain(void){printf(f(0.596)=%lf\n,line(0.596));printf(f(0.99)=%lf\n,line(0.99));}//-----------------------------end--------------------------------//================================================================//=======================Newton²åÖµ·¨==========================//================================================================#includestdio.h#defineN6doublex[]={1,2,3,4,5,6,7};doubley[]={0.368,0.135,0.050,0.018,0.007,0.002,0.001};doublejc(intk)/*´Ëº¯ÊýʵÏÖÇó¾ù²îf[x0,x1,...,xk]*/{inti,j;doubles=0,m;for(i=0;i=k;i++){for(m=1,j=0;j=k;j++)if(j!=i)m*=x[i]-x[j];s+=y[i]/m;}returns;}doublenewton(doublexxx)/*´Ëº¯ÊýʵÏÖÇóÅ£¶Ù²åÖµº¯ÊýNn(x)*/{doublent=y[0],m;inti,j;for(i=1;i=N;i++){for(m=1,j=0;j=i-1;j++)m*=xxx-x[j];nt+=jc(i)*m;}returnnt;}voidmain(void){doublex=1.8;printf(%lf\n,newton(x));}//-----------------------------end--------------------------------//================================================================//=======================Èý´ÎÑùÌõ²åÖµ===========================//================================================================data.txtÎļþµÄÄÚÈÝ:4012300.52.01.5//------------------------Ö÷³ÌÐò¿ªÊ¼-----------------------------#includestdio.h#includemath.h#includestdlib.h#defineN20//¾Ý½ÚµãµÄÊýÁ¿µ÷½ÚNµÄ´óСvoidline_equa(doublea[N][N],doublex[],intn);//×¢ÒâÊý×éµÄϱ꣬ËùʹÓõIJ¿·Öϱê´Ó1¿ªÊ¼!voidmain(void){doublex[N],y[N],a[N][N],m[N],t,f;intn;//Thenumberofpoints;inti,j;doublemu,lanbda,df0,dfn,hi_1,hi,t1,t2,d;FILE*file,*file2;//file:theinitialdata;//file2:theinterpilationdata;if((file=fopen(d:\\data.txt,r))==NULL){puts(Frialtoopenthefile\d:\\data.txt\!\n);return;}fscanf(file,%d,&n);for(i=1;i=n;i++)fscanf(file,%lf,&x[i]);for(i=1;i=n;i++)fscanf(file,%lf,&y[i]);fclose(file);//¶ÁÈëÊý¾ÝÍê±Ïfor(i=1;i=n;i++)//Êä³öx[i]printf(%8.4f,x[i]);puts();for(i=1;i=n;i++)//Êä³öy[i]printf(%8.4f,y[i]);puts();puts();for(i=1;i=n;i++)//³õʼ»¯ÏµÊý¾ØÕófor(j=1;j=n+1;j++)a[i][j]=0.0;//ÈýÍä¾ØËã·¨;//------------------------------------------------------------------------//µÚÒ»Öֱ߽çÌõ¼þ:ÒÑÖª¶Ëµã´¦µÄµ¼ÊýÖµdf0=0.2;dfn=-1;//±ß½çÌõ¼þÒò¾ßÌåÎÊÌâ¶ø¸Ä±äa[1][1]=a[n][n]=2.0;a[1][2]=a[n][n-1]=1.0;a[1][n+1]=(6.0/(x[2]-x[1]))*((y[2]-y[1])/(x[2]-x[1])-df0);a[n][n+1]=(6.0/(x[n]-x[n-1]))*(dfn-(y[n]-y[n-1])/(x[n]-x[n-1]));//-------------------------------------------------------------------------//-----Çó³öϵÊý¾ØÕó-----for(i=2;i=n-1;i++){a[i][i]=2.0;hi_1=x[i]-x[i-1];hi=x[i+1]-x[i];mu=hi_1/(hi_1+hi);lanbda=1.0-mu;t1=(y[i+1]-y[i])/hi;t2=(y[i]-y[i-1])/hi_1;d=6.0*(t1-t2)/(hi+hi_1);a[i][i-1]=mu;a[i][i]=2.0;a[i][i+1]=lanbda;a[i][n+1]=d;}//----Êä³öϵÊý¾ØÕó-----for(i=1;i=n;i++){for(j=1;j=n+1;j++)printf(%8.4lf,a[i][j]);puts();}//------½âÏßÐÔ·½³Ì×é,Çó³öÍä¾Ø-----line_equa(a,m,n);puts();for(i=1;i=n;i++)//Êä³ö½Úµã´¦µÄÍä¾Øprintf(%8.4lf,m[i]);puts();//------¼ÆËã²åÖµº¯ÊýÖµ,²¢Ð´ÈëÒ»Îļþif((file2=fopen(d:\\f.txt,a))==NULL)//´ò¿ª´æ·ÅÊýÖµµÄÎļþ{puts(Failtoopend:\\f.txt!);return;}i=1;for(t=x[1];t=x[n];t=t+0.01)//¼ÆËã²åÖµº¯ÊýÖµ{if(tx[i+1])i++;f=0.0;hi=x[i+1]-x[i];f+=m[i]*pow((x[i+1]-t),3.0)/(6.0*hi);f+=m[i+1]*pow((t-x[i]),3.0)/(6.0*hi);f+=(y[i]-m[i]*hi*hi/6.0)*(x[i+1]-t)/hi;f+=(y[i+1]-m[i+1]*hi*hi/6.0)*(t-x[i])/hi;fprintf(file2,%15.6E\t%15.6E\n,t,f);}fclose(file2);}//---------------------------------------------------------//------------------ÏûÔª·¨½â·½³Ì×éµÄº¯Êý-------------------//a[][]:ϵÊý¾ØÕóx[]:´æ·Å½â,n:άÊývoidline_equa(doublea[N][N],doublex[],intn){doubles,temp;inti,j,k,label;k=1;//½â·½³Ì×éµÄËã·¨¿ªÊ¼do{//----------------Ö÷ÔªËظß˹ÏûÔª·¨-------------------------label=0;temp=a[k][k];for(j=k+1;j=n;j++)//Ñ°ÕÒÖ÷ÔªËØif(a[j][k]temp){temp=a[j][k];label=j;}if(label!=0)//½»»»µÚkÐкÍÖ÷ÔªËØËùÔÚµÄÐÐ{for(j=k;j=n+1;j++){temp=a[k][j];a[k][j]=a[label][j];a[label][j]=temp;}}//------------------------------------------------for(j=k+1;j=n+1;j++)a[k][j]=a[k][j]/a[k][k];//ÏûÔªa[k][k]=1.0;i=1;for(i=k+1;i=n;i++){for(j=k+1;j=n+1;j++)a[i][j]=a[i][j]-a[i][k]*a[k][j];a[i][k]=0.0;}k+
本文标题:数值分析_算法程序_汇总(部分)
链接地址:https://www.777doc.com/doc-4312706 .html