Implementation of the least squares C language __c language

Source: Internet
Author: User
Tags clear screen pow
1. Experimental purposes:

Further familiar with the least square method of curve fitting.

Master programming language character processing program design and debugging techniques.

2. Experimental requirements:

Input: The number of known points and the coordinates of each point.

Output: The fitting curve is obtained according to the principle of least squares and the coordinates of each point.

3. Program Flow:

(1) Enter the number of known points;

(2) input the x coordinates of the known points respectively;

(3) Enter the y-coordinate of the known point respectively;

(4) The fitting curve is obtained by calling the function.


The principle of least squares is as follows:

According to a set of experimental data, the letter of the variable x and the dependent variable y is obtained.

4. It is difficult to find out the coefficients of polynomials according to the known coordinate points. 5. Program Flow Chart


6. Source code (This procedure has passed the test)


#include <stdio.h> #include <math.h> #define X #define Y float x[x],y[y]; The total number of data entered by int n;//is the total number of coordinates void init ();//initializes and enters the relevant data void Confrim (),//confirms the entered data void Deal (), or calculates the fitting curve void modify () according to the input coordinate point;
	/used to modify the corresponding coordinates of the error to avoid some data re-enter void main () {int select;
	System ("Color F1");//dos command makes the interface variable color init ();//Confrim ();
	printf ("Select the number of polynomials to be synthesized (hint: Enter 2 if the function is typed 12 times):");
scanf ("%d", &select)//Enter the number of times you want to select the Fitted function deal (select);
	void init ()//Initialize and enter related data {int i;
	printf ("\n*********************************************************\n");
	printf ("\ n Welcome to use least squares data handler \ n");
	
	printf (\ nplease Enter the number of groups of data you want to process (hint: The program defines a pair of x,y values as a set of data): ");
		while (1) {scanf ("%d", &n);
			if (n<=1) {printf ("\ n Group number of data cannot be less than or equal to 1");
		printf ("Please re-enter the number of groups of data you want to process:");
			else if (n>50) {printf ("\ n Sorry, this procedure is temporarily unable to handle more than 50 groups of data");
		printf ("Please re-enter the number of groups of data you want to process:");
	else break;
		for (i=0;i<n;i++)//Enter the corresponding coordinates to be stored in the array {printf ("\ n Enter the value of%d x x%d=", i+1,i+1);
		scanf ("%f", &x[i]); printf ("\ n Please enter a value for the corresponding Y": y%d= ", i+1);
	scanf ("%f", &y[i]);
	System ("color F2");//System ("CLS");/clear Screen} void deal (int select)//Use the Kramer rule to solve the equation {int i;
	float A0,A1,A2,TEMP,TEMP0,TEMP1,TEMP2; Float sy=0,sx=0,sxx=0,syy=0,sxy=0,sxxy=0,sxxx=0,sxxxx=0;//defines the correlation variable for (i=0;i<n;i++) {sx+=x[i];//computed XI and sy+=y[i];/ /Compute the sum of the squares of the sxx+=x[i]*x[i];//and the Sxxx+=pow (x[i],3);//compute the cubic and sxxxx+=pow (x[i],4) of Xi and the sum of the 4 squares of Xi
	The determinant of the coefficients of the temp=n*sxx-sx*sx;//equation of Xi squared by Yi and the sxxy+=x[i]*x[i]*y[i];//calculated by Xi by Yi are temp0=sy*sxx-sx*sxy;
	TEMP1=N*SXY-SY*SX;
	A0=temp0/temp;
	A1=temp1/temp; 
		if (select==1) {printf ("one-dimensional linear equation obtained by least squares fitting is: \ n"); 
				printf ("F (x) =%3.3fx+%3.3f\n", a1,a0);
	System ("pause");
	The coefficients determinant +sxx* (sx*sxxx-sxx*sxx) of the temp=n* (sxx*sxxxx-sxxx*sxxx)-sx* (sx*sxxxx-sxx*sxxx)//equation;
	temp0=sy* (sxx*sxxxx-sxxx*sxxx)-sxy* (sx*sxxxx-sxx*sxxx) +sxxy* (SX*SXXX-SXX*SXX);
	temp1=n* (sxy*sxxxx-sxxy*sxxx)-sx* (SY*SXXXX-SXX*SXXY) +sxx* (SY*SXXX-SXY*SXX); temp2=n* (sxx*sxxy-sxy*sxxx)-sx* (sx*sxxy-sy*sxxx) +sxx* (sX*SXY-SY*SXX);
	A0=temp0/temp;
	A1=temp1/temp;
	A2=temp2/temp; 
		if (select==2) {printf ("two approximate equations obtained by least squares fitting: \ n"); 
		printf ("F (x) =%3.3fx2+%3.3fx+%3.3f\n", a2,a1,a0);
	System ("pause");
	} void Modify ()//modify the corresponding coordinate of the error ({int z);
	char flag;
		while (1) {printf ("Please enter the first set of data you want to modify:");
		scanf ("%d", &z);
		printf ("\ n Please enter the value of the%d x you want to modify x%d=", z,z);
		scanf ("%f", &x[z-1]);
		printf ("\ n Please enter the value of the corresponding y you want to modify: y%d=", z);
		scanf ("%f", &y[z-1]);
		printf ("Whether to continue modifying the data is y no n:");
		GetChar ();
		scanf ("%c", &flag); if (flag== ' N ' | |
	flag== ' n ') break;
System ("CLS");//Clear screen Confrim ();
	} void Confrim () {char flag;
	int i;
			while (1) {for (i=0;i<n;i++) {printf ("Please enter the value of%d x x%d=", i+1,i+1);
			printf ("%f", X[i]);
			
			printf ("Enter the value of the corresponding y: y%d=", i+1);
			printf ("%f", Y[i]);
		printf ("\ n");
		printf ("Confirm that the data you entered is y no n (ie re-enter) modify M:");
		GetChar ();
		scanf ("%c", &flag); if (flag== ' y ') | |
		flag== ' Y ') break; else if (flag== ' n ' | |
		flag== ' N ') init ();
			else {modify ();
		Break
}
	}	
} 

Test

1 Fitting two curve results (in the book)


2. Fitting a curve result



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.