如何在 Go 语言中求解一元二次方程的所有根?


在本教程中,我们将使用 Go 编程语言找到一元二次方程的根。本教程包含两种不同的编程方法来找出二次方程的根。

解释

指数为 2 的方程称为一元二次方程。一元二次方程的标准方程为 -

$$\mathrm{a\:x\:^{\wedge}\:2\:+\:b\:x\:+\:c\:=\:0}$$

在上述方程中,a、b 和 c 是系数,其中 a 不能为零。为了找到一元二次方程根的性质,即它们是实数还是虚数,我们首先需要使用以下公式找到方程的判别式

$$\mathrm{D\:=\:b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c}$$

如果 D 大于零,则根是不同的实数。

如果 D 等于零,则根是相等的实数。

如果 D 小于零,则根是虚数。

现在我们将使用以下公式找到方程的根 -

$$\mathrm{X1\:=\:(-\:b\:+\:underRoot\:(b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c)\:/\:2\:^{*}\:a}$$

$$\mathrm{X1\:=\:(-\:b\:-\:underRoot\:(b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c)\:/\:2\:^{*}\:a}$$

算法

步骤 1 - 声明系数和判别式的变量。

步骤 2 - 使用相应的值初始化变量。

步骤 3 - 使用相应公式找到判别式。

步骤 4 - 根据判别式的值找到根。

示例

在此示例中,我们将找到函数内一元二次方程的根。

package main
import (

   // fmt package provides the function to print anything
   "fmt"
 
   // math package is to use all the math-related predefined functions
   "math"
)
func main() {
   
   // declaring the variables to store the value
   
   // of the coefficients of quadratic equation
   var a, b, c float64
   
   // declaring a variable of float type to store discriminant
   
   // of the quadratic equation
   var d float64
   
   // initializing the coefficients variable with respective value
   a = 1
   b = 1
   c = 1
   fmt.Println("Finding the roots of the equation with the below coefficients within the function:")
   fmt.Println("a =", a)
   fmt.Println("b =", b)
   fmt.Println("c =", c)
   
   // finding the discriminant using the respective formulae
   d = b*b - 4*a*c
   if d > 0 {
      fmt.Println("Roots are real and different.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      root2 := (-b - math.Sqrt(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", root1)
      fmt.Println("Second Root", root2)
   } else if d == 0 {
      fmt.Println("Roots are equal and same.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      fmt.Println("The root is", root1)
   } else {
      fmt.Println("Roots are complex.")
      realPart := -b / (2 * a)
      imaginaryPart := math.Sqrt(math.Abs(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", realPart, "+", "i", imaginaryPart)
      fmt.Println("Second Root", realPart, "-", "i", imaginaryPart)
   }
}

输出

Finding the roots of the equation with the below coefficients within the function:
a = 1
b = 1
c = 1
Roots are complex.
The roots are:
First Root -0.5 + i 0.8660254037844386
Second Root -0.5 - i 0.8660254037844386

算法

步骤 1 - 声明系数和判别式的变量。

步骤 2 - 使用相应的值初始化变量。

步骤 3 - 使用相应公式找到判别式。

步骤 4 - 根据判别式的值找到根。

示例

在此示例中,我们将找到单独函数中一元二次方程的根。

package main
import (
   
   // fmt package provides the function to print anything
   "fmt"
   
   // math package is to use all the math related predefined functions
   "math"
)
func rootsOfQuadraticEquation(a, b, c float64) {
   
   // declaring a variable of float type to store discriminant
   
   // of the quadratic equation
   var d float64
   
   // finding the discriminant using the respective formulae
   d = b*b - 4*a*c
   if d > 0 {
      fmt.Println("Roots are real and different.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      root2 := (-b - math.Sqrt(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", root1)
      fmt.Println("Second Root", root2)
   } else if d == 0 {
      fmt.Println("Roots are equal and same.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      fmt.Println("The root is", root1)
   } else {
      fmt.Println("Roots are complex.")
      realPart := -b / (2 * a)
      imaginaryPart := math.Sqrt(math.Abs(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", realPart, "+", "i",   imaginaryPart)
      fmt.Println("Second Root", realPart, "-", "i", imaginaryPart)
   }
}
func main() {
   
   // declaring the variables to store the value
   
   // of the coefficients of quadratic equation
   var a, b, c float64
   
   // initializing the coefficients variable with respective value
   a = 4
   b = 12
   c = 9
   fmt.Println("Finding the roots of the equation with the below coefficients in the seperate function:")
   fmt.Println("a =", a)
   fmt.Println("b =", b)
   fmt.Println("c =", c)
   rootsOfQuadraticEquation(a, b, c)
}

输出

Finding the roots of the equation with the below coefficients in the seperate function:
a = 4
b = 12
c = 9Roots are equal and same.
The root is -1.5

结论

这两种方法都可以在 Go 语言中找到一元二次方程的根。从模块化和代码可重用性的角度来看,第二种方法更好,因为我们可以在项目的任何地方调用该函数。要了解有关 Go 语言的更多信息,您可以浏览这些 教程。

更新于: 2022-11-29

898 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.