PLEASE HELP W C PROGRAMMING :D Thanks. I need to... Develop a C-code to perform
ID: 3634807 • Letter: P
Question
PLEASE HELP W C PROGRAMMING :D Thanks.I need to...
Develop a C-code to perform the least square linear curve fit and statistical analysis of the following raw data set for the viscosity as a function of temperature
x=Temperature (K) y=Viscosity (m2/sec)
274 2.5 x 10-3
293 5.5 x 10-4
313 1.0 x 10-4
333 5.0 x 10-5
353 2.0 x 10-5
373 1.2 x 10-5
393 6.0 x 10-6
The code will perform the following function:
find fit: yhat = ae^(b/x), by linear regression
I have written a code to do this I think...but I need to solve the problem or write the code this way...
The variable replacement is used:
y ^=ae^((b/x ^ ) )
log?(y ^ )=log?(a)+b/x ^
We can let
p ^=1/x ^ ; q ^=log?(y ^ ); c=log?(a)
Then the function becomes
q ^=b* ?p ^ +c
The constants b and c can be obtained by linear regression, then a=exp(c)
So what we need to do is create two extra arrays p[] and q[], and calculate the coefficient by linear regression
here's what i tried to write:
// Testing_Final.cpp : Defines the entry point for the console application.//
#include <stdio.h>
#include <math.h>
#define SIZE 7
#define E 2.78281828
double b( double *a, double *c );
double a1( double *a, double *c );
union number {
char *c;
};
int main()
{
union number item;
int n;
double q=0;
double x[SIZE] = {274, 293, 313, 333, 353, 373, 393};
double y[SIZE] = {2.5e-3, 5.5e-4, 1.0e-4, 5.0e-5, 2.0e-5, 1.2e-5, 6.0e-6};
double sol = 0, yf = 0, yd = 0, xy = 0, x2 = 0, std = 0, skew = 0, skewco = 0, k = 0, ym = 0, e = 0, f = 0;
double *xPtr;
xPtr = x;
double *yPtr;
yPtr = y;
FILE *data;
item.c = "------------------------------------------------------------------------------- ";
printf("x=Temp(K) y=Visc(m^2/s) x^2 xy y_hat y_hat - y");
printf(" %s",item.c);
for (n = 0; n < SIZE; n++){
xy = y[n] * x[n];
x2 = x[n] * x[n];
sol = (a1( yPtr, xPtr ))*(pow(E,(b( yPtr, xPtr ))/x[n]));
yf = sol - y[n];
yd = yd + yf * yf;
k = k + yf * yf * yf;
q = q + (yf * yf);
printf( "%4.f%16.8f%20.3f%10.3f%15.8f%14.8f ", x[n], y[n], x2, xy, sol, yf );
}
printf("%s ",item.c);
printf(" Summation of (y_hat-y)^2 = %.25f ", q);
std = pow( yd / (SIZE - 1), 0.5);
skew = k / (SIZE - 1);
skewco = skew / (pow( std, 3));
printf("The Standard Deviation is: %.5f The Skewness is: %.25f The Skewness Coefficient is: %.3f ", std, skew, skewco );
printf("a = %f, b = %f Therefore the Equation is: y = %lf + (%lf)x ", b( yPtr, xPtr ) , a1( yPtr, xPtr ) , b( yPtr, xPtr ), a1( yPtr, xPtr ));
if ( ( data = fopen( "data.txt", "w" ) ) == NULL ) {
printf( "File could not be opened " ); }
else {
fprintf( data, "x=Temp(K) y=Visc(m^2/s) x^2 xy y_hat y_hat - y ");
fprintf( data, "----------------------------------------------------------------------------- ");
std = pow( yd / ( SIZE - 1 ), 0.5 );
skew = k / ( SIZE - 1 );
skewco = skew / ( pow( std, 3 ) );
fprintf( data, "a = %f, b + %f Therefore the Equation is: y = %1f + (%1f)x ", b( yPtr, xPtr ), a1( yPtr, xPtr ), b( yPtr, xPtr ), a1( yPtr, xPtr ));
}
fclose( data );
return 0;
}
double b( double *y, double *x )
{
int n;
double sumy = 0, sumx2 = 0, sumx = 0, sumxy = 0, ttotal = 0, btotal = 0, total = 0;
for ( n = 0; n < SIZE; n++ )
{
sumy = sumy + y [n];
sumx2 = sumx2 + x[n] * x[n];
sumx = sumx + x[n];
sumxy = sumxy + y[n] * x[n];
}
ttotal = sumy * sumx2 - sumx * sumxy;
btotal = n * sumx2 - sumx * sumx;
total = ttotal / btotal;
return total;
}
double a1( double *y, double *x )
{
int n;
double sumy = 0, sumx2 = 0, sumx = 0, sumxy = 0, ttotal = 0, btotal = 0, b2 = 0;
for ( n = 0; n < SIZE; n++ )
{
sumy = sumy + y[n];
sumx2 = sumx2 + x[n] * x[n];
sumx = sumx + x[n];
sumxy = sumxy + x[n] * y[n];
}
ttotal = n * sumxy - sumx * sumy;
btotal = n * sumx2 - sumx * sumx;
b2 = ttotal / btotal;
return b2;
}
THANK YOU IN ADVANCED!
Explanation / Answer
/* Write C program to implement the linear regression algorithm. */ #include #include #include #include float mean(float *a, int n); void deviation(float *a, float mean, int n, float *d, float *S); void main() { float a[20],b[20],dx[20],dy[20]; float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0; float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0; char type_coff[7]; int n=0,i=0; clrscr(); printf(“Enter the value of n: “); scanf(“%d”,&n); printf(“Enter the values of x and y: ”); for(i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.