Curve Fitting of Type y=ax^b Using C


This article is implementation of pseudocode Curve Fitting of Type y=axb Pseudocode using C programming language.

C Program for Fitting y = axb


#include<stdio.h>
#include<conio.h>
#include<math.h>

#define S 50

int main()
{
 int n, i;
 float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0, a, b, A;
 clrscr();
 
 /* Input */
 printf("How many data points?\n");
 scanf("%d", &n);
 for(i=1;i<=n;i++)
 {
  printf("x[%d]=",i);
  scanf("%f", &x[i]);
  printf("y[%d]=",i);
  scanf("%f", &y[i]);
 }
 
 /* Calculating Required Sum */
 for(i=1;i<=n;i++)
 {
  sumX = sumX + log(x[i]);
  sumX2 = sumX2 + log(x[i])*log(x[i]);
  sumY = sumY + log(y[i]);
  sumXY = sumXY + log(x[i])*log(y[i]);
 }
 
 /* Calculating A and b */
 b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
 A = (sumY - b*sumX)/n;
 
 /* Transformation of A to a */
 a = exp(A);
 
 /* Displaying value of a and b */
 printf("Values are: a=%0.2f and b = %0.2f",a,b);
 
 getch();
 return(0);
}

Curve Fitting y = axb C Program Output

	
How many data points?
4
x[1]=61
y[1]=350
x[2]=26
y[2]=400
x[3]=7
y[3]=500
x[4]=2.6
y[4]=600
Values are: a=701.99 and b = -0.17

Recommended Readings

  1. Algorithm for fitting Curve y = axb
  2. Pseudocode for fitting y = axb
  3. C Program for fitting curve y = axb
  4. C++ Program for fitting curve y = axb
  5. Algorithm for fitting Curve y = abx
  6. Pseudocode for fitting y = abx
  7. C Program for fitting curve y = abx
  8. C++ Program for fitting curve y = abx