# cubic polynomial interpolation curve

i am working on plotting cubic interpolating curve graph in java, and encountered problem in finding the y with give at some dx value.

what i am doing is :

suppose i have 4 set of co ordinates

$(x_1,y_1)\\ (x_2,y_2)\\ (x_3,y_3)\\ (x_4,y_4)$

and created the 4 cubic equation from 4 coordinates

$y_1 = a \cdot x_1^3 + b \cdot x_1^2 + c \cdot x_1 + d\\ y_2 = a \cdot x_2^3 + b \cdot x_2^2 + c \cdot x_2 + d\\ y_3 = a \cdot x_3^3 + b \cdot x_3^2 + c \cdot x_3 + d\\ y4 = a \cdot x_4^3 + b \cdot x_4^2 + c \cdot x_4 + d$

i know the values of $x_1,x_2,x_3,x_4,y_1,y_2,y_3,y_4$ i want to find coefficients $a,b,c,d$ so that i can find the $y$ at any given $x$ with cubic equation.

i don't know i am doing right or wrong. please help and correct if i am wrong i case.

What you have will work, creating a system of 4 linear equations, and is not too hard to solve in the specific case, but becomes more difficult in the abstract cases to solve.

Here is an algorithm that will work for $n$ pairs of coordinates (producing a degree $n-1$ polynomial)

$\begin {array}\\ x_1 &y_1\\ &&F_{2,1}=\frac {y_2-y_1}{x_2 - x_1}\\ x_2 &y_2&&&F_{3,1} = \frac {F_{3,2} - F_{2,1}}{x_3-x_1}\\ &&F_{3,2}=\frac {y_3-y_2}{x_3 - x_2}&&&F_{4,1} = \frac {F_{4,2} - F_{3,1}}{x_4-x_1}\\ x_3 &y_3&&&F_{4,2} = \frac {F_{4,3} - F_{3,2}}{x_4-x_2}\\ &&F_{4,3}=\frac {y_4-y_3}{x_4 - x_3}\\ x_4 &y_4 \end {array}$

$y = y_1 + F_{2,1}(x-x_1) + F_{3,1}(x-x_1)(x-x_2)+ F_{4,1}(x-x_1)(x-x_2)(x-x_3)$

using cubic polynomial function f(x) = h3.x^3 + h2.x^2 + h1.x + h0

i reached upto the 4 hermite blending functions

h3 = (2.x^3 - 3.x^2 + 1).p0 h2 = (-2.x^3 + 3.x^2).p1 h1 = (x^3 - 2.x^2 + x).g0 h0 = (x^3 - x^2).g1

where p0,p1 is point 0,point 1 and g0,g1 is gradient at p0 and p1.

now i substituted the 4 functions in the original one f(x) = (2.x^3 - 3.x^2 + 1).p0 + (-2.x^3 + 3.x^2).p1 + (x^3 - 2.x^2 + x).g0 + (x^3 - x^2).g1

when i am substituting the value >= 0 x <= 1 , then f(x) result is wrong, i dont understand where am i going wrong. here is my code

public Vector getPoint(Vector p0,Vector p1,Vector p2,Vector p3,float x) { Vector ans = new Vector ();

    Vector p0t = new Vector (p0);
Vector p1t = new Vector (p1);
Vector p2t = new Vector (p2);
Vector p3t = new Vector (p3);
float c3;
float c2;
float c1;
float c0;

c3 = (2 * x * x * x) - (3 * x * x) + 1;
c2 = (x * x * x) - (2 * x * x) + x;
c1 = (-2 * x * x * x) + (3 * x * x);
c0 = (x * x * x) - (x * x);

p0t.multiply (c3);
p1t.multiply (c1);
p2t.multiply (c2);
p3t.multiply (c0);

return ans;
}

public void renderCurve()
{
Vector p0,p1,p2,p3,p;
for (int i = 0; i < points.Length; i = i + 4)
{
p0 = new Vector (points[i].x,points[i].y,points[i].z);
p1 = new Vector (points[i + 1].x,points[i + 1].y,points[i + 1].z);
p2 = new Vector (points[i + 2].x,points[i + 2].y,points[i + 2].z);
p3 = new Vector (points[i + 3].x,points[i + 3].y,points[i + 3].z);
for (float t = 0; t <= 1; t = (t + Time.deltaTime * 10))
{
p = hermite.getPoint (p0,p1,p2,p3,t);
GameObject pobj = Instantiate (pointObject, new Vector3(p.x,p.y,0.0f), Quaternion.identity) as GameObject;
}
}
}