1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <stdio.h>
#include <math.h>
#define MAXVALS 200
struct gas
{
float V, T, P, logV, logT;
};
void regression(int n, struct gas *gas, float *gradient, float *intercept)
{
float sumx=0.0, sumy=0.0, sumxx=0.0, sumxy=0.0, del;
struct gas *pgas;
/* Calculate sums */
for ( pgas=gas ; pgas<&gas[n] ; pgas++ )
{
sumx += pgas->logV;
sumy += pgas->logT;
sumxx += pow( pgas->logV , 2 );
sumxy += pgas->logV * pgas->logT;
}
/* Use sums to calculate the outputs */
del = n * sumxx - pow(sumx,2);
*gradient = (n * sumxy - sumx * sumy) / del;
*intercept = (sumxx * sumy - sumx * sumxy) / del;
}
int main()
{
struct gas gas[MAXVALS];
int i, n;
float gradient, intercept;
double gamma;
char validdata, filename[100];
FILE *data;
/* Choose data file to open */
while( validdata==0 )
{
printf("Enter input filename: ");
scanf("%s",filename);
data = fopen(filename,"r");
/* Return an error if the file cannot be opened */
if ( data==NULL )
{
printf("Cannot open data file\n\n");
fclose(data);
}
else
{
printf("\nFile opened\n\n");
validdata=1;
}
}
/* Skip first line of data file */
fseek( data , 24 , SEEK_SET );
/* Calculate the log of each value of T and V */
for (i=0 ; ! feof(data) && i<MAXVALS ; i++)
{
fscanf(data, "%f %f %f\n" , &gas[i].V, &gas[i].T, &gas[i].P);
gas[i].logV = log10(gas[i].V);
gas[i].logT = log10(gas[i].T);
}
n = i;
printf("%d data points\n\n",n);
printf("Volume \t %f \nPressure %f \nTemp \t %f\n\n", gas[2].V, gas[2].T, gas[2].P);
regression(n, gas, &gradient, &intercept);
printf("Gradient = %f \nIntercept = %f \n\n", gradient, intercept);
gamma = 1 - gradient;
printf("Adiabatic index = %f\n\n", gamma);
/* Return success message */
printf("Success!\n\n");
fclose(data);
system("pause");
return 1;
}
|