Skip to Main Content
Mastering Algorithms with C
book

Mastering Algorithms with C

by Kyle Loudon
August 1999
Intermediate to advanced content levelIntermediate to advanced
560 pages
18h 57m
English
O'Reilly Media, Inc.
Content preview from Mastering Algorithms with C

Implementation and Analysis of Least-Squares Estimation

The implementation of least-squares estimation presented here requires us to do little more than compute a few summations and apply the results to the formulas presented earlier. The operation begins by summing all values for xi in sumx, all values for yi in sumy, all values of xi 2 in sumx2, and all values of xi yi in sumxy (see Example 13-2). Once we have completed this, we compute b 1 and b 0 using the formulas presented earlier.

The runtime complexity of lsqe is O (n), where n is the number of points used to determine b 1 and b 0. This is because a single loop that iterates n times is used to compute the summations.

Example 13.2. Implementation of Least-Squares Estimation
/***************************************************************************** * * * -------------------------------- lsqe.c -------------------------------- * * * *****************************************************************************/ #include <math.h> #include "nummeths.h" /***************************************************************************** * * * --------------------------------- lsqe --------------------------------- * * * *****************************************************************************/ void lsqe(const double *x, const double *y, int n, double *b1, double *b0) { double sumx, sumy, sumx2, sumxy; int i; /***************************************************************************** * * * Compute the required summations. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Introducing Algorithms in C: A Step by Step Guide to Algorithms in C

Introducing Algorithms in C: A Step by Step Guide to Algorithms in C

Luciano Manelli
Head First C

Head First C

David Griffiths, Dawn Griffiths

Publisher Resources

ISBN: 1565924533Supplemental ContentErrata Page