# Least Square Method Curve Fitting

Source: Internet
Author: User
// Least Square Method Curve Fitting
Typedef carray < Double , Double > Cdoublearray;
Bool calculatecurveparameter (cdoublearray * X, cdoublearray * Y, Long M, Long N, cdoublearray * A)
{
// Coordinates of the X, Y -- X, and Y axes
// M -- number of result variable groups
// N -- number of samples
// A -- result Parameter

Register Long I, J, K;
Double Z, D1, D2, C, p, G, Q;
Cdoublearray B, T, S;
B. setsize (N );
T. setsize (N );
S. setsize (N );
If (M > N) m = N;
For (I = 0 ; I < M; I ++ )
( * A) [I] = 0 ;
Z = 0 ;
B [ 0 ] = 1 ;
D1 = N;
P = 0 ;
C = 0 ;
For (I = 0 ; I < N; I ++ )
{
P=P+(*X) [I]-Z;
C=C+(*Y) [I];
}
C = C / D1;
P = P / D1;
( * A )[ 0 ] = C * B [ 0 ];
If (M > 1 )
{
T [ 1 ] = 1 ;
T [ 0 ] =- P;
D2 = 0 ;
C = 0 ;
G = 0 ;
For (I = 0 ; I < N; I ++ )
{
Q = ( * X) [I] - Z - P;
D2 = D2 + Q * Q;
C = ( * Y) [I] * Q + C;
G = (( * X) [I] - Z) * Q * Q + G;
}
C = C / D2;
P = G / D2;
Q = D2 / D1;
D1 = D2;
( * A )[ 1 ] = C * T [ 1 ];
( * A )[ 0 ] = C * T [ 0 ] + ( * A )[ 0 ];
}
For (J = 2 ; J < M; j ++ )
{
S [J] = T [J - 1 ];
S [J - 1 ] =- P * T [J - 1 ] + T [J - 2 ];
If (J > = 3 )
{
For (K = J - 2 ; K > = 1 ; K -- )
S [k] =- P * T [k] + T [K - 1 ] - Q * B [k];
}
S [ 0 ] =- P * T [ 0 ] - Q * B [ 0 ];
D2 = 0 ;
C = 0 ;
G = 0 ;
For (I = 0 ; I < N; I ++ )
{
Q = S [J];
For (K = J - 1 ; K > = 0 ; K -- )
Q = Q * (( * X) [I] - Z) + S [k];
D2 = D2 + Q * Q;
C = ( * Y) [I] * Q + C;
G = (( * X) [I] - Z) * Q * Q + G;
}
C = C / D2;
P = G / D2;
Q = D2 / D1;
D1 = D2;
( * A) [J] = C * S [J];
T [J] = S [J];
For (K = J - 1 ; K > = 0 ; K -- )
{
(*A) [k]=C*S [k]+(*A) [k];
B [k]=T [k];
T [k]=S [k];
}
}
Return True;
}

Related Keywords:

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.