4
$\begingroup$

Possible Duplicate:
Writing a function $f$ when $x$ and $f(x)$ are known

If I am given 9 co-ordinates of a random graph say for e.g

1. (2,1) 2. (4,3) 3. (7,9) 4. (9,5) 5. (10,3) 6. (11,1)  7. (13,4) 8. (15,7) 9. (17,10) 

this is the plotted graph exactly  however it needs to be curved

((this is the plotted graph exactly however it needs to be curved ))

How can i create an equation to approximately fit the graphs trendline curve.

  • 0
    You can use an interpolating polynomial, then...2012-05-08

2 Answers 2

2

You want to find a polynomial such that a given number of points lie on the graph. observe that every given point is a "condition" on your function. A polynmial of degree $n$ is of the form $P(x)=a_nx^n+\cdots+a_0$ and has therefore $n+1$ degrees of freedom. So for $n$ given points a polynomial of degree $n-1$ should do the job. Hence assume $P(x)$ is a solution. Just plug in your given points, one at a time. E.g. $P(2)=1$ $P(4)=3$ $\cdots$ $P(17)=10$

You will obtain $n$ linear equations for your $n$ variables which you now have to solve. As an advanced exercise: what does it mean if the system has multiple solutions?

  • 0
    Do you mean trial and error? But is there an algorithm?2017-10-17
2

I am guessing that you are trying to fit a straight line to the data?

One common method is least squares, that is, in this case, find $a_0,a_1$ to minimize $\sum_{i=1}^9 (a_0+a_1 x_i - y_i)^2$, where $(x_i, y_i)$ are your data above. If we let $D=\begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ \vdots & \vdots\end{bmatrix}, b= \begin{bmatrix} y_1 \\ y_2 \\ \vdots\end{bmatrix}, a = \begin{bmatrix} a_0 \\ a_1\end{bmatrix},$ then we can write the problem as $\min_{a \in \mathbb{R}^2} || D a -b ||_2^2.$ The minimizing solution satisfies $D^T(Da-b) = 0,$ so we have $a = (D^T D)^{-1}D^T y$. In your case, this (or rather Octave) gives $a=\binom{1.26406}{0.35936}$. So the line $x \mapsto 1.26406+0.35936 x$ is the best-fit line in a least squares sense.