0

I need to implement a function that finds the trajectory of a projectile and I have three points - origin, destination and the point of maximum height.

I need to find the correct quadratic function that includes these points.

I'm having a hard time figuring out what to do. Where should I start?

Abhishek T.
  • 1,133
  • 1
  • 17
  • 33
Soul
  • 35
  • 10
  • http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html Look at equation 4 in particular. – Pig Jul 27 '16 at 03:05
  • Interpolation or Spline are the keywords you are looking for – OneCricketeer Jul 27 '16 at 03:12
  • Use the general formula for a parabola, `axx + bx + c = y`, and substitute the (x,y) coordinates of your three points. This gives you a system of 3 linear equations (one for each point) on 3 variables (`a`, `b`, `c`), which can be solved by standard methods like row reduction. – isekaijin Jul 27 '16 at 03:14
  • Also, Stack Overflow isn't the right place for this kind of question. Math.SE would be more appropriate, perhaps? – isekaijin Jul 27 '16 at 03:15
  • @pyon How do I compute variables without assigning values? Sorry I'm a beginner. – Soul Jul 27 '16 at 03:18
  • I wasn't talking about how you should write your program. That I will leave up to you. I was just describing the algebraic manipulation leading to the solution. Let's say your three points are (0,0), (2,12) and (4,0). Then, after substituting, you get the following equations: `a(0)(0) + b(0) + c = 0`, `a(2)(2) + b(2) + c = 12` and `a(4)(4) + b(4) + c = 0`. They can be further simplified to `c = 0`, `4a + 2b + c = 12` and `16a + 4b + c = 0`. – isekaijin Jul 27 '16 at 03:25
  • 1
    I'm voting to close this question as off-topic because it is primarily about geometry, not programming, and probably belongs on math.stackexchange.com. – m69's been on strike for years Jul 27 '16 at 03:46

1 Answers1

-1

Assume you have your origin and destination on the y-axis,namely x1 and x2. If not you can shift them later.

a*x*x + b*x + c = 0//equation
x1*x2=(c/a);
c = (x1*x2)*a;
x1+x2=(-b/a);
b = (x1+x2)/(-a);
a*((x1+x2)/2)^2 + b*((x1+x2)/2) + c = h//max height
let X=(x1+x2)/2;
a*X*X + ((2*X)/(-a))*X + (x1*x2)*a - h = 0;

Now you can iterate through a=0 until the above equation is true as you have all the values X ,x1 , x2 and h.

double eqn = (-h),a=0;//a=0.Assuming you have declared x1,x2 and X already
while(eqn!=0)
{
    a++;
    eqn = a*X*X + ((2*X)/(-a))*X + (x1*x2)*a - h;
}
b = (x1+x2)/(-a);
c = (x1*x2)*a;

Thus you got all your coeffecients.

yobro97
  • 1,125
  • 8
  • 23
  • The loop never exits. Is -h correct inside the loop? but thanks for your help now I have solid ground to work on. – Soul Jul 27 '16 at 07:12
  • @Sangratura, I actually never intended to write the code/test it....I was giving you a rough idea on how to proceed in the program. Can you post your code? Actually it is not necessary that an integer exists. For example if a=PI(3.14...), then the loop will never exit as there is no integral multiple of PI which is an integer. – yobro97 Jul 27 '16 at 07:18
  • @Sangratura...so it depends on your equation. If the coeffecients are irrational then no integer exists and hence loop will not exit. I hope you understood my point. – yobro97 Jul 27 '16 at 07:20
  • I understand that thus I incremented in smaller intervals but still the same. I guess this method is too expensive to compute. This site for instance, calculates instantly, this is exactly what I need. I dont know what algorithm they use .http://www.analyzemath.com/parabola/three_points_para_calc.html – Soul Jul 27 '16 at 07:25
  • @Sangratura Why don't you try this? After every loop, round the digits to accuracy you want. If you increment by `0.01` then round to `2` digits.http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c for how to round... – yobro97 Jul 27 '16 at 07:36
  • I managed to exit the loop by limiting the eqn to be less than 0.1 to get an idea but I get very wrong results . And it takes too long to compute. Anyway I guess there is something wrong in my logic, I'll work on it. Thanks for your help. – Soul Jul 27 '16 at 07:55
  • Three given points uniquely identify a parabola. There's no need for looping, and indeed, your code will almost always (in the mathematical sense) fail. – AakashM Jul 27 '16 at 11:38