虫儿飞信息加工坊

专业发展 网站建设 感悟心情 优惠购 音乐欣赏



当前位置:网站首页>>专业发展>>数学研究
常用基本数学方法程序实现

文章作者:佚名 责任编辑:admin 发布时间:2012/1/6 15:49:37


1、C程序
(1)I显示方法
#include <stdio.h>

#define HEIGHT 17

int main(void)
{

int i=0;
printf("\n\nIIIIIII\n");
while (i<HEIGHT)
{
printf(" III\n");
i++;
}
printf("IIIIIII\n\n\n");

return 0;
}

(2)艾特肯
#include<stdio.h>
#include<math.h>
float f(float a)
{float b;
b=pow(2,-a);
return b;
}

main()
{float x0,x1,e,y,z;
int k;
scanf("%f,%f",&x0,&e);
for(k=0;k<100;k++)
{y=f(x0);
z=f(y);
x1=x0-(y-x0)*(y-x0)/(z-2*y+x0);
if(fabs(x1-x0)<e)
{printf("%f",x1);
break;
}
else x0=x1;
}
}


(3)杜氏分解法
#include<stdio.h>
#include<math.h>

main()
{int n,i,j,k,t,sum;
float a[10][10],b[10],x[10],y[10];
printf("the top exp is ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=1;i<n;i++)
a[i][1]=a[i][1]/a[1][1];
for(k=1;k<n;k++)
{for(j=k;j<n;j++)
{sum=0;
for(t=0;t<k;t++)
sum+=a[k][t]*a[t][j];
a[k][j]=a[k][j]-sum;
}
if(k!=n-1)
{for(i=k+1;i<n;i++)
{sum=0;
for(t=0;t<k;t++)
sum+=a[i][t]*a[t][k];
a[i][k]=(a[i][k]-sum)/a[k][k];
}
}
}
y[0]=b[0];
for(i=1;i<n;i++)
{sum=0;
for(k=0;k<i;k++)
sum+=a[i][k]*y[k];
y[i]=b[i]-sum;
}
x[n-1]=y[n-1]/a[n-1][n-1];
for(i=n-2;i>=0;i--)
{sum=0;
for(k=i+1;k<n;k++)
sum+=a[i][k]*x[k];
x[i]=(y[i]-sum)/a[i][i];
}
for(i=0;i<n;i++)
printf("%f\n",x[i]);
}

(4)二分法
#include<stdio.h>
#include<math.h>

float f(float x)
{float y;
y=x*x+2*x+1;
return y;
}

main()
{float a,b,c,x;
printf("%s","input three numbers:\n");
scanf("%f%f%f",&a,&b,&c);
while(fabs(b-a)>=c)
{x=(a+b)/2;
if(f(x)!=0)
{if(f(a)*f(x)<0)
b=x;
else a=x;}
else a=x,b=x;
}
printf("%f\n",(a+b)/2);
}

(5)分段线性插值
#include<stdio.h>
#include<math.h>

main()
{int e,h,i,j,k,n;
float x[10],y[10],a,sum,total,e1,e2;
printf("input n\n");
scanf("%d",&n);
printf("input X\n");
for(i=0;i<=n;i++)
scanf("%f",&x[i]);
printf("input Y\n");
for(i=0;i<=n;i++)
scanf("%f",&y[i]);
printf("input x\n");
scanf("%f",&a);
for(j=1;j<=n;j++)
if(x[j-1]<=a&&a<=x[j])
e=j;
if(e==1)
h=j;
else if(e==n)
h=j-1;
else {e1=fabs(a-x[j-2]);
e2=fabs(a-x[j+1]);
if(e1<e2)
h=j-1;
else h=j;}
sum=0;
for(k=h-1;k<=h+1;k++)
{total=1;
for(j=h-1;j<k;j++)
total*=((a-x[j])/(x[k]-x[j]));
for(j=k+1;j<=h+1;j++)
total*=((a-x[j])/(x[k]-x[j]));
sum+=y[k]*total;
}
printf("x=%f,L=%f",a,sum);
}

(6)复合梯形法
#include<stdio.h>
#include<math.h>
#define f(x) 4/(1+(x)*(x))
float g(int k)
{int i,total;
total=1;
for(i=1;i<=k;i++)
total*=2;
return total;
}

main()
{int i,k,k0,k1,tag;
float a,b,e,e1,t[50],sum;
scanf("%f,%f,%f,%d,%d",&a,&b,&e,&k0,&k1);
t[0]=((b-a)*(f(1)-f(0)))/2;
k=1;
tag=0;
do{sum=0;
for(i=1;i<=g(k-1);i++)
sum+=f(a+(2*i-1)*(b-a)/g(k));
t[k]=t[k-1]/2+(b-a)*sum/g(k);
if(k>=k1)
{e1=fabs(t[k]-t[k-1]);
if(e1<e)
{printf("k=%d,T=%f\n",k,t[k]);
tag=1;
}
}
k++;
}while(tag==0&&k<k0);
if(tag==0||k>=k0)
printf("error!\n");
}

(7)复合辛普森
#include<stdio.h>
#include<math.h>
#define f(x) (x)/(4+(x)*(x))

main()
{int k,n;
float a,b,h,s,x;
scanf("%f,%f,%d",&a,&b,&n);
h=(b-a)/(2*n);
x=a;
s=f(x)-f(b);
k=1;
do
{x=x+h;s=s+4*f(x);
x=x+h;s=s+2*f(x);
k++;
}while(k<=n);
s=(s*h)/3;
printf("s=%f\n",s);
}

(8)改进欧拉法
#include<stdio.h>
#include<math.h>
#define f(x,y) (x)-(y)+1

main()
{int k,n;
float a,b,h,y0,x,y,t[2];
scanf("%f,%f,%f,%d",&a,&b,&y0,&n);
h=(b-a)/n;
x=a;y=y0;
printf("%f,%f\n",x,y);
k=1;
do{t[0]=y+h*f(x,y);
x=x+h;
t[1]=y+h*f(x,t[0]);
y=(t[0]+t[1])/2;
printf("%f,%f\n",x,y);
k++;
}while(k<=n);
}

(9)高斯消去法
#include<stdio.h>
#include<math.h>

main()
{float a[10][10],b[10],m[10][10],x[10],sum;
int i,j,k,n;
printf("the top exp:");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
for(i=0;i<n;i++)
scanf("%f",&b[i]);
for(k=0;k<n-1;k++)
{if(a[k][k]==0)
printf("error");
else for(i=k+1;i<n;i++)
{m[i][k]=a[i][k]/a[k][k];
a[i][k]=m[i][k];
b[i]=b[i]-m[i][k]*b[k];
for(j=k+1;j<n;j++)
a[i][j]=a[i][j]-m[i][k]*a[k][j];
}}
if(a[n-1][n-1]==0)
printf("error");
else x[n-1]=b[n-1]/a[n-1][n-1];
b[n-1]=x[n-1];
for(i=n-2;i>=0;i--)
{sum=0;
for(j=i+1;j<n;j++)
{sum+=a[i][j]*x[j];}
x[i]=(b[i]-sum)/a[i][i];
b[i]=x[i];
}
for(i=0;i<n;i++)
printf("%f\n",x[i]);
}

(10)简单迭代法
#include<stdio.h>
#include<math.h>

float f(float x)
{float y;
y=sqrt(10/(4+x));
return y;
}

main()
{float x0,x1,a;
int k,N;
k=0;
scanf("%f,%f,%d",&x0,&a,&N);
for(k=0;k<N;k++)
{x1=f(x0);
if(fabs(x1-x0)<a)
{printf("%d,%f",k,x1);
break;
}
else x0=x1;
}
if(k>=N)
{printf("error");
}
}

(11)列主元元素消元
#include<stdio.h>
#include<math.h>

main()
{float a[10][10],b[10],s,t,e,sum;
int i,j,k,n,m;
printf("The top exp is ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
for(i=0;i<n;i++)
scanf("%f",&b[i]);
scanf("%f",&e);
k=0;
do{t=a[k][k];
for(i=k;i<n;i++)
{if(fabs(t)<fabs(a[i][k]))
{t=a[i][k];
m=i;
}
else m=k;
}
if(fabs(t)<e)
printf("det A = 0\n");
else {if(m!=k)
{for(j=0;j<n;j++)
{s=a[m][j];
a[m][j]=a[k][j];
a[k][j]=s;
}
s=b[m];
b[m]=b[k];
b[k]=s;
}
for(i=k+1;i<n;i++)
for(j=k+1;j<n;j++)
{a[i][k]=a[i][k]/a[k][k];
a[i][j]=a[i][j]-a[i][k]*a[k][j];
b[i]=b[i]-a[i][k]*b[k];
}
}
k++;
}while(k<n-2);
if(fabs(a[n-1][n-1])<e)
printf("det A = 0\n");
else {b[n-1]=b[n-1]/a[n-1][n-1];
for(i=n-2;i>=0;i--)
{sum=0;
for(k=i+1;k<n;k++)
{sum+=a[k][j]*b[j];}
b[i]=(b[i]-sum)/a[i][i];
}
}
for(i=0;i<n;i++)
printf("%f\n",b[i]);
}

(12)龙贝格算法
#include<stdio.h>
#include<math.h>
#define f(x) 4/(1+(x)*(x))
float g(int k)
{int i,total;
total=1;
for(i=1;i<=k;i++)
total*=2;
return total;
}

main()
{int k0,k1,k,tag,i;
float a,b,e,e1,t[50],s[50],c[50],r[50],sum;
scanf("%f,%f,%f,%d,%d",&a,&b,&e,&k0,&k1);
t[0]=(b-a)*(f(0)+f(1))/2;
k=1;
tag=0;
do{if(k==1)
{t[1]=t[0]/2+(b-a)*f(a+(b-a)/2)/2;
s[0]=(4*t[1]-t[0])/3;
}
if(k==2)
{t[2]=t[1]/2+(b-a)*(f(a+(b-a)/4)+f(a+3*(b-a)/4))/4;
s[1]=(4*t[2]-t[1])/3;
c[0]=(16*s[1]-s[0])/15;
}
if(k>=3)
{sum=0;
for(i=1;i<=g(k-1);i++)
sum+=f(a+(2*i-1)*(b-a)/g(k));
t[k]=t[k-1]/2+(b-a)*sum/g(k);
s[k-1]=(4*t[k]-t[k-1])/3;
c[k-2]=(16*s[k-1]-s[k-2])/15;
r[k-3]=(64*c[k-2]-c[k-3])/63;
}
if(k>=k1)
{e1=fabs(r[k-3]-r[k-4]);
if(e1<e)
{printf("k=%d,T=%f\n",k,r[k-3]);
tag=1;
}
}
k++;
}while(tag==0&&k<k0);
if(tag==0||k>=k0)
printf("error!\n");
}

(13)龙格库塔方法
#include<stdio.h>
#include<math.h>
#define f(x,y) (x)-(y)+1

main()
{int n,k;
float a,b,y0,h,x,y,t[4];
scanf("%f,%f,%f,%d",&a,&b,&y0,&n);
h=(b-a)/n;
x=a;y=y0;
printf("%f,%f\n",x,y);
k=1;
do{t[0]=f(x,y);
x=x+h/2;
t[1]=f(x,y+h*t[0]/2);
t[2]=f(x,y+h*t[1]/2);
x=x+h/2;
t[3]=f(x,y+h*t[2]);
y=y+h*(t[0]+2*t[1]+2*t[2]+t[3])/6;
printf("%f,%f\n",x,y);
k++;
}while(k<=n);
}

(14)牛顿插值多项式
#include<stdio.h>
#include<math.h>

main()
{int i,j,k,n;
float x[10],y[10],a,b,p;
printf("input n\n");
scanf("%d",&n);
printf("input X\n");
for(i=0;i<=n;i++)
scanf("%f",&x[i]);
printf("input Y\n");
for(i=0;i<=n;i++)
scanf("%f",&y[i]);
printf("input x\n");
scanf("%f",&a);
b=0;
k=0;
do{p=1;
j=0;
do
{if(j!=k)
p=p*(a-x[j])/(x[k]-x[j]);
j++;
}while(j<n);
b=b+p*y[k];
k++;
}while(k<n);
printf("x=%f,y=%f",a,b);
}

(15)牛顿迭代
#include<stdio.h>
#include<math.h>
#define N 100

float f(float x)
{float y;
y=x-cos(x);
return y;
}

main()
{float x0,x1,a;
int k;
scanf("%f,%f",&x0,&a);
for(k=0;k<N;k++)
{x1=x0-(x0-cos(x0))/(1+sin(x0));
if(fabs(x1-x0)<a)
{printf("%f,%d",x1,k);
break;
}
else x0=x1;
}
if(k>=N)
{printf("error");
}
}

(16)牛顿下山
#include<stdio.h>
#include<math.h>

float f(float x)
{float y;
y=pow(x,3)-x-1;
return y;
}

float g(float x)
{float y;
y=3*pow(x,2)-1;
return y;
}

main()
{float x0,x1,b,e,a,x;
int k;
scanf("%f,%f",&x0,&a);
k=0;
if(fabs(g(x0))<a)
{printf("error");
}else
e=1.00;
while(fabs(f(x0))>=a)
{do{x1=x0-e*f(x0)/g(x0);
b=x0;
x0=x1;
e=e/2.00;
}while(fabs(f(x1))>=fabs(f(b)));
k+=1;e=1.00;
}
x=x0;
printf("%d,%f",k,x);
}

(17)秦九韶
#include<stdio.h>
#include<math.h>
#define N 100

main()
{int m,i,j;
double sum,x,a[N];
printf("input the x and the top exp:");
scanf("%lf,%d",&x,&m);
printf("\ninput the a[i](0<i<100):");
for(i=0;i<=m;i++)
{scanf("%lf",&t);
a[i]=t;
}
sum=a[m];
for(j=m-1;j>=0;j--)
sum=x*sum+a[j];
printf("\nthe result is:%lf\n",sum);
}

(18)三对角线追赶法
#include<stdio.h>
#include<math.h>

main()
{int i,j,k,n;
float d[10][10],g[10],a[10],b[10],c[10],x[10],y[10],f[10];
printf("the top exp is ");
scanf("%d",&n);
scanf("%f,%f,%f,%f",&d[0][0],&d[0][1],&d[n-1][n-2],&d[n-1][n-1]);
for(i=1;i<n-1;i++)
for(j=i-1;j<=i+1;j++)
scanf("%f",&d[i][j]);
for(i=0;i<n;i++)
scanf("%f",&g[i]);
for(i=1;i<n-1;i++)
a[i]=d[i][i-1];
for(i=0;i<n;i++)
b[i]=d[i][i];
for(i=0;i<n-1;i++)
c[i]=d[i][i+1];
f[0]=c[0]/b[0];
for(k=1;k<n-1;k++)
f[k]=c[k]/(b[k]-a[k]*f[k-1]);
y[0]=g[0]/b[0];
for(i=1;i<n;i++)
y[i]=(g[i]-a[i]*y[i-1])/(b[i]-a[i]*f[i-1]);
x[n-1]=y[n-1];
for(i=n-2;i>=0;i--)
x[i]=y[i]-f[i]*x[i+1];
for(i=0;i<n;i++)
printf("%f\n",x[i]);
}

(19)系统
#include <stdio.h>

int main(void)
{

int a,b,CHECK;
printf("Input two nonzero intergers:");
scanf("%d,%d",&a,&b);
printf("%s%4d\n%s%4d\n%s%4d\n%s%4d\n%s%4d\n",
" a=",a,
" b=",b,
" a/b=",a/b,
" a%b=",a%b,
" CHECK=",(a/b)*b+a%b-a
);


return 0;
}

(2)CHAIN:马尔可夫链算法
#include <stdio.h>
#include <math.h>

double a[10][10];

void Guass(int n){
int i,j,k;
double t;
for(k=0;k<n-1;k++)
{t=a[k][k];
for(j=k;j<n;j++)
a[k][j]=a[k][j]/t;
for(i=0;i<n-1;i++)
if(i!=k)
{t=a[i][k]/a[k][k];
for(j=k;j<n;j++)
a[i][j]=a[i][j]-a[k][j]*t;
}
}
return;
}


void chain(){
static double p[10][10],pr[10],diff,table[100][10],pnew[10][10],ptemp[10][10],temp[10],exr[10][10];
int n,i,j,k,s,m,found,inr,inc;
printf("Welcome to the MARKOV CHAIN ANALYSIS system!\n\n");
printf("how many states =? ");
scanf("%d",&n);
printf("\nthe steady transmit possibility of step 1 ?\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%lf",&p[i][j]);
printf("\nthe initiate state of step 1 ?\n");
for(i=0;i<n;i++)
scanf("%lf",&pr[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
pnew[i][j]=p[i][j];
for(i=0;i<n;i++)
table[0][i]=pr[i];
printf("\n step 1");
for(i=0;i<n;i++)
{printf("\n");
for(j=0;j<n;j++)
printf("%f\t",p[i][j]);
}
printf("\n");
for(k=2;k<100;k++)
{for(j=0;j<n;j++)
{temp[j]=0;
for(i=0;i<n;i++)
temp[j]=temp[j]+pr[i]*pnew[i][j];
}
for(i=0;i<n;i++)
table[k-1][i]=temp[i];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ptemp[i][j]=0;
for(m=0;m<n;m++)
ptemp[i][j]=ptemp[i][j]+p[i][m]*pnew[m][j];
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
pnew[i][j]=ptemp[i][j];
for(j=0;j<n;j++)
{for(i=0;i<n-1;i++)
{
for(m=i+1;m<n;m++)
{diff=pnew[i][j]-pnew[m][j];
if(diff<0)
diff=-diff;
if(diff>0.001)
{found=0;
break;
}
found=1;
}
if(diff>0.001) break;
}
if(diff>0.0001) break;
}
if(found==0)
{if(k%5==0)
{printf("\n step %d",k);
for(i=0;i<n;i++)
{printf("\n");
for(j=0;j<n;j++)
printf("%f\t",pnew[i][j]);
}
getch();
}
if(k>=100)
{printf("\nsteady_state probability have not been detained in 100");
return;
}
}
else
{printf("\nstep %d",k);
for(i=0;i<n;i++)
{printf("\n");
for(j=0;j<n;j++)
printf("%f\t",pnew[i][j]);
}
break;
}
}
for(j=0;j<n;j++)
{temp[j]=0;
for(i=0;i<n;i++)
temp[j]=temp[j]+pr[i]*pnew[i][j];
}
for(i=0;i<n;i++)
table[k][i]=temp[i];
printf("\nThe steady-state probability of being in \n");
for(j=0;j<n;j++)
printf("state %d is %f\n",j,pnew[n-1][j]);
printf("probability of being in state\n\n");
for(i=0;i<=k;i++)
{printf("%d",i);
for(j=0;j<n;j++)
printf("\t%f",table[i][j]);
printf("\n");
if(i%10==0) getch();
}

for(s=0;s<n;s++)
{ inr=0;
for(j=0;j<n;j++)
if(j==s)
continue;
else
{inc=0;
for(i=0;i<n;i++)
if(i==s)
continue;
else
{
a[inr][inc]=-p[j][i];
if(j==i)
a[inr][inc]=1+a[inr][inc];
inc++;
}
inr++;
}
for(i=0;i<n-1;i++)
a[i][n-1]=1;
Guass(n);
i=0;
for(j=0;j<n;j++)
if(j!=s)
exr[j][s]=a[i++][n-1];
else
exr[j][s]=1/pnew[n-1][s];
}
printf("\nTable of expected first passage times and recurrence times");
for(i=0;i<n;i++)
{printf("\n%d",i);
for(j=0;j<n;j++)
printf("\t%f",exr[i][j]);
}
}

void main(){
clrscr();
chain();
getch();
}

(3)DECISION:贝叶斯决策方法
#include <stdio.h>
#include <math.h>

#define pi 3.14159
#define p(x,t) exp(-(x-t)*(x-t)/20)/sqrt(20*pi)

void decision(){
int i,j,type,m,n,flag,state[5],index;
float xx,a[5][5],p[5],e[5],sum,decision;
printf("Welcome to the DECISION_STSTEM!");
printf("\n\ntype of the problem,max(key '0')or min(key '1')? ");
scanf("%d",&type);
printf("type of the decision,without data(key'0')or with data(key'1')? ");
scanf("%d",&flag);
printf("number of the actions ");
scanf("%d",&m);
printf("number of the nature states ");
scanf("%d",&n);
for(j=0;j<n;j++)
printf("\tt%d",j+1);
printf("\n");
for(i=0;i<m;i++)
{printf("A%d\t",i+1);
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
}
printf("probability of the natural states=?\n");
for(j=0;j<n;j++)
scanf("%f",&p[j]);
if(flag)
{printf("given data=? ");
scanf("%f",&xx);
printf("states of nature=?\n");
for(j=0;j<n;j++)
scanf("%d",&state[j]);
sum=0;
for(j=0;j<n;j++)
sum+=p[j]*p(xx,state[j]);
for(j=0;j<n;j++)
p[j]=p[j]*p(xx,state[j])/sum;
}
for(i=0;i<m;i++)
{sum=0;
for(j=0;j<n;j++)
sum+=p[j]*a[i][j];
e[i]=sum;
}
decision=e[0];
index=1;
if(type==0)
for(i=1;i<m;i++)
{ if(decision<e[i])
{decision=e[i];
index=i+1;
}
}
else
for(i=0;i<m;i++)
if(decision>e[i])
{decision=e[i];
index=i+1;
}
printf("\n**********\n");
printf("Results:");
printf("\n**********\n");
printf("expected loss for each course of action based on prior distribution\n");
for(i=0;i<m;i++)
printf("%f\t",e[i]);
printf("\n\nThe optimum expected loss is %f ",decision);
printf("\n\nchoose action A( %d )",index);
return;
}

void main(){
clrscr();
decision();
getch();
}

(4)dp_invest:动态规划的投资问题
#include <stdio.h>

int istar[10];

float dp_invest(int N,int K){
int i,j,sum,z,d[10][50];
float g[10][50],f[10][50];
printf("\nThe return function values as follows!\n");
for(j=0;j<K+1;j++)
printf("\t%d",j);
for(i=0;i<N;i++)
{printf("\ntask%d:\t",i+1);
for(j=0;j<K+1;j++)
scanf("%f",&g[i][j]);
}
for(i=0;i<N;i++)
for(j=0;j<K+1;j++)
{f[i][j]=0;d[i][j]=0;}
for(j=0;j<K+1;j++)
{f[N-1][j]=g[N-1][j];
d[N-1][j]=j;
}
for(i=N-2;i>=0;i--)
for(j=1;j<K+1;j++)
{f[i][j]=g[i][0]+f[i+1][j];
d[i][j]=0;
for(z=1;z<=j;z++)
if((g[i][z]+f[i+1][j-z])>f[i][j])
{f[i][j]=g[i][z]+f[i+1][j-z];
d[i][j]=z;
}
}
istar[0]=d[0][K];
for(i=1;i<N;i++)
{sum=0;
for(j=0;j<=i-1;j++)
sum=sum+istar[j];
istar[i]=d[i][K-sum];
}
return f[0][K];
}

void main(){
int i,N,K;
clrscr();
printf("WELCOME TO THE DYNAMIC_INVEST SYSTEM!\n\n");
printf("How many tasks ? ");
scanf("%d",&N);
printf("\nHow many units of materials ? ");
scanf("%d",&K);
printf("\nThe optimal return is %f",dp_invest(N,K));
for(i=0;i<N;i++)
printf("\nThe optimal amout to invest in task %d is %d",i+1,istar[i]);
getch();
}

(5)dp_plan:生产计划算法
#include <stdio.h>

#define pc(j) 20+5*j
#define e(j) j

void dp_plan(){
int i,j,k,sum,limit,n,io,max_stock,max_production,d[10],x[10][100],z,z_maxlimit,z_minlimit,xstar[10];
float f[10][100],temp;
printf("WELCOME TO THE DYNIMIC SYSTEM!\n\nperiods of the inventory =? ");
scanf("%d",&n);
printf("\nthe maximum stocks of each period=? ");
scanf("%d",&max_stock);
printf("\nthe maximum production of each period=? ");
scanf("%d",&max_production);
printf("\nThe stocks of the first period=? ");
scanf("%d",&io);
for(i=0;i<n;i++)
{printf("Demand for period %d is ",i+1);
scanf("%d",&d[i]);
}
for(k=0;k<d[n-1];k++)
{x[n-1][k]=d[n-1]-k;
f[n-1][k]=pc(x[n-1][k]);
}
f[n-1][d[n-1]]=0;
x[n-1][d[n-1]]=0;
for(i=n-2;i>=0;i--)
{sum=0;
for(j=i;j<n;j++)
sum+=d[j];
if(sum<max_stock)
limit=sum;
else
limit=max_stock;
for(k=0;k<=limit;k++)
{if(d[i]-k>0)
{z_minlimit=d[i]-k;
f[i][k]=pc(z_minlimit)+e(0)+f[i+1][0];
x[i][k]=z_minlimit;
}
else
{z_minlimit=0;
f[i][k]=e(k-d[i])+f[i+1][k-d[i]];
x[i][k]=0;
}
if(sum-k>max_stock+d[i]-k)
if(max_stock+d[i]-k>max_production)
z_maxlimit=max_production;
else
z_maxlimit=max_stock+d[i]-k;
else
if(sum-k>max_production)
z_maxlimit=max_production;
else
z_maxlimit=sum-k;
for(z=z_minlimit;z<=z_maxlimit;z++)
{temp=pc(z)+e(k+z-d[i])+f[i+1][k+z-d[i]];
if(f[i][k]>temp)
{f[i][k]=temp;
x[i][k]=z;
}
}
}
}
/* for(i=0;i<n;i++)
{printf("\n\nthe period %d\n",i+1);
for(j=0;j<=5;j++)
printf("\t%f--->%d\t",f[i][j],x[i][j]);
getch();

}*/
printf("\n\nThe minimum policy cost for the %d periods is %f",n,f[0][io]);
xstar[0]=x[0][io];
j=io;
printf("\n\nThe optimal amount to produce in period 1 is %d",xstar[0]);
for(i=1;i<n;i++)
{xstar[i]=x[i][xstar[i-1]-d[i-1]+j];
printf("\nThe optimal amount to produce in period %d is %d",i+1,xstar[i]);
j=xstar[i-1]-d[i-1]+j;
}
}

void main(){
clrscr();
dp_plan();
getch();
}

(6)linear:线性规划单纯形算法
#include <stdio.h>

int K,M,N,Q=100,Type,Get,Let,Et,Code[50],XB[50],IA,IAA[50],Indexg,Indexl,Indexe;
float Sum,A[50][50],B[50],C[50];

void initiate();
void solve();

void main(){
int i,j;
clrscr();
/****** input data ******/
printf("\nWelcome to the linear programming solution!\n\n********\nNotice 1!\n********\nIf the type of objective equation is 'max' ,please enter '1'!\nElse please enter '0'!\n\n\n");
printf("********\nNotice 2!\n********\nDefine the type of subjective equation as following:\n'<='is equal to '0'!\n'>=' is equal to '1'!\n'='is equal to '2'!\n");
printf("\n\nPlease input the coefficients or the constants:\n");
printf("THe number of subjective equations ? ");
scanf("%d",&M);
printf("THe number of variables ? ");
scanf("%d",&K);
printf("THe number of ' <= 'subjective equations ? ");
scanf("%d",&Let);
printf("THe number of ' >= 'subjective equations ? ");
scanf("%d",&Get);
printf("THe number of ' = 'subjective equations ? ");
scanf("%d",&Et);
printf("THe type of objective equation ? ");
scanf("%d",&Type);
N=K+Let+Et+2*Get;
for(i=0;i<M;i++)
{printf("Please input %d's equation:\n",i+1);
printf("type ? ");
scanf("%d",&Code[i]);
printf("constant ? ");
scanf("%f",&B[i]);
for(j=0;j<K;j++)
{printf("coefficient ? ");
scanf("%f",&A[i][j]);
}
}
printf("Plese input the constants of the object equation:\n");
for(j=0;j<K;j++)
scanf("%f",&C[j]);
printf("\n\n*************************************\nPlease check the data you just input!\n************************************* \n");
getch();
if(Type)
printf("The type of the object equation is 'max'\n");
else
printf("The type of the object equation is 'min'\n");
printf("\nThe object equation is :\n");
for(j=0;j<K;j++)
{printf("(%f)X%d ",C[j],j+1);
if(j!=K-1)
printf("+");
}
printf("\n\nThe suject equation is :");
for(i=0;i<M;i++)
{printf("\nNumber %d suject equation is: %d %f ",i+1,Code[i],B[i]);
for(j=0;j<K;j++)
{printf("(%f)X%d ",A[i][j],j+1);
if(j!=K-1)
printf("+");
}
}
/****** initiate data ******/
initiate();
solve();
if(!Type)
A[M][N]=-A[M][N];
printf("\n\nThe optimal value of the original objective function is: %f ",A[M][N]);
getch();
}


/****** initiate variables function ******/
void initiate(){
int i,j;
Indexg=K;
Indexl=Indexg+Get;
Indexe=Indexl+Let;;
for(i=0;i<M+1;i++)
for(j=K;j<N+1;j++)
A[i][j]=0;
for(i=0;i<M;i++)
A[i][N]=B[i];
for(i=0;i<M;i++)
switch(Code[i])
{ case 0: {XB[i]=Indexl;
A[i][Indexl++]=1;
break;
};
case 1: {XB[i]=Indexe;
IAA[IA++]=i;
A[i][Indexe++]=1;
A[i][Indexg++]=-1;
break;
};
case 2: {XB[i]=Indexe;
IAA[IA++]=i;
A[i][Indexe++]=1;
break;
};
}
for(j=0;j<K;j++)
if(Type)
A[M][j]=-C[j];
else
A[M][j]=C[j];
for(j=K;j<=N;j++)
A[M][j]=0;
for(j=K+Get+Let;j<N;j++)
A[M][j]=Q;
Sum=0;
for(j=0;j<=N;j++)
{Sum=0;
for(i=0;i<IA;i++)
Sum=Sum+A[IAA[i]][j];
A[M][j]=A[M][j]-Sum*Q;
}
return;
}

/****** process data function ******/
void solve(){
int i,j,mark=1,minus,minusmark,basic=0,divide,dividemark;
float H,P;
while(1)
{mark=0;
minusmark=0;
minus=0;divide=1000;
dividemark=0;
printf("\n\nBasic solution %d is\n",++basic);
for(i=0;i<M;i++)
printf("Basic variable %d = X( %d )= %f\n",i+1,XB[i]+1,A[i][N]);
printf("\nCurrent value of the object equation is: %f\n",A[M][N]);
getch();
for(j=0;j<N;j++)
{
if(A[M][j]<-6e-8)
mark++;

if(A[M][j]<minus)
{minus=A[M][j];
minusmark=j;
}
}
if(mark==0)
break;
for(i=0;i<M;i++)
{if(A[i][minusmark]==0)
continue;
if(A[i][N]/A[i][minusmark]<=0)
continue;
if(A[i][N]/A[i][minusmark]<divide)
{divide=A[i][N]/A[i][minusmark];
dividemark=i;
}
}
XB[dividemark]=minusmark;
if(divide<0)
printf("There is no solution because of no boundary!");
P=A[dividemark][minusmark];
for(j=0;j<N+1;j++)
A[dividemark][j]=A[dividemark][j]/P;
for(i=0;i<M+1;i++)
{H=A[i][minusmark];
if(i==dividemark)
continue;
for(j=0;j<N+1;j++)
A[i][j]=A[i][j]-H*A[dividemark][j];
}
}
printf("\n\n*************************************\nThe last basic solution is optimal!\n************************************* \n");
return;
}

关键词: 基本数学方法 程序实现
文章推荐
开发学生资源优化数学教学
“四主·三段·六环节”中学数
问题是数学课堂教学的灵魂
谈谈如何构建初中数学高效课堂
毕业论文撰写规范——仅供参考
名师课堂管理的66个经典细节
讲好公开课的注意事项——与省
中学数学教学的基本原则及其教
浅谈中小学专题学习网站的建立
营造有效的数学课堂:扎实而有
常用基本数学方法程序实现
教学质量评估的定量比较评价模
评价在数学教育中的重要性
学生解答数学问题的心理障碍及
初中数学学习习惯指导及量化表
数学课堂学习指导程式_启导自
乱班的处理方法
试题讲解方法
初一数学暑假作业——A组适用
让数学学习成为学生经历的过程
全站文章
近代饶氏名人
中外著名数学家资料集
如何设置局域网IP
常见无线路由器默认密码大全与
也谈笔记本电脑的无线网卡开关
云阳各大中小餐厅及酒楼联系方
视频: 重庆市云阳
Flash软件最大16000
动易网站系统用户权限设置问题
郑观应《盛世危言》
关于虫儿飞 天景初中登录 虫儿飞用户登录 网址导航
Copyright 2005-2019©cqraojun.cn All Rights Reserved.
设计制作:虫儿飞 备案号:渝ICP备12001302号-1
客户端IP:107.23.37.199,当前访问人次:3092