数模论坛

 找回密码
 注-册-帐-号
搜索
热搜: 活动 交友 discuz
楼主: totoerer

C语言解题经典程序(2)

[复制链接]
 楼主| 发表于 2004-5-15 07:11:13 | 显示全部楼层
< center" align=center>/*列主元消去法解线性方程组  GAUSS.C  */<p></p></P>< 22pt; mso-line-height-rule: exactly">#include&lt;math.h&gt;<p></p></P>< 22pt; mso-line-height-rule: exactly">#include&lt;stdio.h&gt;<p></p></P><P 22pt; mso-line-height-rule: exactly"> <p></p></P><P 22pt; mso-line-height-rule: exactly">int gauss(double **a,int n)<p></p></P><P 22pt; mso-line-height-rule: exactly">  {<p></p></P><P 22pt; mso-line-height-rule: exactly">  int i,j,k,l;  double c,t,eps=1.0e-15;<p></p></P><P 22pt; mso-line-height-rule: exactly">  for(k=0;k&lt;n;k++)<p></p></P><P 22pt; mso-line-height-rule: exactly">    {<p></p></P><P 22pt; mso-line-height-rule: exactly">    c=0.0;<p></p></P><P 22pt; mso-line-height-rule: exactly">    for(i=k;i&lt;n;i++)<p></p></P><P 22pt; mso-line-height-rule: exactly">      {<p></p></P><P 22pt; mso-line-height-rule: exactly">      t=fabs(a[k]);<p></p></P><P 22pt; mso-line-height-rule: exactly">      if(t&gt;c)<p></p></P><P 22pt; mso-line-height-rule: exactly">       {   c=t;l=i;  }<p></p></P><P 22pt; mso-line-height-rule: exactly">      }<p></p></P><P 22pt; mso-line-height-rule: exactly">    if(c&lt;=eps)return(0);<p></p></P><P 22pt; mso-line-height-rule: exactly">    if(l!=k)<p></p></P><P 22pt; mso-line-height-rule: exactly">      for(j=k;j&lt;=n;j++)<p></p></P><P 22pt; mso-line-height-rule: exactly">       {<p></p></P><P 22pt; mso-line-height-rule: exactly">       t=a[k][j];  a[k][j]=a[l][j];   a[l][j]=t;<p></p></P><P 22pt; mso-line-height-rule: exactly">       }<p></p></P><P 22pt; mso-line-height-rule: exactly">    c=1.0/a[k][k];<p></p></P><P 22pt; mso-line-height-rule: exactly">    for(j=k+1;j&lt;=n;j++)a[k][j]*=c;<p></p></P><P 22pt; mso-line-height-rule: exactly">    for(i=k+1;i&lt;n;i++)<p></p></P><P 22pt; mso-line-height-rule: exactly">      for(j=k+1;j&lt;=n;j++)<p></p></P><P 22pt; mso-line-height-rule: exactly">        a[j]-=(a[k]*a[k][j]);<p></p></P><P 22pt; mso-line-height-rule: exactly">    }<p></p></P><P 22pt; mso-line-height-rule: exactly">  for(i=n-1;i&gt;=0;i--)<p></p></P><P 22pt; mso-line-height-rule: exactly">    for(j=i+1;j&lt;n;j++)<p></p></P><P 22pt; mso-line-height-rule: exactly">      a[n]-=(a[j]*a[j][n]);<p></p></P><P 22pt; mso-line-height-rule: exactly">  return(1);<p></p></P><P 14pt; LINE-HEIGHT: 22pt; mso-char-indent-count: 1.0; mso-char-indent-size: 14.0pt; mso-line-height-rule: exactly">}<p></p></P><P 22pt; mso-line-height-rule: exactly"> <p></p></P><P 22pt; mso-line-height-rule: exactly">main()<p></p></P><P 22pt; mso-line-height-rule: exactly">  {<p></p></P><P 22pt; mso-line-height-rule: exactly">  int i,j,n=3;<p></p></P><P 22pt; mso-line-height-rule: exactly">  double *pa[3];<p></p></P><P 22pt; mso-line-height-rule: exactly">  double a[3][4]={12,-3,3,15,-18,3,-1,-15,1,1,1,6};<p></p></P><P 22pt; mso-line-height-rule: exactly">  for(i=0;i&lt;n;i++)pa=a;<p></p></P><P 22pt; mso-line-height-rule: exactly">  if(gauss(pa,n))<p></p></P><P 22pt; mso-line-height-rule: exactly">    {<p></p></P><P 22pt; mso-line-height-rule: exactly">    for(i=0;i&lt;n;i++)<p></p></P><P 22pt; mso-line-height-rule: exactly">      printf("\nx%d = %15.14lf",i,a[n]);<p></p></P><P 22pt; mso-line-height-rule: exactly">    }<p></p></P><P 22pt; mso-line-height-rule: exactly">  else printf("\nFiled !");<p></p></P><P 22pt; mso-line-height-rule: exactly">  }<p></p></P>
 楼主| 发表于 2004-5-15 07:13:14 | 显示全部楼层
< center" align=center><B>龙贝格求积公式计算定积分 Lomberg1.C<p></p></B></P>< 24pt; mso-line-height-rule: exactly">#include&lt;math.h&gt;<p></p></P>< 24pt; mso-line-height-rule: exactly">#include&lt;stdio.h&gt;<p></p></P><P 24pt; mso-line-height-rule: exactly">double f(double x)<p></p></P><P 24pt; mso-line-height-rule: exactly">  {  return(4.0/(1+x*x));  }<p></p></P><P 24pt; mso-line-height-rule: exactly">double lomberg(double a,double b,double eps)<p></p></P><P 24pt; mso-line-height-rule: exactly">  {<p></p></P><P 24pt; mso-line-height-rule: exactly">  int k=1;<p></p></P><P 24pt; mso-line-height-rule: exactly">  double h1,h2,t1,t2,s1,s2,c1,c2,r1,r2,sam;<p></p></P><P 24pt; mso-line-height-rule: exactly">  double rs,rc,rr,x;<p></p></P><P 24pt; mso-line-height-rule: exactly">  rs=1.0/3.0;rc=1.0/15.0;rr=1.0/63.0;<p></p></P><P 24pt; mso-line-height-rule: exactly">  h1=b-a; h2=h1*0.5;  t1=(f(a)+f(b))*h2;<p></p></P><P 24pt; mso-line-height-rule: exactly">  t2=t1*0.5+f(a+h2)*h2;  s1=(t2*4.0-t1)*rs;<p></p></P><P 24pt; mso-line-height-rule: exactly">  h1=h2;h2*=0.5;t1=t2;x=a+h2;<p></p></P><P 24pt; mso-line-height-rule: exactly">  t2=t1*0.5+(f(x)+f(x+h1))*h2;<p></p></P><P 24pt; mso-line-height-rule: exactly">  s2=(t2*4.0-t1)*rs;  c1=(s2*16.0-s1)*rc;<p></p></P><P 24pt; mso-line-height-rule: exactly">  h1=h2;h2*=0.5;t1=t2;s1=s2;x=a+h2;  sam=0.0;<p></p></P><P 24pt; mso-line-height-rule: exactly">  for(x=a+h2;x&lt;b;x+=h1)sam+=f(x);<p></p></P><P 24pt; mso-line-height-rule: exactly">  t2=t1*0.5+sam*h2;  s2=(t2*4.0-t1)*rs;<p></p></P><P 24pt; mso-line-height-rule: exactly">  c2=(s2*16.0-s1)*rc;  r1=(c2*64.0-c1)*rr;<p></p></P><P 24pt; mso-line-height-rule: exactly">  while(k&lt;=20)<p></p></P><P 24pt; mso-line-height-rule: exactly">    {<p></p></P><P 24pt; mso-line-height-rule: exactly">    h1=h2;h2*=0.5;t1=t2;s1=s2;c1=c2;x=a+h2;<p></p></P><P 24pt; mso-line-height-rule: exactly">    sam=0.0;<p></p></P><P 24pt; mso-line-height-rule: exactly">    for(x=a+h2;x&lt;b;x+=h1)sam+=f(x);<p></p></P><P 24pt; mso-line-height-rule: exactly">    t2=t1*0.5+sam*h2;    s2=(t2*4.0-t1)*rs;<p></p></P><P 24pt; mso-line-height-rule: exactly">    c2=(s2*16.0-s1)*rc;    r2=(c2*64.0-c1)*rr;<p></p></P><P 24pt; mso-line-height-rule: exactly">    if(fabs(r2-r1)&lt;eps)break;<p></p></P><P 24pt; mso-line-height-rule: exactly">    r1=r2;k++;<p></p></P><P 24pt; mso-line-height-rule: exactly">    }<p></p></P><P 24pt; mso-line-height-rule: exactly">  printf("\nk=%d",k);<p></p></P><P 24pt; mso-line-height-rule: exactly">  return(r2);<p></p></P><P 24pt; mso-line-height-rule: exactly">  }<p></p></P><P 24pt; mso-line-height-rule: exactly">main()<p></p></P><P 24pt; mso-line-height-rule: exactly">  {<p></p></P><P 24pt; mso-line-height-rule: exactly">  double eps=1.0e-12,answer;<p></p></P><P 24pt; mso-line-height-rule: exactly">  answer=lomberg(0.0,1.0,eps);<p></p></P><P 24pt; mso-line-height-rule: exactly">  printf("\nThe Answer is %15.14lf ",answer);<p></p></P><P 24pt; mso-line-height-rule: exactly">  }<p></p></P><P 24pt; mso-line-height-rule: exactly">循环3次    计算结果:   3.14159265358979<p></p></P>
 楼主| 发表于 2004-5-15 07:13:40 | 显示全部楼层
< center" align=center> 步长自适应龙格—库塔法解微分方程  RUNGKUTA.C<p></p></P>< 22pt; mso-line-height-rule: exactly">#include&lt;math.h&gt;<p></p></P>< 22pt; mso-line-height-rule: exactly">#include&lt;stdio.h&gt;<p></p></P><P 22pt; mso-line-height-rule: exactly"> <p></p></P><P 22pt; mso-line-height-rule: exactly">double f(double x,double y)<p></p></P><P 22pt; mso-line-height-rule: exactly">{ return(x*exp(-y));}<p></p></P><P 22pt; mso-line-height-rule: exactly"> <p></p></P><P 22pt; mso-line-height-rule: exactly">double rungkuta(double x0,double y0,double h)<p></p></P><P 22pt; mso-line-height-rule: exactly">{<p></p></P><P 22pt; mso-line-height-rule: exactly">double h12,h16,x,y,K1,K2,K3,K4;<p></p></P><P 22pt; mso-line-height-rule: exactly">h12=0.5*h;h16=h/6.0;<p></p></P><P 22pt; mso-line-height-rule: exactly">    x=x0;y=y0;K1=f(x,y);<p></p></P><P 22pt; mso-line-height-rule: exactly">    x+=h12;y=y0+h12*K1;K2=f(x,y);<p></p></P><P 22pt; mso-line-height-rule: exactly">    y=y0+h12*K2;K3=f(x,y);<p></p></P><P 22pt; mso-line-height-rule: exactly">    x+=h12;y=y0+h*K3;K4=f(x,y);<p></p></P><P 22pt; mso-line-height-rule: exactly">    y=y0+(K1+2.0*K2+2.0*K3+K4)*h16;<p></p></P><P 22pt; mso-line-height-rule: exactly">return(y);<p></p></P><P 22pt; mso-line-height-rule: exactly">}<p></p></P><P 22pt; mso-line-height-rule: exactly"> <p></p></P><P 22pt; mso-line-height-rule: exactly">double get_step(double x0,double y0,double *h,double eps)<p></p></P><P 22pt; mso-line-height-rule: exactly">{<p></p></P><P 22pt; mso-line-height-rule: exactly">int k=0;  double h1,y,y1,y2;<p></p></P><P 22pt; mso-line-height-rule: exactly">h1=*h; y=rungkuta(x0,y0,h1);<p></p></P><P 22pt; mso-line-height-rule: exactly">while(k&lt;32)<p></p></P><P 22pt; mso-line-height-rule: exactly">  {<p></p></P><P 22pt; mso-line-height-rule: exactly">  h1*=0.5;<p></p></P><P 22pt; mso-line-height-rule: exactly">  y1=rungkuta(x0,y0,h1);<p></p></P><P 22pt; mso-line-height-rule: exactly">  y2=rungkuta(x0+h1,y1,h1);<p></p></P><P 22pt; mso-line-height-rule: exactly">  if(fabs(y2-y)&lt;eps)break;<p></p></P><P 22pt; mso-line-height-rule: exactly">  y=y1;k++;<p></p></P><P 22pt; mso-line-height-rule: exactly">  }<p></p></P><P 22pt; mso-line-height-rule: exactly">if(k==0)return(y2);<p></p></P><P 22pt; mso-line-height-rule: exactly">*h=h1;return(y1);<p></p></P><P 22pt; mso-line-height-rule: exactly">}<p></p></P><P 22pt; mso-line-height-rule: exactly">double r_k(double x0,double y0,double xn,double eps)<p></p></P><P 22pt; mso-line-height-rule: exactly">{<p></p></P><P 22pt; mso-line-height-rule: exactly">double h,x,y;<p></p></P><P 22pt; mso-line-height-rule: exactly">h=(xn-x0)/256.0; x=x0;y=y0;<p></p></P><P 22pt; mso-line-height-rule: exactly">for(;;)<p></p></P><P 22pt; mso-line-height-rule: exactly">  {<p></p></P><P 22pt; mso-line-height-rule: exactly">  y=get_step(x,y,&amp;h,eps);  x+=h;<p></p></P><P 22pt; mso-line-height-rule: exactly">  if(x&gt;=xn)break;<p></p></P><P 22pt; mso-line-height-rule: exactly">  }<p></p></P><P 22pt; mso-line-height-rule: exactly">return(y);<p></p></P><P 22pt; mso-line-height-rule: exactly">}<p></p></P><P 22pt; mso-line-height-rule: exactly"> <p></p></P><P 22pt; mso-line-height-rule: exactly">main()<p></p></P><P 22pt; mso-line-height-rule: exactly">{<p></p></P><P 22pt; mso-line-height-rule: exactly">double answer,eps=1.0e-15;<p></p></P><P 22pt; mso-line-height-rule: exactly">printf("\nThe Answer is %16.15lf",log(13.5));<p></p></P><P 22pt; mso-line-height-rule: exactly">answer=r_k(0.0,0.0,5.0,eps);<p></p></P><P 22pt; mso-line-height-rule: exactly">printf("\nThe Answer is %16.15lf",answer);<p></p></P><P 22pt; mso-line-height-rule: exactly">}<p></p></P><P 23pt; mso-line-height-rule: exactly">计算结果:<v:shapetype> <v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path gradientshapeok="t" connecttype="rect" extrusionok="f"></v:path><lock v:ext="edit" aspectratio="t"></lock></v:shapetype><v:shape><v:imagedata></v:imagedata></v:shape><p></p></P><P 23pt; mso-line-height-rule: exactly">精确结果为:<v:shape> <v:imagedata></v:imagedata></v:shape><p></p></P>
 楼主| 发表于 2004-5-15 07:14:18 | 显示全部楼层
< center" align=center><B>幂法求矩阵的主特征值tezheng.c<p></p></B></P>< 25pt; mso-line-height-rule: exactly">#include&lt;stdio.h&gt;<p></p></P>< 25pt; mso-line-height-rule: exactly">#include&lt;math.h&gt;<p></p></P><P 25pt; mso-line-height-rule: exactly"> <p></p></P><P 25pt; mso-line-height-rule: exactly">double  mifa(double *a[],double u0[],int n,double eps)<p></p></P><P 25pt; mso-line-height-rule: exactly">  {<p></p></P><P 25pt; mso-line-height-rule: exactly">  int k=1,i,j;<p></p></P><P 25pt; mso-line-height-rule: exactly">  double t0,t1,u1[20];<p></p></P><P 25pt; mso-line-height-rule: exactly">  t0=u0[0];<p></p></P><P 25pt; mso-line-height-rule: exactly">  for(i=1;i&lt;n;i++)  <p></p></P><P 32pt; LINE-HEIGHT: 25pt; mso-char-indent-count: 2.0; mso-char-indent-size: 16.0pt; mso-line-height-rule: exactly">if(fabs(u0)&gt;fabs(t0))t0=u0;<p></p></P><P 25pt; mso-line-height-rule: exactly">  while(k&lt;=200)<p></p></P><P 25pt; mso-line-height-rule: exactly">    {<p></p></P><P 25pt; mso-line-height-rule: exactly">    for(i=0;i&lt;n;i++)<p></p></P><P 25pt; mso-line-height-rule: exactly">      {<p></p></P><P 25pt; mso-line-height-rule: exactly">      u1=0.0;<p></p></P><P 25pt; mso-line-height-rule: exactly">      for(j=0;j&lt;n;j++)u1+=a[j]*u0[j];<p></p></P><P 25pt; mso-line-height-rule: exactly">      }<p></p></P><P 25pt; mso-line-height-rule: exactly">    t1=u1[0];<p></p></P><P 31.2pt; LINE-HEIGHT: 25pt; mso-line-height-rule: exactly">for(i=1;i&lt;n;i++)<p></p></P><P 46.7pt; LINE-HEIGHT: 25pt; mso-char-indent-count: 2.92; mso-char-indent-size: 15.95pt; mso-line-height-rule: exactly">if(fabs(u1)&gt;fabs(t1))  t1=u1;<p></p></P><P 25pt; mso-line-height-rule: exactly">    for(i=0;i&lt;n;i++)  u1/=t1;<p></p></P><P 25pt; mso-line-height-rule: exactly">    for(i=0;i&lt;n;i++)u0=u1;<p></p></P><P 25pt; mso-line-height-rule: exactly">    if(fabs(t1-t0)&lt;eps)break;<p></p></P><P 25pt; mso-line-height-rule: exactly">    k++;t0=t1;<p></p></P><P 25pt; mso-line-height-rule: exactly">    }<p></p></P><P 25pt; mso-line-height-rule: exactly">  return(t1);<p></p></P><P 25pt; mso-line-height-rule: exactly">  }<p></p></P><P 25pt; mso-line-height-rule: exactly"> <p></p></P><P 25pt; mso-line-height-rule: exactly">main()<p></p></P><P 25pt; mso-line-height-rule: exactly">  {<p></p></P><P 25pt; mso-line-height-rule: exactly">  int i,n=3;<p></p></P><P 25pt; mso-line-height-rule: exactly">  double a[3][3]={6,-12,6,-21,-3,24,-12,-12,51};<p></p></P><P 25pt; mso-line-height-rule: exactly">  double *pa[3],u0[3]={1,2,3};<p></p></P><P 25pt; mso-line-height-rule: exactly">  double lbt,eps=1.0e-12;<p></p></P><P 25pt; mso-line-height-rule: exactly">  pa[0]=a[0];pa[1]=a[1];pa[2]=a[2];<p></p></P><P 25pt; mso-line-height-rule: exactly">  lbt=mifa(pa,u0,n,eps);<p></p></P><P 25pt; mso-line-height-rule: exactly">  printf("\nlbt=%10.8lf",lbt);<p></p></P><P 25pt; mso-line-height-rule: exactly">  printf("\n");<p></p></P><P 25pt; mso-line-height-rule: exactly">  for(i=0;i&lt;n;i++)printf("X[%d]=%10.8lf  ",i,u0);<p></p></P><P 25pt; mso-line-height-rule: exactly">  }<p></p></P><P 25pt; mso-line-height-rule: exactly"> <p></p></P>
 楼主| 发表于 2004-5-15 07:15:07 | 显示全部楼层
< center" align=center><B>牛顿插值法通用计算程序</B><B>NUCA.C<p></p></B></P>< 24pt; mso-line-height-rule: exactly"><B>#include&lt;stdio.h&gt;<p></p></B></P>< 24pt; mso-line-height-rule: exactly"><B>#include&lt;conio.h&gt;<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>double nuca(double xi[],double yi[],int n,double x)<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>{<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>int k,j;  double y,xn;  double yk[40];<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>for(k=0;k&lt;=n;k++)yk[k]=yi[k];<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>y=yk[0];  xn=1.0;<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>for(j=1;j&lt;=n;j++)<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  {<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  for(k=0;k&lt;=n-j;k++)<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  yk[k]=(yk[k+1]-yk[k])/(xi[k+j]-xi[k]);<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  xn=xn*(x-xi[j-1]);<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  y+=yk[0]*xn;<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  }<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>return(y);<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>}<p></p></B></P><P 24pt; mso-line-height-rule: exactly"><B>main()<p></p></B></P><P 24pt; mso-line-height-rule: exactly"><B>{<p></p></B></P><P 24pt; mso-line-height-rule: exactly"><B>double xi[]={64.0,81.0,100.0,121.0,144.0,169.0};<p></p></B></P><P 24pt; mso-line-height-rule: exactly"><B>double yi[]={8.0,9.0,10.0,11.0,12.0,13.0};<p></p></B></P><P 24pt; mso-line-height-rule: exactly"><B>double x=115.0,y;<p></p></B></P><P 24pt; mso-line-height-rule: exactly"><B>y=nuca(xi,yi,5,x);<p></p></B></P><P 24pt; mso-line-height-rule: exactly"><B>printf("\nx=%g y=%10.8lf",x,y);<p></p></B></P><P 24pt; mso-line-height-rule: exactly"><B>}</B></P>
 楼主| 发表于 2004-5-15 07:16:04 | 显示全部楼层
< center" align=center><B>牛顿法解非线性方程组  newtonfa.c<p></p></B></P>< 23pt; mso-line-height-rule: exactly">#include&lt;math.h&gt;<p></p></P>< 23pt; mso-line-height-rule: exactly">#include&lt;stdio.h&gt;<p></p></P><P 23pt; mso-line-height-rule: exactly">#define N   3<p></p></P><P 23pt; mso-line-height-rule: exactly">double a[N][N+1];<p></p></P><P 23pt; mso-line-height-rule: exactly">void FDF(double x[])<p></p></P><P 23pt; mso-line-height-rule: exactly">{<p></p></P><P 23pt; mso-line-height-rule: exactly">double ex,sx,pi;<p></p></P><P 23pt; mso-line-height-rule: exactly">pi=4.0*atan(1.0);<p></p></P><P 23pt; mso-line-height-rule: exactly">a[0][3]=3.0*x[0]-cos(x[1]*x[2])-0.5;<p></p></P><P 23pt; mso-line-height-rule: exactly">a[1][3]=x[0]*x[0]-81.0*(x[1]+0.1)*(x[1]+0.1)+sin(x[2])+1.06;<p></p></P><P 23pt; mso-line-height-rule: exactly">ex=exp(-x[0]*x[1]);<p></p></P><P 23pt; mso-line-height-rule: exactly">a[2][3]=ex+20.0*x[2]+pi*10.0/3.0-1.0;<p></p></P><P 23pt; mso-line-height-rule: exactly">a[0][0]=3.0;sx=sin(x[1]*x[2]);<p></p></P><P 23pt; mso-line-height-rule: exactly">a[0][1]=x[2]*sx;a[0][2]=x[1]*sx;<p></p></P><P 23pt; mso-line-height-rule: exactly">a[1][0]=2.0*x[0];a[1][1]=-162.0*(x[1]+0.1);a[1][2]=cos(x[2]);<p></p></P><P 23pt; mso-line-height-rule: exactly">a[2][0]=-x[1]*ex;a[2][1]=-x[0]*ex;a[2][2]=20.0;<p></p></P><P 23pt; mso-line-height-rule: exactly">}<p></p></P><P 23pt; mso-line-height-rule: exactly">int gauss(int n)<p></p></P><P 23pt; mso-line-height-rule: exactly">  {<p></p></P><P 23pt; mso-line-height-rule: exactly">  int i,j,k,i0;<p></p></P><P 23pt; mso-line-height-rule: exactly">  double c,t,eps=1.0e-15;<p></p></P><P 23pt; mso-line-height-rule: exactly">  for(k=0;k&lt;n;k++)<p></p></P><P 23pt; mso-line-height-rule: exactly">    {<p></p></P><P 23pt; mso-line-height-rule: exactly">    c=0.0;<p></p></P><P 23pt; mso-line-height-rule: exactly">    for(i=k;i&lt;n;i++)<p></p></P><P 23pt; mso-line-height-rule: exactly">      {<p></p></P><P 23pt; mso-line-height-rule: exactly">      t=fabs(a[k]);<p></p></P><P 23pt; mso-line-height-rule: exactly">      if(t&gt;c)<p></p></P><P 23pt; mso-line-height-rule: exactly">        {   c=t;i0=i;  }<p></p></P><P 23pt; mso-line-height-rule: exactly">      }<p></p></P><P 23pt; mso-line-height-rule: exactly">    if(c&lt;=eps)return(0);<p></p></P><P 23pt; mso-line-height-rule: exactly">    if(i0!=k)<p></p></P><P 23pt; mso-line-height-rule: exactly">      for(j=k;j&lt;=n;j++)<p></p></P><P 23pt; mso-line-height-rule: exactly">        {<p></p></P><P 23pt; mso-line-height-rule: exactly">        t=a[k][j];<p></p></P><P 23pt; mso-line-height-rule: exactly">        a[k][j]=a[i0][j];   a[i0][j]=t;<p></p></P><P 23pt; mso-line-height-rule: exactly">        }<p></p></P><P 23pt; mso-line-height-rule: exactly">    c=1.0/a[k][k];<p></p></P><P 23pt; mso-line-height-rule: exactly">    for(j=k+1;j&lt;=n;j++)a[k][j]*=c;<p></p></P><P 23pt; mso-line-height-rule: exactly">    for(i=k+1;i&lt;n;i++)<p></p></P><P 64pt; TEXT-INDENT: -64pt; LINE-HEIGHT: 23pt; mso-char-indent-count: -4.0; mso-char-indent-size: 16.0pt; mso-line-height-rule: exactly">      for(j=k+1;j&lt;=n;j++) a[j]-=(a[k]*a[k][j]);<p></p></P><P 23pt; mso-line-height-rule: exactly">    }<p></p></P><P 23pt; mso-line-height-rule: exactly">  for(i=n-1;i&gt;=0;i--)<p></p></P><P 23pt; mso-line-height-rule: exactly">    for(j=i+1;j&lt;n;j++)<p></p></P><P 23pt; mso-line-height-rule: exactly">      a[n]-=(a[j]*a[j][n]);<p></p></P><P 23pt; mso-line-height-rule: exactly">  return(1);<p></p></P><P 23pt; mso-line-height-rule: exactly">}<p></p></P><P 23pt; mso-line-height-rule: exactly">int  newtonfa(double x0[],double eps)<p></p></P><P 23pt; mso-line-height-rule: exactly">{<p></p></P><P 23pt; mso-line-height-rule: exactly">int i,k=1;  double  dx,x1[N],y[N];<p></p></P><P 23pt; mso-line-height-rule: exactly">for(;;)<p></p></P><P 23pt; mso-line-height-rule: exactly">  {<p></p></P><P 23pt; mso-line-height-rule: exactly">  FDF(x0);<p></p></P><P 23pt; mso-line-height-rule: exactly">  if(!gauss(N))return(0);<p></p></P><P 23pt; mso-line-height-rule: exactly">  for(i=0;i&lt;N;i++)y=a[N];<p></p></P><P 23pt; mso-line-height-rule: exactly">  dx=0.0;<p></p></P><P 23pt; mso-line-height-rule: exactly">  for(i=0;i&lt;N;i++) if(fabs(y)&gt;dx)dx=fabs(y);<p></p></P><P 23pt; mso-line-height-rule: exactly">  for(i=0;i&lt;N;i++) x1=x0-y;<p></p></P><P 23pt; mso-line-height-rule: exactly">  for(i=0;i&lt;N;i++) x0=x1;<p></p></P><P 23pt; mso-line-height-rule: exactly">  if(dx&lt;eps)break;<p></p></P><P 23pt; mso-line-height-rule: exactly">    printf("\nk=%d",k);<p></p></P><P 23pt; mso-line-height-rule: exactly">    printf("\nx1=%12.10lf x2=%12.10lf  x3=%12.10lf",x1[0],x1[1],x1[2]);<p></p></P><P 23pt; mso-line-height-rule: exactly">  k++;if(k&gt;200)return(0);<p></p></P><P 23pt; mso-line-height-rule: exactly">  }<p></p></P><P 23pt; mso-line-height-rule: exactly">return(1);<p></p></P><P 23pt; mso-line-height-rule: exactly">}<p></p></P><P 23pt; mso-line-height-rule: exactly">main()<p></p></P><P 23pt; mso-line-height-rule: exactly">{<p></p></P><P 23pt; mso-line-height-rule: exactly">int i;double x0[N]={3.0,2.0,1.0},eps=1.0e-12;<p></p></P><P 23pt; mso-line-height-rule: exactly">if(newtonfa(x0,eps))<p></p></P><P 23pt; mso-line-height-rule: exactly">  {<p></p></P><P 23pt; mso-line-height-rule: exactly">  for(i=0;i&lt;N;i++)<p></p></P><P 23pt; mso-line-height-rule: exactly">    printf("\nx[%d] = %14.12lf  ",i,x0);<p></p></P><P 23pt; mso-line-height-rule: exactly">  }<p></p></P><P 23pt; mso-line-height-rule: exactly">else printf("\nFiled !");<p></p></P><P 23pt; mso-line-height-rule: exactly">}</P>
 楼主| 发表于 2004-5-15 07:17:08 | 显示全部楼层
< center" align=center><B>牛顿法求方程的根 NTROOT.C<p></p></B></P>< 22pt; mso-line-height-rule: exactly"><B>#include&lt;math.h&gt;<p></p></B></P>< 22pt; mso-line-height-rule: exactly"><B>#include&lt;stdio.h&gt;<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>double f(double x)<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>{  return(x+log(x)-2.0);  }<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B> <p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>double df(double x)<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>{  return(1.0+1.0/x);  }<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B> <p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>double ca_root(double x0,double eps)<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>{<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>int n=1;  double x1,fx,dfx;<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>while(n&lt;800)<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  {<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  fx=f(x0);    dfx=df(x0);  x1=x0-fx/dfx;<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  printf("\nn=%d  x1=%14.12lf",n,x1);<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  if(fabs(x1-x0)&lt;eps)break;  x0=x1;n++;<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>  }<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>return(x1);<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>}<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>main()<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>{<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>double eps=1.0e-12,root;<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>root=ca_root(1.0,eps);<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>printf("\nThe root =%14.12lf",root);<p></p></B></P><P 22pt; mso-line-height-rule: exactly"><B>}<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>经过5次循环,计算结果为:1.557145598998<p></p></B></P>
 楼主| 发表于 2004-5-15 07:21:26 | 显示全部楼层
<>/*穷举法求多元函数极值*/</P><>#include&lt;math.h&gt;</P><>#include&lt;float.h&gt;</P><P>#include&lt;stdio.h&gt;</P><P>#include&lt;stdlib.h&gt;</P><P>double PI,xishu;</P><P>double ex=600.0,dx=190.0;</P><P>double N01(double t)</P><P>{</P><P>double x;</P><P>x=(t-ex)/dx;</P><P>return(exp(-(x*x*0.5)));</P><P>}</P><P>double EXN01(double t)</P><P>{</P><P>double x;</P><P>x=(t-ex)/dx;</P><P>return(t*exp(-x*x*0.5));</P><P>}</P><P>double simpson(double a,double b,double eps,double (*f)(double))</P><P>  {</P><P>  int m=1; double x,h,h1,sam1,sam2,s2,s1,d,rs;</P><P>  h=0.5*(b-a); rs=1.0/3.0;</P><P>  sam1=f(a)+f(b); sam2=f(a+h); s1=h*rs*(sam1+4.0*sam2);</P><P>  while(m&lt;=19)</P><P>    {</P><P>    h1=h; h*=0.5; sam1+=(sam2*2.0);</P><P>    sam2=0.0; x=a+h;</P><P>    while(x&lt;b)</P><P>      { sam2+=f(x); x+=h1; }</P><P>    s2=h*rs*(sam1+4.0*sam2);</P><P>    if(fabs(s2-s1)&lt;eps)break;</P><P>    s1=s2; m++;</P><P>    }</P><P>  return(s2);</P><P>  }</P><P>double F1(double x)</P><P>  {</P><P>  return(xishu*simpson(0.0,x,1.0e-12,EXN01));</P><P>  }</P><P>double FX(double x)</P><P>  {</P><P>  return(xishu*simpson(0.0,x,1.0e-12,N01));</P><P>  }</P><P>double au(double x)</P><P>  {</P><P>  double a,g1,g2;</P><P>  g1=F1(x);  g2=FX(x);</P><P>  a=(g1+x-x*g2)/g2;</P><P>  return(a);</P><P>  }</P><P> <p></p></P><P>main()</P><P>  {</P><P>  double minl=1.0e+10,x,y,z,a,b=11400.0,c,f2;</P><P>  double minx,miny;</P><P>  PI=4.0*atan(1.0);</P><P>  xishu=1.0/(dx*sqrt(PI*2.0));</P><P>  for(x=300.0;x&lt;=400.0;x+=1.0)</P><P>    {</P><P>    f2=1000.0/x;</P><P>    a=au(x);c=(a*b)/(a+b);</P><P>    for(y=10.0;y&lt;=25.0;y+=1.0)</P><P>      {</P><P>      z=12.0/y+f2+((y+1.0)*100.0+2500)/c;</P><P>      if(z&lt;minl)</P><P>        {</P><P>        minl=z;minx=x,miny=y;</P><P>        printf("\nminl=%10.8lf  x=%g  y=%g",minl,minx,miny);</P><P>        }</P><P>      }</P><P>    }</P><P>  printf("\nminl=%10.8lf  x=%g  y=%g",minl,minx,miny);</P><P>  }</P><P> <p></p></P>
 楼主| 发表于 2004-5-15 07:22:16 | 显示全部楼层
< 36pt; TEXT-INDENT: -36pt; LINE-HEIGHT: 22pt; mso-line-height-rule: exactly; tab-stops: list 36.0pt; mso-list: l2 level1 lfo2">例3     当n=0,1,2,…,10时,输出3<SUP>n</SUP><p></p></P>< 22pt; mso-line-height-rule: exactly">#include&lt;stdio.h&gt;<p></p></P>< 22pt; mso-line-height-rule: exactly">main()<p></p></P><P 22pt; mso-line-height-rule: exactly">  {<p></p></P><P 22pt; mso-line-height-rule: exactly">  int  n;  unsigned  answer=1;<p></p></P><P 16.8pt; LINE-HEIGHT: 22pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">for(n=0;n&lt;=10;n++)<p></p></P><P 16.8pt; LINE-HEIGHT: 22pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">  {<p></p></P><P 16.8pt; LINE-HEIGHT: 22pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">  printf(“\n3的%d次方等于%u”,n,answer);<p></p></P><P 16.8pt; LINE-HEIGHT: 22pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">  answer*=3;<p></p></P><P 33.6pt; LINE-HEIGHT: 22pt; mso-char-indent-count: 2.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">}<p></p></P><P 16.8pt; LINE-HEIGHT: 22pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">}<p></p></P><P 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"> <p></p></P><P 36pt; TEXT-INDENT: -36pt; tab-stops: list 36.0pt; mso-list: l2 level1 lfo2">例4           输入两个任意正整数,求它们的最大公约数。<p></p></P><P 21pt; mso-line-height-rule: exactly">#include&lt;stdio.h&gt;<p></p></P><P 21pt; mso-line-height-rule: exactly">main()<p></p></P><P 16.8pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">{<p></p></P><P 21pt; mso-line-height-rule: exactly">  unsigned  n1, n2, t;<p></p></P><P 16.8pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">printf(“\nInput two number\n”);<p></p></P><P 16.8pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">scanf(“%u,%u”,&amp;n1,&amp;n2);<p></p></P><P 16.8pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">if(n1&gt;n2)  {t=n1; n1=n2; n2=t;}<p></p></P><P 16.8pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">for(;;)<p></p></P><P 33.6pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">{<p></p></P><P 33.6pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">t=n2%n1;<p></p></P><P 33.6pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">if(t==0)break;<p></p></P><P 33.6pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">n2=n1; n1=t;<p></p></P><P 33.6pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">}<p></p></P><P 16.8pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">printf(“\n最大公约数是%u”,n1);<p></p></P><P 16.8pt; LINE-HEIGHT: 21pt; mso-char-indent-count: 1.0; mso-char-indent-size: 16.8pt; mso-line-height-rule: exactly">}<p></p></P>
 楼主| 发表于 2004-5-15 07:22:46 | 显示全部楼层
< center" align=center>/*        列主元法求逆矩阵      */<p></p></P>< 18pt">#include&lt;math.h&gt;<p></p></P>< 18pt">#include&lt;stdio.h&gt;<p></p></P><P 18pt">#include&lt;malloc.h&gt;<p></p></P><P 18pt">int inverta(double  *a[],double  *b[],int  n,double  eps)<p></p></P><P 18pt">  {<p></p></P><P 18pt">  int i,j,k,i0;<p></p></P><P 18pt">  double c,t,maxa;<p></p></P><P 18pt">  for(i=0;i&lt;n;i++)<p></p></P><P 18pt">    for(j=0;j&lt;n;j++)<p></p></P><P 18pt">      if(j==i)b[j]=1.0;<p></p></P><P 18pt">      else b[j]=0.0;<p></p></P><P 18pt">  for(k=0;k&lt;n;k++)<p></p></P><P 32.4pt; LINE-HEIGHT: 18pt">{<p></p></P><P 32.4pt; LINE-HEIGHT: 18pt">maxa=0.0;<p></p></P><P 18pt">    for(i=k;i&lt;n;i++)<p></p></P><P 18pt">      {<p></p></P><P 18pt">      t=fabs(a[k]);<p></p></P><P 18pt">      if(t&gt;maxa)<p></p></P><P 18pt">           {          maxa=t;i0=i;          }<p></p></P><P 18pt">      }<p></p></P><P 18pt">    if(maxa&lt;=eps)return(0);<p></p></P><P 18pt">    if(i0!=k)<p></p></P><P 18pt">      {<p></p></P><P 18pt">      for(j=0;j&lt;n;j++)<p></p></P><P 18pt">          {<p></p></P><P 18pt">          t=a[k][j];c=b[k][j];<p></p></P><P 18pt">          a[k][j]=a[i0][j];b[k][j]=b[i0][j];<p></p></P><P 18pt">          a[i0][j]=t;b[i0][j]=c;<p></p></P><P 18pt">          }<p></p></P><P 18pt">      }<p></p></P><P 18pt">    c=1.0/a[k][k];<p></p></P><P 18pt">    for(j=k;j&lt;n;j++)a[k][j]*=c;<p></p></P><P 18pt">    for(j=0;j&lt;n;j++)b[k][j]*=c;<p></p></P><P 18pt">    for(i=k+1;i&lt;n;i++)<p></p></P><P 18pt">      {<p></p></P><P 18pt">      for(j=k+1;j&lt;n;j++)   a[j]-=(a[k]*a[k][j]);<p></p></P><P 18pt">      for(j=0;j&lt;n;j++)     b[j]-=(a[k]*b[k][j]);<p></p></P><P 18pt">      }<p></p></P><P 18pt">    }<p></p></P><P 18pt">  for(k=n-2;k&gt;=0;k--)<p></p></P><P 18pt">    for(i=k;i&gt;=0;i--)<p></p></P><P 18pt">      for(j=0;j&lt;n;j++)b[j]-=(b[k+1][j]*a[k+1]);<p></p></P><P 18pt">  return(1);<p></p></P><P 18pt">  }<p></p></P><P 18pt"> <p></p></P><P 18pt">main()<p></p></P><P 18pt">  {<p></p></P><P 18pt">  int i,j,n=4;<p></p></P><P 18pt">  double eps=1.0e-9, *pa[4],*pb[4];<p></p></P><P 18pt">  double a[4][4]={1,1,0,1,1,2,2,2,2,-2,2,1,3,-1,5,3},b[4][4];<p></p></P><P 18pt">  for(i=0;i&lt;n;i++)<p></p></P><P 18pt">    {    pa=a;    pb=b;    }<p></p></P><P 18pt">  if(inverta(pa,pb,n,eps))<p></p></P><P 18pt">    {<p></p></P><P 18pt">    for(i=0;i&lt;n;i++)<p></p></P><P 18pt">      {<p></p></P><P 18pt">      printf("\n");<p></p></P><P 18pt">      for(j=0;j&lt;n;j++)<p></p></P><P 18pt">      printf("a-1[%d][%d]=%g   ",i,j,b[j]);<p></p></P><P 18pt">      }<p></p></P><P 18pt">    }<p></p></P><P 18pt">  else printf("\nFiled !");<p></p></P><P 18pt">  }<p></p></P>
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

小黑屋|手机版|Archiver|数学建模网 ( 湘ICP备11011602号 )

GMT+8, 2024-11-27 22:28 , Processed in 0.064950 second(s), 12 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表