einstein
einstein

Reputation: 13850

Finding a third degree equation that fits two points with given slopes in javascript

I need to draw a line in my website, actually a curve representing a third degree polynom. What is the easiest way of finding a third degree equation that fits two points with given slopes in javascript?

Find the third degree equation for(or find the coeffecient a,b,c,d in general formula ax^3+bx^2+cx+d = y):

startX, startY, startSlope

endX, endY, endSlope

Upvotes: 1

Views: 2553

Answers (2)

MSalters
MSalters

Reputation: 179819

Essentially this is just straightforward math.

You've got an unknown expression y=ax3+bx2+cx+d. You can drop quite a few terms by defining x' = (x-startX)/endX (i.e. startX' = 0, endX' = 1). You'll also have to scale the slopes; startSlope' = startSlope * 1/(endX-startX).

From this it follows that d' = startY. That's your first free parameter.

Next, note that the slope is trivially obtained by differentiation. dy/dx' = 3a'x'2+2b'x'+c'. Therefore, c' is just startSlope'.

a' and b' take a pair of equations: endY = a'+b'+c'+d', and endSlope = 3a'+2b'+c'+d'. Therefore a' = endSlope' - 2*endY, and b' = 3*endY - endSlope'.

Upvotes: 1

Stephen Chung
Stephen Chung

Reputation: 14605

Assuming a 3-deg polynomial is ax3+bx2+cx+d, you have four unknowns.

Take the derivative to find the slope. That gives three unknowns (the constant term drops out).

You have two deritive equations for the two slopes. Please two equations for the (x,y) pair on the original equation. Therefore you have a total of four equations for four unknowns.

Solve.

Upvotes: 0

Related Questions