TP Intégration Numérique
 All Files Functions
essai04.c
Go to the documentation of this file.
1 
17 #include <math.h>
18 #include <string.h>
19 
20 #include "mesh1D.h"
21 #include "quad.h"
22 #include "utils.h"
23 
24 void usage(){
25  printf("NAME\n\tessai04 - see TP3.pdf\n");
26  printf("SYNOPSIS\n\tessai04 [FILENAME] [NINI PAS NFIN]\n");
27  printf("DESCRIPTION\n");
28  printf("\tFILENAME (optional) : name of the output filename\n");
29  printf("\tNINI PAS NFIN (optional) : three integers values\n");
30 }
31 
32 double f(double x){
33  return 8.*sqrt(x*(1-x));
34 }
35 
36 void GetMainParameters(int argc,char **argv,
37  char **pFileName,int *pini,int *ppas,int *pfin);
38 
39 void ComputeErrors(double (*pf)(double), double a, double b, double Exact,
40  int nini, int pas, int nfin,
41  double **ph, double **pError, int *n);
42 
43 
44 int main(int argc, char **argv){
45  int nini=100,pas=100,nfin=1000; /* default values*/
46  char *FileName=""; /* default value*/
47  double *ph,*pError;
48  int n;
49 
50  GetMainParameters(argc,argv,&FileName,&nini,&pas,&nfin);
51  ComputeErrors(sin,0.,M_PI,2,nini,pas,nfin,&ph,&pError,&n);
52  /* Student question : what happens when using the following call of ComputeErrors function
53  * instead of the previous one?
54  * ComputeErrors(f,0.,1.,2.,nini,pas,nfin,&ph,&pError,&n); */
55 
56  if (strlen(FileName)>0){
57  printf("Save results in file %s ... \n",FileName);
58  saveTwoDoubleDynArray(ph,pError,n,FileName,"%.16e %.16e");
59  printf("done.\n");
60  }
61  Free(ph);
62  Free(pError);
63  return EXIT_SUCCESS; // return 0
64 }
65 
66 void GetMainParameters(int argc,char **argv,
67  char **pFileName,int *pnini,int *ppas,int *pnfin){
68  switch(argc){
69  case 1:
70  break;
71  case 2:
72  *pFileName=argv[1];
73  break;
74  case 5:
75  *pFileName=argv[1];
76  *pnini=atoi(argv[2]);
77  *ppas=atoi(argv[3]);
78  *pnfin=atoi(argv[4]);
79  break;
80  default :
81  usage();
82  exit(EXIT_FAILURE);
83  }
84  if (strcmp(*pFileName,"help")==0){
85  usage();
86  exit(EXIT_FAILURE);
87  }
88 }
89 
90 void ComputeErrors(double (*pf)(double), double a, double b, double Exact,
91  int nini, int pas, int nfin,
92  double **ph, double **pError,int *pn){
93  int nb,i,N;
94  double val;
95  *pn=(nfin-nini)/pas+1;
96  *ph=(double *)Malloc((*pn)*sizeof(double));
97  *pError=(double *)Malloc((*pn)*sizeof(double));
98  for (i=0;i<*pn;i++)
99  (*pError)[i]=(*ph)[i]=1.0*i;
100  for (i=0;i<*pn;i++){
101  N=nini+i*pas;
102  val=quadPMAlloc(pf,a,b,N,&(*ph)[i]); /* Question for students : What is the difference beetween &(*ph)[i] and ph[i]? */
103  (*pError)[i]=fabs(val-Exact)/Exact;
104  printf("N=%8d, val=%.16lf, error=%.16e\n",N,val,(*pError)[i]);
105  }
106 }