求一元二次方程实根Python
时间: 2024-03-21 10:36:14 浏览: 100
求一元二次方程的实根可以使用Python编程来实现。以下是一个示例代码:
```python
import math
def solve_quadratic_equation(a, b, c):
discriminant = b**2 - 4*a*c
if discriminant > 0:
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)
return root1, root2
elif discriminant == 0:
root = -b / (2*a)
return root
else:
return "No real roots"
# 示例用法
a = 1
b = -3
c = 2
roots = solve_quadratic_equation(a, b, c)
print("实根为:", roots)
```
上述代码定义了一个名为`solve_quadratic_equation`的函数,该函数接受三个参数`a`、`b`和`c`,分别代表一元二次方程的系数。函数内部通过判断判别式的值来确定方程的根的情况,并返回相应的结果。
在示例中,我们给出了一元二次方程的系数`a=1`、`b=-3`和`c=2`,然后调用`solve_quadratic_equation`函数来求解实根,并将结果打印输出。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)