2013-07-16 30 views
0

這看起來像是一個難以置信的簡單問題,但我發現的一切都太複雜了,以至於我無法理解。通過代碼解決彈道方程

我有這個基本的彈道式:

ballistic trajectory

既然我知道V,G,X和Y,我將如何去尋找THETA?在紙上閱讀非常容易,但我不知道這將如何在代碼中完成。

[編輯#3:]我嘗試(從下面的答案輸入)是這樣的:

gx = g*x 
brackets = gx^2 + 2*y*v^2 
sqrroot = sqrt(v^4 - g*brackets) 

top1 = v^2 + sqrroot 
theta1 = atan(top1/gx) 

top2 = v^2 - sqrroot 
theta2 = atan(top2/gx) 
+1

這不是一個二次方程.... – Dan455

+0

向下打破方程成小的離散步驟。爲gx解決。解決gx平方。求解2yv平方。等等,直到你有theta。 –

+0

@ Dan455這是什麼? –

回答

0

如果您沒有訪問反正切方法,可以使用擬牛頓算法。

+0

我不知道擬牛頓算法是什麼。 –

0

在你的最後一行,

theta = atan(equation/gx) 

equation未設置。您可能想用top代替equation

它也可能有助於您輸出每個中間結果(gx,括號,sqrroot和頂部)。看看是否有任何意外的中間結果。

1

你忽略了一個解決方案 - 你有

top = v^2 + sqrroot 

但你也需要做重新計算與

top = v^2 - sqrroot 

,以考慮您的方程±

所以:

top1 = v^2 + sqrroot 
top2 = v^2 - sqrroot 

theta1 = atan(top1/gx) 
theta2 = atan(top2/gx) 

(我不知道什麼是equation在你的代碼,但我假設你的意思top

+0

我只生成了+方程的代碼。我已經使用上面的解決方案更新了我的帖子中的代碼。 –

1

更多類似這樣的。

gx = g*x 
brackets = g*x^2 + 2*y*v^2 
sqrroot = sqrt(v^4 - g*brackets) 
top1 = v^2 + sqrroot 
theta1 = atan(top1/gx) 
top2 = v^2 - sqrroot 
theta2 = atan(top2/gx) 

您必須考慮公式中的加號和減號。

您可以計算乘法之前的平方。在某些語言中,可以通過計算g * x * x來計算g * x^2。

0

A C溶液

#include<math.h> 

void MattW_f(double *theta_p, double *theta_n, double g, double v, double x, double y) { 
    double v2 = v*v; 
    double gx = g*x; 
    // No check against sqrt(negative number) 
    double discriminant = sqrt(v2*v2 - g*(gx*x + 2*y*v2)); 
    if (theta_p) { 
    // atan2() returns -pi to +pi, it is a 4 quadrant arctan. 
    *theta_p = atan2(v2 + discriminant, gx); 
    } 
    if (theta_n) { 
    *theta_n = atan2(v2 - discriminant, gx); 
    } 
    } 
相關問題