fitlinear

 

Description

a simplified version of Linear fit

Syntax

bool fitlinear( const vector & vx, const vector & vy, vector & vCoeff, const vector & vWt = NULL, RegStats * psRegStats = NULL, bool bThroughZero = false )

Parameters

vx
[input] X data
vy
[input] Y data
vCoeff
[output] Intercept, Slope and their errors, [0]=intercept, [1]=slope, [2]=error of intercept, [3]=error of slope
vWt
[input] optional weight
psRegStats
[output] optional regression stats
bThroughZero
[input] fit line through zero if set to true

Return

Returns TRUE on success and FALSE on failure.

Examples

EX1

//Linear fitting by X, Y coordinate
void fitlinear_Ex1()
{
        vector vx = {1.24944, 3.17474, 4.81103, 6.37617, 8.30147, 8.90618};
        vector vy = {6.83952, 5.21834, 3.07314, 2.37445, 2.28712, 1.28275};
        vector vc;
        if( fitlinear(vx, vy, vc) )
        {
                vector vfit;
                double slope = vc[1];
                double intercept = vc[0];
                printf("slope = %g, intercept = %g\n", slope, intercept);
                vfit = intercept + slope * vx;
                
                Worksheet wks;
                wks.Create();
                wks.SetSize(-1, 3);
                Dataset da(wks, 0);
                Dataset db(wks, 1);
                Dataset dfit(wks, 2);
                da = vx;
                db = vy;
                dfit = vfit;
        }
}

Remark

See Also

fitpoly, ocmath_linear_fit

header to Include

origin.h

Reference