Sunday 18 August 2019

C program for least square fitting of aexp(bx)


/*least square fitting of aexp(bx) */
#include<stdio.h>
#include<math.h>
void
main()
{
int n,i;
float a,b,x[20],y[20],x1[20],y1[20],sx=0.0,sxx=0.0,sy=0.0,sxy=0.0,c,m,e=2.71828;
printf("enter the total number of data:");
scanf("%d",&n);
printf("enter the value of x:");
for(i=0;i<n;i++)
{
                scanf("%f",&x[i]);
}
printf("enter the values of y:");
for(i=0;i<n;i++)
{
                scanf("%f",&y[i]);
}
for(i=0;i<n;i++)
{ x1[i]=x[i];
y1[i]=log(y[i]);
                sx=sx+x1[i];
                sxx=sxx+(x1[i]*x1[i]);
                sy=sy+y1[i];
                sxy=sxy+(x1[i]*y1[i]);
}
m=(n*sxy-sx*sy)/(n*sxx-sx*sx);
c=(sxx*sy-sx*sxy)/(n*sxx-sx*sx);
a=pow(e,c);
b=m;
printf("the line of least fit is:\n y=%fe^(%fx)\n",a,b);
}
RESULT:
enter the total number of data:5
enter the value of x:1 2 3 4 5
enter the values of y:70 259 920 3340 12000
the line of least fit is:
 y=19.563120e^(1.284523x)
Press any key to continue

No comments:

Post a Comment

Thanks for comment.