数模论坛

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

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

[复制链接]
 楼主| 发表于 2004-5-15 07:49:47 | 显示全部楼层
< center" align=center><B>正割法求方程的根  YUNJIROT.C<p></p></B></P>< 23pt; mso-line-height-rule: exactly"><B>#include&lt;math.h&gt;<p></p></B></P>< 23pt; mso-line-height-rule: exactly"><B>#include&lt;stdio.h&gt;<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>double f(double x)<p></p></B></P><P 15.6pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: .99; mso-char-indent-size: 15.75pt"><B>{  return(x+log(x)-2.0);  }<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B> <p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>double ca_root(double x0,double x1,double eps)<p></p></B></P><P 15.75pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: 1.0; mso-char-indent-size: 15.75pt"><B>{<p></p></B></P><P 15.75pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: 1.0; mso-char-indent-size: 15.75pt"><B>int  n=1;  double x2,fx0,fx1;<p></p></B></P><P 15.75pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: 1.0; mso-char-indent-size: 15.75pt"><B>fx0=f(x0);<p></p></B></P><P 15.75pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: 1.0; mso-char-indent-size: 15.75pt"><B>while(n&lt;800)<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>    {<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>    fx1=f(x1);  x2=x1-fx1/(fx1-fx0)*(x1-x0);<p></p></B></P><P 15.75pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: 1.0; mso-char-indent-size: 15.75pt"><B>  if(fabs(x2-x1)&lt;eps)break;<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>    x0=x1;fx0=fx1;x1=x2;n++;<p></p></B></P><P 15.75pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: 1.0; mso-char-indent-size: 15.75pt"><B>  }<p></p></B></P><P 15.75pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: 1.0; mso-char-indent-size: 15.75pt"><B>return(x2);<p></p></B></P><P 15.75pt; LINE-HEIGHT: 23pt; mso-line-height-rule: exactly; mso-char-indent-count: 1.0; mso-char-indent-size: 15.75pt"><B>}<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B> <p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>main()<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>{<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>double  root, eps=1.0e-12;<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>root=ca_root(1.0,2.0,eps);<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>printf("\nThe root =%14.12lf",root);<p></p></B></P><P 23pt; mso-line-height-rule: exactly"><B>}<p></p></B></P><P 23pt; mso-line-height-rule: exactly">经过6次循环,计算结果为:1.557145598998<p></p></P>
 楼主| 发表于 2004-5-15 07:50:13 | 显示全部楼层
< center" align=center>/*最优梯度法求多元函数的极小值  TIDU.C  */<p></p></P><>#include&lt;conio.h&gt;</P><>#include&lt;math.h&gt;</P><P>#include&lt;stdio.h&gt;</P><P>#define N   2</P><P>int j;  double x[N];</P><P>double f(double x[])</P><P>{</P><P>double fxy;</P><P>fxy=x[0]-x[1]+2.0*x[0]*x[0]+2.0*x[0]*x[1]+x[1]*x[1]+log(1.0+x[1]*x[1]);</P><P>return(fxy);</P><P>}</P><P>void grad(double x[],double g[])</P><P>  {</P><P>  g[0]=4.0*x[0]+2.0*x[1]+1.0;</P><P>  g[1]=2.0*x[0]+2.0*x[1]-1.0+2.0*x[1]/(1.0+x[1]*x[1]);</P><P>  }</P><P>double fun(double x[],double g[],double lbt)</P><P>{</P><P>int i;  double x2[N];</P><P>for(i=0;i&lt;N;i++) x2=x-lbt*g;</P><P>return(f(x2));</P><P>}</P><P>double ca_min(double x[],double g[],double eps)</P><P>{</P><P>double a,b,c,x1,x2,x3,f1,f2,f3;</P><P>a=-2.0;  b=2.0;  c=(b-a)*0.25;  x1=a+c;  f1=fun(x,g,x1);</P><P>x2=x1+c;  f2=fun(x,g,x2);  x3=x2+c;  f3=fun(x,g,x3);</P><P>for(;;)</P><P>  {</P><P>  if(f1&lt;=f2&amp;&amp;f1&lt;f3)</P><P>    {    b=x2;x2=x1;f2=f1;    }</P><P>  else if(f3&lt;=f2&amp;&amp;f3&lt;f1)</P><P>    {    a=x2;x2=x3;f2=f3;    }</P><P>  else</P><P>    {    a=x1;b=x3;    }</P><P>  c=(b-a)*0.25;</P><P>  if(fabs(c)&lt;eps)break;</P><P>  x1=a+c;f1=fun(x,g,x1);  x3=x2+c;f3=fun(x,g,x3);</P><P>  }</P><P>return(x2);</P><P>}</P><P> <p></p></P><P>void  tidufa(double x0[],double eps)</P><P>{</P><P>int i; unsigned k=0;</P><P>double lbt,f1,f2,x1[N],g[N];</P><P>f1=f(x0);</P><P>while(k&lt;100)</P><P>  {</P><P>  grad(x0,g);</P><P>  lbt=ca_min(x0,g,eps*0.1);</P><P>    /*printf("\nlbt=%g \n",lbt);*/</P><P>  for(i=0;i&lt;N;i++)x1=x0-lbt*g;</P><P>  f2=f(x1);</P><P>  for(i=0;i&lt;N;i++)x0=x1;</P><P>  if(fabs(f2-f1)&lt;eps)</P><P>    {</P><P>    printf("f2-f1&lt;eps  k=%d",k);</P><P>    break;</P><P>    }</P><P>    /*printf("k=%d  x1=%15.14lf  x2=%15.14lf",k,x1[0],x1[1]);*/</P><P>  f1=f2;k++;</P><P>  }</P><P>}</P><P> <p></p></P><P>main()</P><P>{</P><P>int i;</P><P>double x0[N],eps=1.0e-015;</P><P>x0[0]=1.0;x0[1]=2.0;</P><P>tidufa(x0,eps);</P><P>  for(i=0;i&lt;N;i++)</P><P 21.6pt">printf("\nx[%d] = %12.10lf  ",i,x0);</P><P>printf("\nf(X) = %12.10lf  ",f(x0);</P><P>}</P><P> <p></p></P>
 楼主| 发表于 2004-5-15 07:50:38 | 显示全部楼层
< 17pt; TEXT-ALIGN: center; mso-line-height-rule: exactly" align=center>/*改进坐标轮换法求多元函数的极小值  DIRECT.C  */<p></p></P>< 17pt; mso-line-height-rule: exactly"> <p></p></P>< 17pt; mso-line-height-rule: exactly">#include&lt;conio.h&gt;<p></p></P><P 17pt; mso-line-height-rule: exactly">#include&lt;math.h&gt;<p></p></P><P 17pt; mso-line-height-rule: exactly">#include&lt;stdio.h&gt;<p></p></P><P 17pt; mso-line-height-rule: exactly">#define N   2<p></p></P><P 17pt; mso-line-height-rule: exactly"> <p></p></P><P 17pt; mso-line-height-rule: exactly">int j;<p></p></P><P 17pt; mso-line-height-rule: exactly">double x[N];<p></p></P><P 17pt; mso-line-height-rule: exactly"> <p></p></P><P 17pt; mso-line-height-rule: exactly">double fun(double x[])<p></p></P><P 17pt; mso-line-height-rule: exactly">{<p></p></P><P 17pt; mso-line-height-rule: exactly">double fxy,t,d;<p></p></P><P 17pt; mso-line-height-rule: exactly">t=x[0]-2.0;d=x[0]-2.0*x[1];<p></p></P><P 17pt; mso-line-height-rule: exactly">fxy=t*t*t*t+d*d;<p></p></P><P 17pt; mso-line-height-rule: exactly">return(fxy);<p></p></P><P 17pt; mso-line-height-rule: exactly">}<p></p></P><P 17pt; mso-line-height-rule: exactly"> <p></p></P><P 17pt; mso-line-height-rule: exactly">double fx(double y[],double lbt)<p></p></P><P 17pt; mso-line-height-rule: exactly">{<p></p></P><P 17pt; mso-line-height-rule: exactly">int i;<p></p></P><P 17pt; mso-line-height-rule: exactly">for(i=0;i&lt;N;i++)<p></p></P><P 17pt; mso-line-height-rule: exactly">x=y;<p></p></P><P 17pt; mso-line-height-rule: exactly">x[j]=y[j]+lbt;<p></p></P><P 17pt; mso-line-height-rule: exactly">return(fun(x));<p></p></P><P 17pt; mso-line-height-rule: exactly">}<p></p></P><P 17pt; mso-line-height-rule: exactly"> <p></p></P><P 17pt; mso-line-height-rule: exactly">double fy(double y[],double lbt)<p></p></P><P 17pt; mso-line-height-rule: exactly">{<p></p></P><P 17pt; mso-line-height-rule: exactly">int i;<p></p></P><P 17pt; mso-line-height-rule: exactly">double x2[2];<p></p></P><P 17pt; mso-line-height-rule: exactly">for(i=0;i&lt;N;i++)<p></p></P><P 17pt; mso-line-height-rule: exactly">x2=x+lbt*y;<p></p></P><P 17pt; mso-line-height-rule: exactly">return(fun(x2));<p></p></P><P 17pt; mso-line-height-rule: exactly">}<p></p></P><P 17pt; mso-line-height-rule: exactly"> <p></p></P><P 17pt; mso-line-height-rule: exactly">double   ca_min(double a,double b,double eps,\<p></p></P><P 17pt; mso-line-height-rule: exactly">double (*fun)(),double y[])<p></p></P><P 17pt; mso-line-height-rule: exactly">{<p></p></P><P 17pt; mso-line-height-rule: exactly">double c;<p></p></P><P 17pt; mso-line-height-rule: exactly">double x1,x2,x3,f1,f2,f3;<p></p></P><P 17pt; mso-line-height-rule: exactly">c=(b-a)*0.25;<p></p></P><P 17pt; mso-line-height-rule: exactly">x1=a+c;f1=fun(y,x1);<p></p></P><P 17pt; mso-line-height-rule: exactly">x2=x1+c;f2=fun(y,x2);<p></p></P><P 17pt; mso-line-height-rule: exactly">x3=x2+c;f3=fun(y,x3);<p></p></P><P 17pt; mso-line-height-rule: exactly">for(;;)<p></p></P><P 17pt; mso-line-height-rule: exactly">  {<p></p></P><P 17pt; mso-line-height-rule: exactly">  if(f1&lt;=f2&amp;&amp;f1&lt;f3)<p></p></P><P 17pt; mso-line-height-rule: exactly">    {<p></p></P><P 17pt; mso-line-height-rule: exactly">    b=x2;x2=x1;f2=f1;<p></p></P><P 17pt; mso-line-height-rule: exactly">    }<p></p></P><P 17pt; mso-line-height-rule: exactly">  else if(f3&lt;=f2&amp;&amp;f3&lt;f1)<p></p></P><P 17pt; mso-line-height-rule: exactly">    {<p></p></P><P 17pt; mso-line-height-rule: exactly">    a=x2;x2=x3;f2=f3;<p></p></P><P 17pt; mso-line-height-rule: exactly">    }<p></p></P><P 17pt; mso-line-height-rule: exactly">  else<p></p></P><P 17pt; mso-line-height-rule: exactly">    {<p></p></P><P 17pt; mso-line-height-rule: exactly">    a=x1;b=x3;<p></p></P><P 17pt; mso-line-height-rule: exactly">    }<p></p></P><P 17pt; mso-line-height-rule: exactly">  c=(b-a)*0.25;<p></p></P><P 17pt; mso-line-height-rule: exactly">  if(fabs(c)&lt;eps)break;<p></p></P><P 17pt; mso-line-height-rule: exactly">  x1=a+c;f1=fun(y,x1);<p></p></P><P 17pt; mso-line-height-rule: exactly">  x3=x2+c;f3=fun(y,x3);<p></p></P><P 17pt; mso-line-height-rule: exactly">  }<p></p></P><P 17pt; mso-line-height-rule: exactly">return(x2);<p></p></P><P 17pt; mso-line-height-rule: exactly">}<p></p></P><P 17pt; mso-line-height-rule: exactly"> <p></p></P><P 17pt; mso-line-height-rule: exactly">int direct(double x0[],double eps)<p></p></P><P 17pt; mso-line-height-rule: exactly">{<p></p></P><P 17pt; mso-line-height-rule: exactly">int i;unsigned k=0;<p></p></P><P 17pt; mso-line-height-rule: exactly">double lbt,px;<p></p></P><P 17pt; mso-line-height-rule: exactly">double x1[N],y[N];<p></p></P><P 17pt; mso-line-height-rule: exactly">for(i=0;i&lt;N;i++)y=x0;<p></p></P><P 17pt; mso-line-height-rule: exactly">while(k&lt;200)<p></p></P><P 17pt; mso-line-height-rule: exactly">  {<p></p></P><P 17pt; mso-line-height-rule: exactly">  for(j=0;j&lt;N;j++)<p></p></P><P 17pt; mso-line-height-rule: exactly">    {<p></p></P><P 17pt; mso-line-height-rule: exactly">    lbt=ca_min(-1.0,1.0,eps*0.01,fx,y);<p></p></P><P 17pt; mso-line-height-rule: exactly">    y[j]=y[j]+lbt;<p></p></P><P 17pt; mso-line-height-rule: exactly">    }<p></p></P><P 17pt; mso-line-height-rule: exactly">  px=0.0;<p></p></P><P 17pt; mso-line-height-rule: exactly">  for(i=0;i&lt;N;i++)<p></p></P><P 17pt; mso-line-height-rule: exactly">    {<p></p></P><P 17pt; mso-line-height-rule: exactly">    x=x1=y;<p></p></P><P 17pt; mso-line-height-rule: exactly">    printf("x[%d]=%15.14lf  ",i,x);<p></p></P><P 17pt; mso-line-height-rule: exactly">    y=x1-x0;<p></p></P><P 17pt; mso-line-height-rule: exactly">    x0=x1;<p></p></P><P 17pt; mso-line-height-rule: exactly">    px+=(y*y);<p></p></P><P 17pt; mso-line-height-rule: exactly">    }<p></p></P><P 17pt; mso-line-height-rule: exactly">  if(px&lt;eps)break;<p></p></P><P 17pt; mso-line-height-rule: exactly">    lbt=ca_min(-2.0,2.0,eps,fy,y);<p></p></P><P 17pt; mso-line-height-rule: exactly">    printf("k=%d lbt=%g \n",k,lbt);<p></p></P><P 17pt; mso-line-height-rule: exactly">  for(i=0;i&lt;N;i++)y=x1+lbt*y;<p></p></P><P 17pt; mso-line-height-rule: exactly">  k++;<p></p></P><P 17pt; mso-line-height-rule: exactly">  }<p></p></P><P 17pt; mso-line-height-rule: exactly">return(1);<p></p></P><P 17pt; mso-line-height-rule: exactly">}<p></p></P><P 17pt; mso-line-height-rule: exactly"> <p></p></P><P 17pt; mso-line-height-rule: exactly">main()<p></p></P><P 17pt; mso-line-height-rule: exactly">{<p></p></P><P 17pt; mso-line-height-rule: exactly">int i;<p></p></P><P 17pt; mso-line-height-rule: exactly">double x0[2],eps=1.0e-012;<p></p></P><P 17pt; mso-line-height-rule: exactly">x0[0]=0.0;x0[1]=0.0;<p></p></P><P 17pt; mso-line-height-rule: exactly">direct(x0,eps);<p></p></P><P 17pt; mso-line-height-rule: exactly">  for(i=0;i&lt;N;i++)<p></p></P><P 17pt; mso-line-height-rule: exactly">    printf("\nx[%d] = %18.16lf  ",i,x0);<p></p></P><P 17pt; mso-line-height-rule: exactly">}</P>
发表于 2004-8-3 18:02:22 | 显示全部楼层
我正缺这个,真是救星,THANK YOU
发表于 2004-8-21 02:55:44 | 显示全部楼层
<>草你吗</P><>什么够日的程序</P>
发表于 2004-8-21 02:56:34 | 显示全部楼层
[em06]
发表于 2004-8-21 06:33:30 | 显示全部楼层
高手,真是高手,只是没有我要用的!!!
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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