Silicon's blog

Menu
  • Home
  • Kadena
  • Solana
  • Ethereum
  • Bot Automation
  • Proxmox
  • Nginx Proxy Manager
  • Others
  • Contact
Menu

Fitting quadratic equation to a set of data points using Least Squares Method with C and Python (Verified with curve fitting function in Excel)

Posted on September 5, 2022May 17, 2023 by Silicon
Sharing is Caring:
Twitter 0
Copy 0

 

According to Sharma’s article, any polynomial equations could fit to a given set of data points in this way.

To fit a qudratic equation to the data points below, we may try to use Sharma’s method with N = 5 (We have 5 data-point pairs). After Gaussian elimination, we will get a0 = 0.55, a1 = -1.07 and a2 = 1.42.

Using excel curve fitting function (Polynomial & Order = 2), we will obtain identical result.

A simple C program for fitting a quadratic equation to data points using the Least Squares Method

#include <stdio.h>
#include <math.h>
#define array_size 5
float x_data[array_size] = {0, 1, 2, 3, 4};
float y_data[array_size] = {1, 1.8, 1.3, 2.5, 6.3};

float x[9] = {0}; //The number of elements of a 3 x 3 matrix (LHS) = 9
float y[3] = {0}; //The number of elements of a 3 x 1 matrix (RHS) = 3

int main()
{
	//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	//%%	Least Squares Approximation Method
	//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		float x1 = 0;
		float x2 = 0;
		float x3 = 0;
		float x4 = 0;
		float xY = 0;
		float xY2 = 0;
		float sumY = 0;

	    for(int i = 0; i<=array_size; i++)
		{
		   float temp = x_data[i];
		   float temp2 = y_data[i];
		   x1 += temp;
		   x2 += pow(temp,2);
		   x3 += pow(temp,3);
		   x4 += pow(temp,4);
		   xY += temp * temp2;
		   xY2 += pow(temp,2) * temp2;
		   sumY += temp2;
		}

		x[0] = array_size;
		x[1] = x1;
		x[2] = x2;
		x[3] = x1;
		x[4] = x2;
		x[5] = x3;
		x[6] = x2;
		x[7] = x3;
		x[8] = x4;
		y[0] = sumY;
		y[1] = xY;
		y[2] = xY2;

	//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	//%%	Gaussian elimiation
	//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	float common = x[3]/x[0];
	x[4] -= x[1] * common;
	x[5] -= x[2] * common;
	y[1] -= y[0] * common;

	float common2 = x[6]/x[0];
	x[7] -= x[1] * common2;
	x[8] -= x[2] * common2;
	y[2] -= y[0] * common2;

	float common3 = x[7]/x[4];
	x[8] -= x[5] * common3;
	y[2] -= y[1] * common3;

    //	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    //	%%	Back substitution
    //	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	float a0 = y[2]/x[8];
	float a1 = (y[1] - a0 * x[5])/x[4];
	float a2 = (y[0] - a0 * x[2] - a1 * x[1])/x[0];
    printf("%.2fx^2%+.2fx%+.2f",a0,a1,a2);

    return 0;
}

A simple Python program for fitting a quadratic equation to data points using the Least Squares Method

def array_size():
    return 5

x_data = [0, 1, 2, 3, 4]
y_data = [1, 1.8, 1.3, 2.5, 6.3]

x = [0]*9 #The number of elements of a 3 x 3 matrix (LHS) = 9
y = [0]*3 #The number of elements of a 3 x 1 matrix (RHS) = 3

if __name__ == "__main__":

	#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	#%%	Least Squares Approximation Method
	#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    x1 = 0
    x2 = 0
    x3 = 0
    x4 = 0
    xY = 0
    xY2 = 0
    sumY = 0

    i = 0
    while(i<array_size()):        
        temp = x_data[i]
        temp2 = y_data[i]
        x1 += temp
        x2 += pow(temp,2)
        x3 += pow(temp,3)
        x4 += pow(temp,4)
        xY += temp * temp2
        xY2 += pow(temp,2) * temp2
        sumY += temp2
   
        i+=1

    x[0] = array_size()
    x[1] = x1
    x[2] = x2
    x[3] = x1
    x[4] = x2
    x[5] = x3
    x[6] = x2
    x[7] = x3
    x[8] = x4
    y[0] = sumY
    y[1] = xY
    y[2] = xY2

	#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	#%%	Gaussian elimiation
	#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    common = x[3]/x[0]
    x[4] -= x[1] * common
    x[5] -= x[2] * common
    y[1] -= y[0] * common

    common2 = x[6]/x[0]
    x[7] -= x[1] * common2
    x[8] -= x[2] * common2
    y[2] -= y[0] * common2

    common3 = x[7]/x[4]
    x[8] -= x[5] * common3
    y[2] -= y[1] * common3

    #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    #%%	Back substitution
    #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    a0 = y[2]/x[8]
    a1 = (y[1] - a0 * x[5])/x[4]
    a2 = (y[0] - a0 * x[2] - a1 * x[1])/x[0]
    print("%.2fx^2%+.2fx%+.2f"%(a0,a1,a2))

For more complicated polynomial fitting using the Least Squares Method, please refer to Sharma’s original article.

Leave a Reply Cancel reply


The reCAPTCHA verification period has expired. Please reload the page.

©2025 Silicon's blog
Click to Copy