[英]Need Help Finding the intersection of two equations in Java
我看到其他人發布了這個問題,但沒有正確說出來,所以他最終沒有得到任何幫助,所以我想我會嘗試更直接。
以下是向我們提出的問題:
//在一個名為Intersect.java的文件中編寫並提交您的代碼。 使用IO模塊讀取輸入。 使用System.out.println()打印您的答案。
編寫一個程序來計算2個方程之間的交集:
度數為2(二次)的多項式,即
y = dx ^ 2 + fx + g
其中d,f和g是常數
和1度(線性)方程即
y = mx + b
其中m是斜率,b是常數
以上只是文本,而不是可能出現在Java程序中的代碼。
詢問用戶每個等式中的常數值。 將交集輸出為有序對(x,y),如果不存在則輸出“無”。 下面是一個示例運行。
java相交
輸入常數d:5輸入常數f:-3輸入常數g:2輸入常數m:1輸入常數b:3
交叉點是:(1,4)( - 0.20,2.8)//
主要問題基本上是要求我們編寫一個代碼,要求用戶輸入各個常數和兩個方程的斜率,一個是二次多項式,另一個是點斜率的線性方程。
我知道我們必須在代碼中使用二次方程式,但是我不知道如何實際編寫代碼。
一旦我們讓用戶輸入常數,在這種情況下5(d,f,g,m,b; m是斜率)我們需要讓代碼運行計算以將這些常量輸入到上面的例子中(y = dx ^ 2 + fx + g | y = mx + b)如果沒有交點,或者如果它確實相交,則返回“無”,它與它相交的有序對(x,y)。
我已經知道如果輸入0作為常量它返回(NaN,NaN),我也知道需要將其重新寫入None。
到目前為止我只有以下內容:
public class Intersect {public static void main(String [] args){
System.out.println("Enter the constant d:");
int d = IO.readInt();
System.out.println("Enter the constant f:");
int f = IO.readInt();
System.out.println("Enter the constant g:");
int g = IO.readInt();
System.out.println("Enter the constant m:");
int m = IO.readInt();
System.out.println("Enter the constant b:");
int b = IO.readInt();
如果有人能夠對此有所了解,那就太棒了,謝謝!
編輯1:
到目前為止,我已將代碼更改為以下內容,但是,我仍然不知道如何讓它返回給我一個答案:
public class Intersect {public static void main(String [] args){
System.out.println("Enter the constant d:");
int d = IO.readInt();
System.out.println("Enter the constant f:");
int f = IO.readInt();
System.out.println("Enter the constant g:");
int g = IO.readInt();
System.out.println("Enter the constant m:");
int m = IO.readInt();
System.out.println("Enter the constant b:");
int b = IO.readInt();
//y = dx^2 + fx + g
//y = mx + b
//mx + b = dx^2 + fx + g
//x^2 * (d) + x * ( f - m ) + ( g - b )
int A = d;
int B = f - m;
int C = g - b;
double x1 = - B + Math.sqrt( B^2 - 4 * A * C ) / (2 * A);
double x2 = - B - Math.sqrt( B^2 - 4 * A * C ) / (2 * A);
double y1 = m * x1 + b;
double y2 = m * x1 + b;
}
另外,eclipse告訴我根本不使用x2,y1和y2。 我知道我需要使用System.out.println()但是我不明白我能把它放在那里以使答案成為有序對。 此外,我嘗試設置一個If語句讓答案返回None而不是NaN但是它返回NaN None。
我猜....
//y = dx^2 + fx + g
//y = mx + b
//mx + b = dx^2 + fx + g
//x^2 * (d) + x * ( f - m ) + ( g - b )
A = d
B = f - m
C = g - b
x1 = - B + sqr( B^2 - 4 * A * C ) / (2 * A)
x2 = - B - sqr( B^2 - 4 * A * C ) / (2 * A)
y1 = m * x1 + b
y2 = m * x1 + b
您需要考慮很多特殊情況。
我希望我能把它們弄好。
所以在初始化值之后你可以把它放在:
// calculating some useful values.
double t = -(f - m) / (2.0 * d);
double u = t * t - (g - b) / (double) d;
// the first polynomial is linear, so both terms are.
if (d == 0) {
// both linear functions have the same slope.
if (f == m) {
// both functions are shifted the same amount along the y-Axis.
if (g == b)
// the functions lie on top of each other.
System.out.println("There is an infinite amount intersections");
// the functions are shifted different amounts along the y-Axis.
else
// the lines are parallel.
System.out.println("There are no intersections");
}
// both linear functions have different slopes.
else {
// solve linear equation.
double x = (b - g) / (double) (f - m);
double y = m * x + b;
System.out.println("The intersection is: (" + x + "," + y + ")");
}
}
// the functions do not cross each other.
else if (u < 0)
System.out.println("There are no intersections");
// the linear function is a tangent to the quadratic function.
else if (u == 0) {
// solve equation.
double x = t;
double y = m * x + b;
System.out.println("The intersection is: (" + x + "," + y + ")");
}
// the linear function intersects the quadratic function at two points.
else {
// solve quadratic equation.
double x1 = t + Math.sqrt(u);
double x2 = t - Math.sqrt(u);
double y1 = m * x1 + b;
double y2 = m * x2 + b;
System.out.println("The intersections are: (" + x1 + "," + y1 + ") (" + x2 + "," + y2 + ")");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.