3

I'm trying to find all the cubic equations that can be found from a certain scenario

In this scenario, there are 3 points, which can be moved dynamically, but for this situation should be assumed to be static. All qualifying cubic equations must pass through these three points. There is also a slider that augments the current function to change it's shape, while keeping it through the three points.

An example of what I mean can (sorta) be seen here: https://jsfiddle.net/3281qebw/

I already tried to find the equations, and here's my work:

ax1^3+bx1^2+cx1+d=y1
ax2^3+bx2^2+cx2+d=y2
ax3^3+bx3^2+cx3+d=y3

ax1^3+bx1^2+cx-y1 = ax2^3+bx2^2+cx2-y2

a=y1-y2-d*(x1-x2)-b*(x1^2-x2^2)
  -----------------------------
          x1^3-x2^3

From there, I tried to find the other two, leading to this code:

xone = (slider - 50) / 50;

//variables for use in equation
var varK = (firsty - secondy) / (Math.pow(firstx, 3) - Math.pow(secondx, 3));
var varL = (firstx - secondx) / (Math.pow(firstx, 3) - Math.pow(secondx, 3));
var varM = (Math.pow(firstx, 2) - Math.pow(secondx, 2)) / (Math.pow(firstx, 3) - Math.pow(secondx, 3));

var varAlpha = (firsty - (varK * Math.pow(firstx, 3))) / (Math.pow(firstx, 2) - Math.pow(firstx, 3) * varM);

var varBeta = 1 / (Math.pow(firstx, 2) - Math.pow(firstx, 3) * varM);

var varDelta = (firstx - Math.pow(firstx, 3) * varL) / (Math.pow(firstx, 2) - Math.pow(firstx, 3) * varM);

//variables for equation
xzero = ((varK - xone * varL - varAlpha + varDelta * xone) * Math.pow(thirdx, 3) + (varAlpha - varDelta * xone) * Math.pow(thirdx, 2) + xone * thirdx) / (varBeta * Math.pow(thirdx, 3) - varBeta * Math.pow(thirdx, 2) + 1);
xtwo = varAlpha - varBeta * xzero - varDelta * xone;
xthree = varK - xone * varL - xtwo * varM;

I was sure that this would work, but, as can be seen in the jsfiddle, the third point gets excluded for some reason when the slider is moved.

I want to find any errors that may be in my algorithms, or any better general equations, if possible, please.

I've decided to take a different approach to it:

d=ax^3+bx^2+cx-y

Using this, I've gotten to:

y2=y1+a(x2^3-x1^3)+b(x2^2-x1^2)+c(x2-x1)

      y₂-y₁
 c  = ───── - b(x₁+x₂)-a(x2^2+x2x1+x1^2)
      x₂-x₁

But that's as far as I can get.

I've put a couple hours more work into it and made these code blocks:

    xzero = (thirdy - (firsty * Math.pow(thirdx, 3) / Math.pow(firstx, 3)) + (secondy * Math.pow(thirdx, 3) / firstx) - (Math.pow(thirdx, 3) * (firsty * Math.pow(secondx, 3) / Math.pow(firstx, 3)) / firstx) + (Math.pow(thirdx, 3) * (xone * (secondx, 3) / Math.pow(xone, 2)) / firstx) + (xone * Math.pow(thirdx, 3) / Math.pow(firstx, 2)) - secondy * Math.pow(thirdx, 2) + (firsty * Math.pow(secondx, 3) * Math.pow(thirdx, 2) / Math.pow(firstx, 3)) - (xone * Math.pow(secondx, 3) * Math.pow(thirdx, 2) / Math.pow(firstx, 2)) + (xone * secondx * Math.pow(thirdx, 2)) - xone * thirdx) / (1 + (Math.pow(secondx, 3) * Math.pow(thirdx, 2) / Math.pow(firstx, 3)) + (Math.pow(thirdx, 3) / firstx) - Math.pow(thirdx, 2) - (Math.pow(secondx, 3) * Math.pow(thirdx, 3) * (1 / Math.pow(firstx, 3)) / firstx))

xtwo = (secondy - (firsty * Math.pow(secondx, 3) / Math.pow(firstx, 3)) + (xone * Math.pow(secondx, 3) / Math.pow(firstx, 2)) + (xzero * Math.pow(secondx, 3) / Math.pow(firstx, 3)) - xone * secondx - xzero) / (Math.pow(secondx, 2) - (Math.pow(secondx, 3) / firstx));
xthree = (firsty / Math.pow(firstx, 3)) - (xtwo / firstx) - (xone / Math.pow(firstx, 2)) - (xzero / Math.pow(firstx, 3));

}

But as can be seen from here: https://jsfiddle.net/v6bLu80w/ it doesn't work for some reason. Can I please get some help with this?

Correction: It does work as long as the variable isn't a zero, but there seems to be another problem which I was having before where it won't go through the third point, as can be seen here: https://jsfiddle.net/v6bLu80w/1/

gamehen
  • 324
  • 1
  • 5
  • 18
  • So your question [hasn't been answered](https://math.stackexchange.com/questions/1395273/finding-all-possible-cubic-equations-from-two-three-points)? – Lars Kotthoff Aug 14 '15 at 05:25
  • @LarsKotthoff Well, the trouble with that was that a. I haven't learnt collegiate level mathematics yet, and b. this situation felt too technical to be suitable for the math section of SE. In short, I have no idea how to transcribe the given answer. – gamehen Aug 14 '15 at 05:28
  • @LarsKotthoff as for the d3 tag, I thought that it might have been possible that I had set up the line wrong, since the logic in my algebra seems sound. – gamehen Aug 14 '15 at 05:31
  • Have a look at [this answer](https://answers.yahoo.com/question/index?qid=20081023064732AA1idL0). And if you go to 4 points I'd expect you to figure it out on your own, given that you've been helped with 2 and 3 points :) – Lars Kotthoff Aug 14 '15 at 05:36
  • http://stackoverflow.com/a/31910076/1048572 – Bergi Aug 14 '15 at 05:49
  • @LarsKotthoff I've looked, but I don't see how it's related? It doesn't look like a general equation. – gamehen Aug 14 '15 at 05:53
  • @Bergi Thanks, but that's my own question haha – gamehen Aug 14 '15 at 05:53
  • 1. I hope your `x1<=x2<=x3` otherwise it is not function and you need to handle as such (in 2D so each axis is separated and add 3.th parameter t) 2. I got the impression you do not need all the equations just one selected by 4th parameter (slider or what ever) so you can use any 4 point interpolation cubic where 4th point is constructed from the 4th parameter and your 3 points see [interpolation cubic](http://stackoverflow.com/a/22806242/2521214). For example as extension of last 2 points +/- some angle distortion from original direction – Spektre Aug 14 '15 at 10:26
  • @Spektre I'm sorry but I really don't understand interpolation – gamehen Aug 14 '15 at 13:41

1 Answers1

1

There's two separate things you need to do: (1) find the quadratic that goes through the three points, and (2) modify it to get the different cubics that you want.

For (1): The quadratic is

q(x)=y1*(x-x2)*(x-x3)/((x1-x2)*(x1-x3))+y2*(x-x1)*(x-x3)/((x2-x1)*(x2-x3))+y3*(x-x1)*(x-x2)/((x3-x1)*(x3-x2))

That looks intimidating, but (a) it's clearly quadratic, and (b) if you plug in x1, x2, or x3, everything cancels and works out.

For (2): The cubic is f(x)=a*(x-x1)*(x-x2)*(x-x3), because it's a cubic that doesn't affect the function at the desired points.

Your solution is the sum of the two.

Teepeemm
  • 4,331
  • 5
  • 35
  • 58
  • Thanks, but what if I only have two points, and then have two variables? And so you mean that if I add the two equations together then I'll get my function? – gamehen Aug 14 '15 at 13:39
  • 1
    If you have three points (as in your original question), then adding the two functions together will give all possible cubics through those points. I'm not sure what you mean by two points and two variables. – Teepeemm Aug 14 '15 at 18:36
  • you have a typo: "find the quadratic that goes through the *two* points" – Joni Aug 15 '15 at 18:42
  • @Joni Whoops. Thanks. – Teepeemm Aug 15 '15 at 20:31