- #36
phizo
- 48
- 1
Code:
#include <stdio.h>
#include <math.h>
main(argc,argv)
int argc;
char *argv[]; {
// y = ax2 + bx + c // (1,6) (7,8)(10,2)
float y1,y2,y3,x1,x2,x3, sa,sb,sc, sae,sbe,sce, na,nb,nc;
float stepa, stepb, stepc, ea, oldea, a, b, c, e1, e2, e3, et;
int c1;
c1=0;
x1=2;y1=0;
x2=-2; y2=0;
x3=5; y3=4;
oldea=10000000;
sa=sb=sc=-100;
sae=sbe=sce=100;
stepa=1;
stepb=1;
stepc=1;
while(c1++<6){
oldea=10000000;
for(a=sa; a<sae; a+=stepa){
for(b=sb; b<sbe; b+=stepb){
for(c=sc; c<sce; c+=stepc){
e1 = a*x1*x1 + b*x1 +c -y1;
e2 = a*x2*x2 + b*x2 +c -y2;
e3 = a*x3*x3 + b*x3 +c -y3;
et = fabs(e1) + fabs(e2) + fabs(e3);
ea=fabs(et);
// printf("ea %-08.2f et %-08.2f oldea %-08.2f *** ",ea,et,oldea); //fflush(stdout);
if (ea< oldea) {
oldea=ea;
na=a; nb=b; nc=c; //n=new, so i am saving the nearest guess
}
// printf("sa %-08.2f sb %-08.2f sc %-08.2f \n",a,b,c);
// printf(" best guess a=%-8.2f b= %-8.2f c=%-8.2f", na,nb,nc);
}
}
}
// printf("\n**** a=%f b= %f c=%f", na,nb,nc);
sa=na-(stepa/(float)1);
sb=nb-(stepb/(float)1);
sc=nc-(stepc/(float)1);
sae=na+(stepa/(float)1);
sbe=nb+(stepb/(float)1);
sce=nc+(stepc/(float)1);
stepa=(float)(sae-sa)/30;
stepb=(float)(sbe-sb)/30;
stepc=(float)(sce-sc)/30;
printf("\nNEW RUN %d ", c1);
printf(" New sa=%f b= %f c=%f ends sa=%f b= %f c=%f ", sa,sb,sc, sae, sbe, sce);
}
}