ffnajia
Messages postés
30
Date d'inscription
mercredi 12 avril 2006
Statut
Membre
Dernière intervention
17 avril 2007
26 sept. 2006 à 14:47
merci beaucoup shell
ca marche maintenant.
par contre j'ai un autre code ou je sais pas pourquoi il prend pas en compte de else, pour la ligne 70.
merci de m'aider ou de coriger ma fote
bizou
#include
#include<cmath>
using namespace std;
//***************déclaration des fonctions***********************************
double puis(double x, double y){
double z;
z=exp(y*log(x));
return z;
}
double fonct(double x, int i){
double y;
if(i==0) y=3*puis(x,0.3);
if(i==1) y=1;
if(i>1) y=0;
return y;
}
double uti(double x, int i){
double y;
if(i==0) y=log(x);
if(i==1) y=1./(x+.001);
return y;
}
double grad(double delta, double x, double z, int n, int j, double h){
double y;
y=-(h*exp((n-delta)*j*h))*x-z*h;
return y;
}
//******************************************************************************
int main(){
double delta,h,lambda,capifinal,utilite,ro,epsi,capi0,cd,cg,consom,cap,cap1,capp;
int nb,n,m,ok,iter,compter,number,Maxiter,Maxcompter,ctrl,aa;
//***********************déclaration des données*******************************
FILE* Ecriture = fopen("donnees_E","w");
fprintf(Ecriture,"%lf %lf %lf %lf %lf %lf %d %d %d %d",100000.,.8,1.,0.,0.01,0.01,100,8,1000,500);
fclose(Ecriture);
FILE* Lecture = fopen("donnees_E","r");
fscanf(Lecture,"%lf %lf %lf %lf %lf %lf %d %d %d %d",&capi0,&delta,&lambda,&capifinal,&ro,&epsi,&nb,&n,&Maxiter,&Maxcompter);
fclose(Lecture);
//*****************************************************************************
double* capi = new double [nb];
double* mu = new double [nb];
double** conso = new double* [Maxiter+1];
cg=0;cd=capi0;
capi[0]=capi0;
capp=0;
ctrl=0;
for(int i=0;i<Maxiter+1;i++) conso[i]=new double [nb];
compter=0;
cap1=capi[0];
h=1./(nb);
//***********************Dichotomie pour trouver la consommation initiale******
while(compter<Maxcompter and cap1-capifinal>epsi){
consom=(cg+cd)/2.;cap=capi[0];
for(int i=0;i<nb-1;i++){capi[i+1]=capi[i]+h*(fonct(capi[i],0)-n*capi[i]-consom);
if(capi[i+1]<=capifinal){ctrl=1;break;}
}
// for(int i=0;i<nb-1;i++)printf("%d,%lf\n",i,capi[i]);
// printf(" %lf %lf %lf\n",capi[3],capi[20],capi[99]);
if(ctrl=1){cd=consom;cap1=capi[0];}
else { cg=consom;ctrl=0;cap1=capi[nb-1];}
printf(" %lf %lf\n",cd,cg);
cin>>aa;
compter++;
}
//******************************************************************************
//********************Déscente de gradient**************************************
for(int i=0;i<nb;i++)conso[0][i]=cg;
FILE* fich = fopen("utilite_E","w");
iter=0;
while(iter<Maxiter){
for(int i=0;i<nb-1;i++) capi[i+1]=capi[i]+h*( fonct(capi[i],0)-n*capi[i]-conso[iter][i]);
// printf("%d\n",iter);
// printf("%lf\n",conso[iter][1]);
// printf("%d\n",iter);
if(capi[nb-1]>capifinal){
utilite=0.;
mu[nb-2]=2.*lambda*(capi[nb-1]-capifinal);
for(int i=2;i<nb-2;i++) mu[nb-(i+1)]=mu[nb-i]*(1+h*0.9*puis(capi[i],-0.7)-h*n);
for(int i=0;i<nb;i++) utilite+=h*exp((n-delta)*i*h)*uti(conso[iter][i],0);
// for(int i=0;i<nb;i++) printf("%d %lf %lf\n",i,capi[i],conso[iter][i]);
fprintf(fich,"\n%lf",utilite);
for(int i=0;i<nb;i++) conso[iter+1][i]=conso[iter][i]-ro*grad(delta,uti(conso[iter][i],1),mu[i],n,i,h);
}
else break;
iter++;
}
fclose(fich);
printf("i Capital Conso ");
printf("\n<< %d iterations de Dichotomie & %d iterations de Descente de Gradient >>\n\n",compter,iter);
//********************************************************************************
FILE* fich2 = fopen("consommation_E","w");
for(int j=0;j<nb;j++){
for(int i=0;i<Maxiter;i++)fprintf(fich2,"%lf ",conso[i][j]);
fprintf(fich2,"\n");
}
fclose(fich2);
FILE* fich3 = fopen("capital_E","w");
for(int j=0;j<nb;j++) fprintf(fich3,"%lf\n",capi[j]);
fclose(fich3);
//*********************Sorties graphiques******************************************
FILE* capsci = fopen("capital_E.sci","w");
fprintf(capsci,"x=read('capital_E',%d, 1);\n",nb);
fprintf(capsci,"xtitle("lambda=%lf ",lambda);
fprintf(capsci,"delta=%lf ",delta);
fprintf(capsci,"n=%d ",n);
fprintf(capsci,"capifinal=%lf ",capifinal);
fprintf(capsci,"ro=%lf ",ro);
fprintf(capsci,"capi[0]=%lf ",'t','?');\n" ,capi[0]);
fprintf(capsci,"v=[1:%d];\n",nb);
fprintf(capsci,"plot2d(v,x,[2,%d+1]);\n",nb);
fprintf(capsci,"legends(['CAPITAL'],2,5);\n");//Pour ajouter le titre
fclose(capsci);
FILE* utilisci = fopen("utilite_E.sci","w");
fprintf(utilisci,"x=read('utilite_E',%d,1);\n",iter);
fprintf(utilisci,"xbasc();\n");
fprintf(utilisci,"xtitle("lambda=%lf ",lambda);
fprintf(utilisci,"delta=%lf ",delta);
fprintf(utilisci,"n=%d ",n);
fprintf(utilisci,"capifinal=%lf ",capifinal);
fprintf(utilisci,"ro=%lf ",ro);
fprintf(utilisci,"capi[0]=%lf ",'t','?');\n" ,capi[0]);
fprintf(utilisci,"v=[1:%d];\n",iter);
fprintf(utilisci,"plot2d(v,x,3);\n");
fprintf(utilisci,"legends(['BIEN ÊTRE ECONOMIQUE'],3,5);\n");
fclose(utilisci);
FILE*consosci = fopen("consommation_E.sci","w");
fprintf(consosci,"x=read('consommation_E',%d,%d);\n",nb,iter);
fprintf(consosci,"xbasc();\n");
fprintf(consosci,"xtitle("lambda=%lf ",lambda);
fprintf(consosci,"delta=%lf",delta);
fprintf(consosci,"n=%d ",n);
fprintf(consosci,"capifinal=%lf ",capifinal);
fprintf(consosci,"ro=%lf ",ro);
fprintf(consosci,"capi[0]=%lf ",'t','?');\n" ,capi[0]);
fprintf(consosci,"for i=1:%d\n",iter);
fprintf(consosci,"y=x(:,i);\n");
fprintf(consosci,"v=[1:%d];\n",nb);
fprintf(consosci,"plot2d(v,y,i);\n");
fprintf(consosci,"end\n");
fprintf(consosci,"xset('color',2);plot2d(v,y,[-3:97]);\n");
fprintf(consosci,"legends(['CONSOMMATION OPTIMALE'],-3,5);\n");
fclose(consosci);
//*********************************************************************************
getchar();
return 0;
}
Merci à tous
Najia