Curve Fitting of Type y=ab^x Using C


This article is implementation of algorithm Curve Fitting of Type y=abx Algorithm and pseudocode Curve Fitting of Type y=abx Pseudocode discussed earlier using C programming language. Output of program is also provided.

C Program for Fitting y=abx


#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, B;
 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 + x[i];
  sumX2 = sumX2 + x[i]*x[i];
  sumY = sumY + log(y[i]);
  sumXY = sumXY + 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 and B to b */
 a = exp(A);
 b = exp(B);
 
 /* Displaying value of a and b */
 printf("Values are: a=%0.2f and b = %0.2f",a,b);
 
 getch();
 return(0);
}

C Program Output for Fitting y=abx

How many data points?
5
x[1]=0
y[1]=2
x[2]=1
y[2]=7
x[3]=2
y[3]=11
x[4]=3
y[4]=26
x[5]=4
y[5]=50
Values are: a=2.44 and b = 2.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