Source code of the least square method written in c ++

Source: Internet
Author: User
Tags x2 y2
# Include <stdio. h>
# Include <conio. h>
# Include <math. h>
# Include <process. h>
# Define N 5 // n points
# Define T 3 // t fitting
# Define W 1 // Weight Function
# Define precision 0.00001
Float pow_n (float a, int N)
{
Int I;
If (n = 0)
Return (1 );
Float res =;
For (I = 1; I <n; I ++)
{
Res * =;
}
Return (RES );
}
Void mutiple (float a [] [N], float B [] [t + 1], float C [] [t + 1])
{
Float res = 0;
Int I, J, K;
For (I = 0; I <t + 1; I ++)
For (j = 0; j <t + 1; j ++)
{
Res = 0;
For (k = 0; k <n; k ++)
{
Res + = A [I] [k] * B [k] [J];
C [I] [j] = res;
}
}
}
Void matrix_trans (float a [] [T + 1], float B [] [N])
{
Int I, j;
For (I = 0; I <N; I ++)
{
For (j = 0; j <T + 1; j ++)
{
B [j] [I] = a [I] [j];
}
}
}
Void init (float x_y [] [2], int n)
{
Int I;
Printf ("Enter % d known points:/n", N );
For (I = 0; I <n; I ++)
{
Printf ("(x % d y % d):", I, I );
Scanf ("% f", & x_y [I] [0], & x_y [I] [1]);
}
}
Void get_A (float matrix_A [] [T + 1], float x_y [] [2], int n)
{
Int I, j;
For (I = 0; I <N; I ++)
{
For (j = 0; j <T + 1; j ++)
{
Matrix_A [I] [j] = W * pow_n (x_y [I] [0], j );
}
}
}
Void print_array (float array [] [T + 1], int n)
{
Int I, j;
For (I = 0; I <n; I ++)
{
For (j = 0; j <T + 1; j ++)
{
Printf ("%-g", array [I] [j]);
}
Printf ("/n ");
}
}
Void convert (float argu [] [T + 2], int n)
{
Int I, j, k, p, t;
Float rate, temp;
For (I = 1; I <n; I ++)
{
For (j = I; j <n; j ++)
{
If (argu [I-1] [I-1] = 0)
{
For (p = I; p <n; p ++)
{
If (argu [p] [I-1]! = 0)
Break;
}
If (p = n)
{
Printf ("no solution to the equations! /N ");
Exit (0 );
}
For (t = 0; t <n + 1; t ++)
{
Temp = argu [I-1] [t];
Argu [I-1] [t] = argu [p] [t];
Argu [p] [t] = temp;
}
}
Rate = argu [j] [I-1]/argu [I-1] [I-1];
For (k = I-1; k <n + 1; k ++)
{
Argu [j] [k]-= argu [I-1] [k] * rate;
If (fabs (argu [j] [k]) <= PRECISION)
Argu [j] [k] = 0;
}
}
}
}
Void compute (float argu [] [T + 2], int n, float root [])
{
Int I, j;
Float temp;
For (I = n-1; I> = 0; I --)
{
Temp = argu [I] [n];
For (j = n-1; j> I; j --)
{
Temp-= argu [I] [j] * root [j];
}
Root [I] = temp/argu [I] [I];
}
}
Void get_y (float trans_a [] [N], float x_y [] [2], float y [], int N)
{
Int I, J;
Float temp;
For (I = 0; I <n; I ++)
{
Temp = 0;
For (j = 0; j <n; j ++)
{
Temp + = trans_a [I] [J] * x_y [J] [1];
}
Y [I] = temp;
}
}
Void cons_formula (float coef_a [] [t + 1], float y [], float coef_form [] [t + 2])
{
Int I, J;
For (I = 0; I <t + 1; I ++)
{
For (j = 0; j <t + 2; j ++)
{
If (j = t + 1)
Coef_form [I] [J] = Y [I];
Else
Coef_form [I] [J] = coef_a [I] [J];
}
}
}
Void print_root (float a [], int N)
{
Int I, J;
Printf ("% d points of % d fitting polynomial coefficients:/n", N, T );
For (I = 0; I <n; I ++)
{
Printf ("a [% d] = % g,", I + 1, a [I]);
}
Printf ("/n ");
Printf ("fitting curve equation:/ny (x) = % g", a [0]);
For (I = 1; I <n; I ++)
{
Printf ("+ % g", a [I]);
For (j = 0; j <I; j ++)
{
Printf ("* X ");
}
}
Printf ("/n ");
}
Void process ()
{
Float x_y [N] [2], matrix_A [N] [T + 1], trans_A [T + 1] [N], coef_A [T + 1] [T + 1], coef_formu [T + 1] [T + 2], y [T + 1], a [T + 1];
Init (x_y, N );
Get_A (matrix_A, x_y, N );
Printf ("matrix A:/n ");
Print_array (matrix_A, N );
Matrix_trans (matrix_A, trans_A );
Mutiple (trans_A, matrix_A, coef_A );
Printf ("method matrix:/n ");
Print_array (coef_A, T + 1 );
Get_y (trans_A, x_y, y, T + 1 );
Cons_formula (coef_A, y, coef_formu );
Convert (coef_formu, T + 1 );
Compute (coef_formu, T + 1, );
Print_root (a, T + 1 );
}
Void main ()
{
Process ();
}
]>
</Content>
<PostDateTime> 19:23:57 </PostDateTime>
</Reply>
<Reply>
<PostUserNickName> </PostUserNickName>
<Rank> Level 1 (Elementary) </rank>
<Ranknum> user1 </ranknum>
<Credit> 100 </credit>
<ReplyID> 40389872 </ReplyID>
<TopicID> 5478010 </TopicID>
<PostUserId> 1526752 </PostUserId>
<PostUserName> jiangxc2004 </PostUserName>
<Point> 0 </Point>
<Content>
<! [CDATA [
You can change it.
If you do not enter parameters from the terminal, enter five known points in the program:
(X0 y0):-2-0.1
(X1 y1):-1 0.1
(X2 y2): 0 0.4
(X3 y3): 1 0.9
(X4 y4): 2 1.6
Matrix A is:
1-2 4-8
1-1 1-1
1 0 0 0
1 1 1 1
1 2 4 8
Algorithm matrix:
5 0 10 0
0 10 0 34
10 0 34 0
0 34 0 130
The polynomial coefficients of three fitting for five points are:
A [1] = 0.408571, a [2] = 0.391667, a [3] = 0.0857143, a [4] = 0.00833333,
The fitting curve equation is:
Y (x) = 0.408571 + 0.391667 * x + 0.0857143 * x + 0.00833333 * x]>
</Content>
<Postdatetime> 19:26:11 </postdatetime>
</Reply>
<Reply>
<Postusernickname> </postusernickname>
<Rank> Level 1 (Elementary) </rank>
<Ranknum> user1 </ranknum>
<Credit> 100 </credit>
<Replyid> 40390406 </replyid>
<Topicid> 5478010 </topicid>
<Postuserid> 1526752 </postuserid>
<PostUserName> jiangxc2004 </PostUserName>
<Point> 0 </Point>
<Content>
<! [CDATA [
In this way, you can directly call the process () function! If the second fitting is done, the macro T will be 2;
The number of fitting points N can also be modified!
You can also go to the comments to call the return value!

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.