/* LHREM.C
Main program to predect the temperature coefficients of a
temperature compensated
rare earth permanent maget.
S. Liu, University of Daton
May 8, 1988. Re-wrote October 29, 1990.
Add functions together,
October 31, 1990
*/
#define MAX 12
/* MAX-2 is the maximum degree of polynomium */
#define PNT 300
/* MAX is the maximum number of linear equations */
/* PNT is the maximum number of data
points */
#include<stdio.h>
#include<math.h>
#include<slgssd1.c>
#include<slclbr2.c>
void sllssqft(void);
void sladplnm(void);
void sltmpcef(void);
int
i, j, k;
/* i,j,k serve only as array indexes in
functions */
main()
{
int
c;
system("cls");
do {
printf("\n\n1.
Least square fitting.");
printf("\n\n2.
Add polynomials.");
printf("\n\n3.
Deternime temperature coefficients.");
printf("\n\n4.
Quit.\n");
printf("\n\n Your choice (1-4) ");
scanf("%d",
&c);
} while ((c<1)||(c>4));
if (c==1) sllssqft();
if (c==2) sladplnm();
if (c==3) sltmpcef();
printf("\n\n Good bye !!");
}
/* Function : SLLSSQFT.C
A program to determine
coefficients of mth- degree polynomial of
least square fitting
S. Liu, University of
Dayton. Jan. 15, 1987
Modiffied
in May 1988. Re-wrote in C in Sep.- Oct.
1990.
*/
void sllssqft(void)
{
void etdt(int *p, float x[], float
y[]);
void rddt(int *q, float x[], float
y[]);
void sllsft(int n, float x[], float
y[], int *pm, float xx[]);
void mltchls(int n, float x[], float
y[], int m, float xx[]);
void wtceft(int m, float xx[]);
int
n, m;
float x[PNT],
y[PNT], xx[MAX];
char c;
system("cls");
do {
printf("1.
Enter datat from the keyboard.\n\n");
printf("2. Read
data from the disc.\n\n\n");
printf(" Your choice: (1 or 2)\n\n");
scanf("%c",
&c);
} while ((c!='1') && (c!='2'));
if (c=='1') etdt(&n, x, y);
else rddt(&n, x, y);
sllsft(n,
x, y, &m, xx);
mltchls(n,
x, y, m, xx);
}
void etdt(int *p, float x[], float
y[])
{
i=0;
printf("\n\n Enter your data point one by one. The maximum data\n");
printf("point
should be less than %d. When finish,
enter 999 for\n",
PNT);
printf("both
X and Y values.\n\n");
do {
printf("\nEnter X ( %d ) = ", i);
scanf("%f",
&x[i]);
printf("\nEnter Y ( %d ) = ", i);
scanf("%f",
&y[i]);
++i;
} while ((x[i-1]!=999) || (y[i-1] != 999));
*p = i-1; /*
(*p) --> n will be the number
of data points */
}
void rddt(int *q, float x[], float
y[])
{
FILE *fp;
int
i=0;
char flnm[13];
printf("\nEnter the file name to be read.\n");
scanf("%s",
&flnm);
if ((fp=fopen(flnm,
"r"))==NULL) {
printf("Cannot
open file for reading.");
exit(1);
}
while(!feof(fp)) {
fscanf(fp, "%f%f", &x[i], &y[i]);
i++;
}
*q=i;
fclose(fp);
for (i=0; i<*q; i++)
printf("\nx(%d) =
%f, y(%d) = %f ", i, x[i], i,
y[i]);
}
void mltchls(int n, float x[], float
y[], int m, float xx[])
{
int
c;
printf("\n\nType any key to continue.\n\n");
getch();
do {
printf("\n1.
Change the degree of polynomial, m.\n\n");
printf("2.
Write the coefficients to the disc.\n\n");
printf("3.
Re-run this program.\n\n");
printf("4.
Return to the main program.\n\n");
printf("5.
Quit.\n\n\n");
printf(" Your choice (1-5) ");
scanf("%d",
&c); /* if c is of the type char, then the */
} while ((c<1) || (c>5)); /*
program will not run properly.
*/
if (c==1) {
sllsft(n,
x, y, &m, xx);
mltchls(n,
x, y, m, xx);
}
if (c==2) {
wtceft(m,
xx);
mltchls(n,
x, y, m, xx);
}
if (c==3) sllssqft();
if (c==4) main();
}
void wtceft(int m, float xx[])
{
FILE *fp;
char flnm[13];
printf("\n\nEnter the filename to be written. (e.g.
B:FILENAME.PLN)\n");
scanf("%s",
&flnm);
if ((fp=fopen(flnm,
"w"))==NULL) {
printf("Cannot
open file for writing.\n");
exit(1);
}
for (i=1; i<=m+1; i++) {
printf(" %f ",
xx[i]);
fprintf(fp, " %f ", xx[i]);
}
printf("\n\nWriting finished.\n\n");
fclose(fp);
}
/* Function : SLADPLNM
A program to add k
polynomials
S. Liu, University of Dayton
January 1, 1990,
re-wrote in C, October 25, 1990
*/
void sladplnm()
{
void etpct(int n, float x[]);
void mltchad(int n, int m, float x[], float coef[][MAX],
float scoef[]);
void svcef(int n, int
m, float x[], float coef[][MAX], float scoef[]);
void exmp(int m, float scoef[]);
void adplnm(int n, int
m, float x[], float coef[][MAX], float scoef[]);
FILE *fp[MAX];
int
n, m, max, tmn[MAX];
float x[MAX], coef[MAX][MAX], scoef[MAX];
char plnm[10][13];
system("cls");
/* Initialize matrix coef[][] */
for (i=0; i<MAX; i++)
for (j=0; j<MAX;
j++) coef[i][j]=0;
/* Enter the
filenames of polynomials */
printf("Enter
the number of the original alloys, n.\n\n");
scanf("%d",
&n);
printf("Enter
the filenames of the corresponding polynomials one by one.\n\n");
for (i=0; i<n; i++) {
printf("Enter
the filename of No.%d polynomial, please. ", i+1);
scanf("%s",
&plnm[i]);
}
/* Read coefficients
of n polynomials */
for (i=0; i<n; i++) {
if ((fp[i]=fopen(plnm[i],
"r"))==NULL) {
printf("Cannot
open file %s for reading.", plnm[i]);
exit(1);
}
tmn[i]=0;
j=0;
while (!feof(fp[i])) {
fscanf(fp[i], "%f", &coef[i][j]);
j++;
tmn[i]++;
}
fclose(fp[i]);
}
max=tmn[0];
for (i=1; i<n; i++) if (tmn[i] > max) max=tmn[i];
m=max-1; /* m will be the degree of the
resulted polynomial */
printf("\n m = %d", m);
etpct(n,
x); /* Enter
percentages of each original alloy */
adplnm(n,
m, x, coef, scoef);/* Add k
polynomials */
mltchad(n,
m, x, coef, scoef); /* Multy
choices */
}
void etpct(int n, float x[]) /* Enter the percentage for each original
alloys */
{
float sx=0;
printf("\n\nEnter the atomic percentage for each original alloy one by
one.\n\n");
for (i=0; i<n; i++) {
printf("\nEnter the atomic percentage for No.%d
alloy.\n\n", i+1);
scanf("%f",
&x[i]);
sx
+= x[i];
}
if (abs(100-sx) >
0.1) {
printf("The
total does not equal 100%, please re-enter.");
etpct(n,
x);
}
}
void adplnm(int n, int
m, float x[], float coef[][MAX], float scoef[])
{
for (j=0; j<=m;
j++) scoef[j]=0;
for (j=0; j<=m;
j++) {
for (i=0; i<n; i++) scoef[j] += x[i] / 100 * coef[i][j];
printf("\nscoef(%d) = %f", j, scoef[j]);
}
}
void mltchad(int n, int m, float x[], float coef[][MAX],
float scoef[])
{
int
c;
printf("\n\nType any key to continue.\n\n");
getch();
do {
printf("1.
Save the resulted coefficients on to disc.\n\n");
printf("2. Change
the percentage og the original alloys.\n\n");
printf("3.
See the example of the temperature dependence of the resulted
alloy.\n\n");
printf("4.
Re-run the present program.\n\n");
printf("5.
Return to the main program.\n\n");
printf("6.
Quit.\n\n");
scanf("%d",
&c);
} while ((c<1)||(c>6));
if (c==1) {
svcef(n,
m, x, coef, scoef);
mltchad(n,
m, x, coef, scoef);
}
if (c==2) {
etpct(n,
x);
adplnm(n,
m, x, coef, scoef);
mltchad(n,
m, x, coef, scoef);
}
if (c==3) {
exmp(m,
scoef);
mltchad(n,
m, x, coef, scoef);
}
if (c==4) sladplnm();
if (c==5) main();
}
void svcef(int n, int
m, float x[], float coef[][MAX], float scoef[])
{
FILE *fp;
char flnm[13];
printf("\n\nEnter the filename to be written. (e.g.
B:FILENAME.PLN)\n");
scanf("%s",
&flnm);
if ((fp=fopen(flnm,
"a"))==NULL) {
printf("Cannot
open file for writing.\n");
mltchad(n,
m, x, coef, scoef);
}
for (i=0; i<=m; i++) {
printf(" %f
", scoef[i]);
fprintf(fp, " %f ", scoef[i]);
}
fclose(fp);
printf("\n\nWriting finished.\n\n");
}
void exmp(int m, float scoef[])
{
float t, t1, t2, itv, y;
printf("\n\nEnter the starting temperature.\n\n");
scanf("%f",
&t1);
printf("\nEnter the ending temperature.\n\n");
scanf("%f",
&t2);
printf("\nEnter the interval.\n\n");
scanf("%f",
&itv);
printf("\n\n T Y\n\n");
for (t=t1;
t<=(t2+itv/2); t+=itv) {
y=0;
if (t==0)
t=0.00000000001;
for (j=0; j<=m;
j++) y += scoef[j]
* pow(t, j);
printf("\n %5.1f %6.1f", t, y);
}
}
/* Function :SLTMPCEF.C
A function to determine
the temperature coefficients.
S. Liu, University of
Dayton.
May 19, 1988, Re-wrote
in C, October 25, 1990.
*/
void sltmpcef(void)
{
void mltchcf(int m, float scoef[]);
void tmpcef(int m, float scoef[]);
void avtmpcef(int m, float scoef[]);
FILE *fp;
int
m, tmn=0;
float scoef[MAX];
char flnm[13];
system("cls");
printf("Enter
the filename of the polynomial to be read.
");
scanf("%s",
&flnm);
if ((fp=fopen(flnm,
"r"))==NULL) {
printf("Cannot
open file %s for reading.", flnm);
exit(1);
}
i=0;
while (!feof(fp)) {
fscanf(fp, "%f", &scoef[i]);
i++;
tmn++;
}
fclose(fp);
m = tmn -1;
mltchcf(m,
scoef);
}
void mltchcf(int m, float scoef[])
{
int
c;
do {
printf("\n\n1.
Determine the temp. coef. at a give
temperature.");
printf("\n\n2.
Determine the temp. coef. in a temperature
interval.");
printf("\n\n3.
Re-run this program.");
printf("\n\n4.
Return to the main program.");
printf("\n\n5.
Quit.\n\n");
scanf("%d",
&c);
} while ((c<1)||(c>5));
if (c==1) {
tmpcef(m,
scoef);
mltchcf(m,
scoef);
}
if (c==2) {
avtmpcef(m,
scoef);
mltchcf(m,
scoef);
}
if (c==3) sltmpcef();
if (c==4) main();
}
void tmpcef(int m, float scoef[])
{
float t, mgn, tcef, summ=0,
sum=0;
printf("\n\nEnter the temperature.
");
scanf("%f",
&t);
if (t==0) t=0.00000000001;
for (j=0; j<=m;
j++) summ += scoef[j] * pow(t, j);
mgn
= summ;
/* Determine the
derivative of the polynomial at t */
for (i=0; i<=m; i++)
sum += i * scoef[i] * pow(t, (i-1));
tcef
= sum / mgn * 100;
printf("\n\nThe temp. coef. at %5.1f degree
is %f %/deg.\n\n", t, tcef);
}
void avtmpcef(int m, float scoef[])
{
float t[3], sum[MAX],
tcef;
printf("\n\nEnter the first temperature, T1. ");
scanf("%f",
&t[1]);
printf("\n\nEnter the second temperature, T2. ");
scanf("%f",
&t[2]);
for (i=1; i<=2; i++) {
if (t[i]==0) t[i]=0.00000000001;
sum[i] = 0;
for (j=0; j<=m;
j++) sum[i] +=
scoef[j] * pow(t[i], j);
}
tcef
= (sum[2] - sum[1]) / (t[2] - t[1]) / sum[1] * 100;
printf("\n\nThe temp. coef. between %f and
%f is %f %/deg.\n\n", t[1], t[2], tcef);
}
_