0% found this document useful (0 votes)
12 views

C Parogramming

Uploaded by

salman ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C Parogramming

Uploaded by

salman ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DESCRIPTION:

 C program that uses the least squares approximation (also


known as Chi square minimization) to find the best fit line to a
series of data-points. Or in other words, the equation of a line
that best fits a given set of data.
EQUATION OF LINE:

where ‘m’ is the slope and ‘c’ is the intercept.


So we will need to determine these constants in the above
equation.
We will be using the Least Squares Method to achieve this
 C program that uses the least-squares approximation (also
known as Chi square minimization) to find the best fit line to a
series of data-points. Or in other words, the equation of a line
that best fits a given set of data.
The equation of a line is given by:

where ‘m’ is the slope and ‘c’ is the intercept.


So we will need to determine these constants in the above
equation.
We will be using the  to achieve

You can refer to this link for a detailed proof.


The code is pretty much easy to understand. If you still have
any doubts leave them in the comments section down below

PAROGRAMMING:
#include<stdio.h>
#include<math.h>
/*****
Function that calculates and returns the slope of the best fit line
Parameters:
N: no. of data-points
x[N]: array containing the x-axis points
y[N]: array containing the corresponding y-axis points
*****/
double slope(int N, double x[N], double y[N]){
    double m;
    int i;
    double sumXY=0;
    double sumX=0;
    double sumX2=0;
    double sumY=0;
    for(i=0;i<N;i++){
        sumXY=sumXY+x[i]*y[i];
        sumX=sumX+x[i];
        sumY=sumY+y[i];
        sumX2=sumX2+x[i]*x[i];
    }
    sumXY=sumXY/N;
    sumX=sumX/N;
    sumY=sumY/N;
    sumX2=sumX2/N;
    m=(sumXY-sumX*sumY)/(sumX2-sumX*sumX);
    return m;
}
/*****
Function that calculates and returns the intercept of the best fit line
Parameters:
N: no. of data-points
x[N]: array containing the x-axis points
y[N]: array containing the corresponding y-axis points
*****/
double intercept(int N, double x[N], double y[N]){
    double c;
    int i;
    double sumXY=0;
    double sumX=0;
    double sumX2=0;
    double sumY=0;
    for(i=0;i<N;i++){
        sumXY=sumXY+x[i]*y[i];
        sumX=sumX+x[i];
        sumY=sumY+y[i];
        sumX2=sumX2+x[i]*x[i];
    }
    sumXY=sumXY/N;
    sumX=sumX/N;
    sumY=sumY/N;
    sumX2=sumX2/N;
    c=(sumX2*sumY-sumXY*sumX)/(sumX2-sumX*sumX);
    return c;
}
main(){
    int N;
    printf("Enter the no. of data-points:\n");
    scanf("%d",&N);
    double x[N], y[N];
    printf("Enter the x-axis values:\n");
    int i;
    for(i=0;i<N;i++){
        scanf("%lf",&x[i]);
    }
    printf("Enter the y-axis values:\n");
    for(i=0;i<N;i++){
        scanf("%lf",&y[i]);
    }
    printf("The linear fit is given by the equation:\n");
    double m=slope(N,x,y);
    double c=intercept(N,x,y);
    printf("y = %lf x + %lf",m,c);
}

OUTPUTR

You might also like