二次函数的一般形式为 \( y = ax^2 + bx + c \),其中 \( a \), \( b \), \( c \) 是常数,且 \( a
eq 0 \)。以下是用不同编程语言定义二次函数的示例:
Python
```python
def quadratic_function(a, b, c, x):
"""计算二次函数的值
:param a: 二次项系数
:param b: 一次项系数
:param c: 常数项
:param x: 自变量
:return: 二次函数的值
"""
return a * x2 + b * x + c
调用二次函数并打印结果
a = 2
b = 3
c = 1
x = 5
result = quadratic_function(a, b, c, x)
print("二次函数的值为:", result)
```
Java
```java
public class QuadraticFunction {
public static double quadraticFunction(double a, double b, double c, double x) {
return a * Math.pow(x, 2) + b * x + c;
}
public static void main(String[] args) {
double a = 2;
double b = 3;
double c = 1;
double x = 5;
double result = quadraticFunction(a, b, c, x);
System.out.println("二次函数的值为: " + result);
}
}
```
C++
```cpp
include include double quadraticFunction(double a, double b, double c, double x) { return a * std::pow(x, 2) + b * x + c; } int main() { double a = 2; double b = 3; double c = 1; double x = 5; double result = quadraticFunction(a, b, c, x); std::cout << "二次函数的值为: " << result << std::endl; return 0; } ``` JavaScript ```javascript function quadraticFunction(a, b, c, x) { return a * Math.pow(x, 2) + b * x + c; } // 调用二次函数并打印结果 let a = 2; let b = 3; let c = 1; let x = 5; let result = quadraticFunction(a, b, c, x); console.log("二次函数的值为:", result); ``` C ```csharp using System; class QuadraticFunction { public static double Function(double a, double b, double c, double x) { return a * Math.Pow(x, 2) + b * x + c; } public static void Main() { double a = 2; double b = 3; double c = 1; double x = 5; double result = Function(a, b, c, x); Console.WriteLine("二次函数的值为: " + result); } } ``` 这些示例展示了如何在不同的编程语言中定义和调用二次函数。你可以根据所使用的编程语言选择合适的代码片段进行学习和使用。