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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define M 10 /* maximum row */
#define N 10 /* maximum column */
int gauss(int m,int n,double a[][N],double x[]);
void print_matrix1(int m, double matrix[]);
void print_matrix2(int m, int n, double matrix[][N]);
int main()
{
double x[] = {1.5, 1.6, 1.7, 1.8, 1.9, 2.0};
double y[] = {0.0668, 0.0538, 0.0456, 0.0349, 0.0297, 0.022};
double z[N];
double v0 =0;
double v1 =0;
double v2 =0;
double v3 =0;
double v4 =0;
double t0 =0;
double t1 =0;
double t2 =0;
// double yhat;
FILE *file3;
int n=6;
int l;
for ( l=0; l<n; l++)
{
v0++;
v1 = v1 + x[l];
v2 = v2 + pow(x[l],2);
v3 = v3 + pow(x[l],3);
v4 = v4 + pow(x[l],4);
t0 = t0 + y[l];
t1 = t1 + x[l]*y[l];
t2 = t2 + (x[l]*x[l])*y[l];
}
double a[M][N] = {{v0, v1, v2, t0}, {v1, v2, v3, t1}, {v2, v3, v4, t2}};
int n_row = 3, n_column = 4, return_val;
printf("matrix A_C:\n");
print_matrix2( n_row, n_column, a );
return_val = gauss( n_row, n_column, a, x );
printf("matrix X:\n");
print_matrix1( n_row, x );
printf("Quad Regresion = %fx^2+%fx+%f\n",x[2],x[1],x[0]);
///////START\\\\\\\\\
//Solve for Mean of Y
double r1, r, meany, yhat, xhat;
double s =0;
double s0 =0;
int i;
meany=t0/n;
//Solve So
for(int i=0; i<n; i++)
{
s0+=pow((y[i]-meany),2);
}
//Solve S
for(i=0; i<n; i++)
{
yhat=x[2]+ x[1]*x[i] + x[0] *pow(x[i],2);
s+=pow( y[i] -yhat , 2);
}
//Solve for Corelation Coeffice
r1=(s0-s)/s0;
r=sqrt(r1);
printf("\nThe Corelation Coefft is: r= %f\n", r);
file3=fopen("Data2.txt", "w");
for(i=0; i<71; i++)
{
xhat= .01*i + 1.4;
yhat= x[2] + x[1]*xhat + x[0] *pow(xhat,2);
fprintf(file3, "(%.3f , %f)\n", xhat, yhat);
}
fclose(file3);
return 0;
}
/////END\\\\\\
int gauss(int m, int n, double a[][N], double x[])
{
int i, j, k;
/*** forward elimination ***/
for( j = 0; j < n-1; j++ )
{
for( k = j+1; k < n; k++ )
{
a[j][k] = a[j][k] / a[j][j];
}
a[j][j] = 1.0;
for( i = j+1; i < m; i++ )
{
for( k = j+1; k < n; k++ )
{
a[i][k] -= a[i][j] * a[j][k];
}
a[i][j] = 0.0;
}
}
print_matrix2( m, n, a );
/*** back substitution ***/
x[m-1] = a[m-1][m];
for( i = m-2; i >= 0; i-- )
{
x[i] = a[i][m];
for( j = m-1; j > i; j-- )
{
x[i] -= a[i][j] * x[j];
a[i][j] = 0;
}
a[i][m] = x[i];
}
print_matrix2( m, n, a );
return 0;
}
void print_matrix1(int m, double matrix[])
{
int i;
for( i = 0; i < m; i++ )
{
printf("%12.4f", matrix[i]);
printf("\n");
}
printf("\n");
getchar();
}
void print_matrix2(int m, int n, double matrix[][N])
{
int i, j;
for( i = 0; i < m; i++ )
{
for( j = 0; j < n; j++ )
{
printf("%12.4f", matrix[i][j]);
}
printf("\n");
}
printf("\n");
getchar();
for (int j=0;j<n;j++)
return 0;
}
| |