Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't mind that you'll get a straight line "curve", then you only need six variables for any amount of data. Here's the source code that's going into my upcoming book; I'm sure that you can figure out how the DataPoint class works:</p> <p>Interpolation.h:</p> <pre><code>#ifndef __INTERPOLATION_H #define __INTERPOLATION_H #include "DataPoint.h" class Interpolation { private: int m_count; double m_sumX; double m_sumXX; /* sum of X*X */ double m_sumXY; /* sum of X*Y */ double m_sumY; double m_sumYY; /* sum of Y*Y */ public: Interpolation(); void addData(const DataPoint&amp; dp); double slope() const; double intercept() const; double interpolate(double x) const; double correlate() const; }; #endif // __INTERPOLATION_H </code></pre> <p>Interpolation.cpp:</p> <pre><code>#include &lt;cmath&gt; #include "Interpolation.h" Interpolation::Interpolation() { m_count = 0; m_sumX = 0.0; m_sumXX = 0.0; m_sumXY = 0.0; m_sumY = 0.0; m_sumYY = 0.0; } void Interpolation::addData(const DataPoint&amp; dp) { m_count++; m_sumX += dp.getX(); m_sumXX += dp.getX() * dp.getX(); m_sumXY += dp.getX() * dp.getY(); m_sumY += dp.getY(); m_sumYY += dp.getY() * dp.getY(); } double Interpolation::slope() const { return (m_sumXY - (m_sumX * m_sumY / m_count)) / (m_sumXX - (m_sumX * m_sumX / m_count)); } double Interpolation::intercept() const { return (m_sumY / m_count) - slope() * (m_sumX / m_count); } double Interpolation::interpolate(double X) const { return intercept() + slope() * X; } double Interpolation::correlate() const { return m_sumXY / sqrt(m_sumXX * m_sumYY); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload