///Curve fitting of a straight line of the form y = a + bx#include<stdio.h>#include<math.h>int main(){int ax[20], ay[20], i, n;int sum_x = 0, sum_xy = 0, sum_x2 = 0, sum_y = 0 ;float a , b;printf("Enter no of records: ");scanf("%d", &n);printf("Enter Data: ");printf("\nX Y\n");for(i = 0; i < n; i++){scanf("%d%d", &ax[i], &ay[i]);}for(i = 0; i < n; i++){sum_x += ax[i];sum_y += ay[i];sum_xy += ax[i] * ay[i];sum_x2 += pow(ax[i], 2);}b = (n*sum_xy - sum_x*sum_y)/(n*sum_x2 - pow(sum_x,2));a = (sum_y - b*sum_x)/n;printf("\na = %.3f b = %.3f", a, b);printf("\nThe Equation is %.3f + %.3fX\n\n", a, b);return 0;}

## Sunday, September 18, 2011

### Numerical Methods:Fitting the curve of the form y = a + bx using least square method in C

### About Author

**Bibek Subedi** is currently pursuing his Masters'degree in Computer Science at the University of Nevada, Las Vegas, USA. He has more than 4 years of experience in Software Development. He loves researching in the field of Algorithms, Parallel Computing, Big Data and Distributed Systems. He is a part time blogger and a bathroom singer (:D). If you didn't understand any part of the article, you can contact him or you can leave a comment in the comment box below.

Subscribe to:
Post Comments (Atom)

## No comments:

## Post a Comment