递推关系是一个方程,一旦给出一个或多个初始项,它就会递归地定义值的序列或多维数组;序列或数组的每一项进一步定义为前面各项的函数。以下是使用多项式归约法求解递推方程所需的步骤:
- 为给定的递推方程形成一个特征方程。
- 求解特征方程并求特征方程的根。
- 用未知系数简化解。
- 求解关于初始条件的方程以获得特定解。
例子:
考虑以下二阶递推方程:
T(n) = a1T(n-1) + a2T(n-2)
为求解该方程,将其公式化为特征方程。
让我们重新排列方程如下:
T(n) - a1T(n-1) - a2T(n-2) = 0
让, T(n) = x n
现在我们可以说T(n-1) = x n-1和T(n-2)=x n-2
现在等式将是:
xn + a1xn-1 + a2xn-2 = 0
将整个方程除以x n-2 [因为x不等于0 ] 我们得到:
x2 + a1x + a2 = 0
我们得到了我们的特征方程x 2 +a 1 x+a 2 =0
现在,找出这个方程的根。
求特征方程的根时可能存在三种情况,它们是:
案例 1:特征方程的根是实数和异数
如果特征方程有r个不同的根,那么可以说有r个基本解是可能的。可以采用任何根的线性组合来获得线性递推方程的一般解。
如果r 1 , r 2 , r 3 ……, r k是特征方程的根,则递推方程的通解为:
tn = c1r1n + c2r2n + c3r3n +...........+ ckrkn
案例 2:特征方程的根是实数但不相异
让我们考虑特征方程的根是不明确的,根 r 在 m 的重数中。在这种情况下,特征方程的解将是:
r1 = rn
r2 = nrn
r3 = n2rn
.........
rm = nm-1rn
因此,包括所有解以获得给定递推方程的一般解。
案例 3:特征方程的根不同但不真实
如果特征方程的根是复数,则求根的共轭对。
若r 1和r 2是一个特征方程的两个根,且互为共轭对,则可表示为:
r1 = reix
r2 = re-ix
一般的解决方案是:
tn = rn(c1cos nx + c2sin nx)
例子:
让我们解决给定的递推关系:
T(n) = 7*T(n-1) - 12*T(n-2)
设 T(n) = x n
现在我们可以说 T(n-1) = x n-1和 T(n-2)=x n-2
将整个方程除以 x n-2 ,我们得到:
x2 - 7*x + 12 = 0
下面是求解给定二次方程的实现:
C++
// C++ program to find roots
// of a quadratic equation
#include
using namespace std;
class Quadratic{
public:
// Prints roots of quadratic
// equation ax * 2 + bx + x
void findRoots(int a, int b, int c)
{
// If a is 0, then equation is not
// quadratic, but linear
if (a == 0)
{
cout << "Invalid";
return;
}
int d = b * b - 4 * a * c;
float sqrt_val = sqrt(abs(d));
// Real Roots
if (d > 0)
{
cout << "Roots are real and different" << endl;
cout << fixed << std::setprecision(1)
<< float((-b + sqrt_val) / (2 * a)) << endl;
cout << fixed << std::setprecision(1)
<< float((-b - sqrt_val) / (2 * a)) << endl;
}
// Imaginary Roots
else
{
cout << "Roots are complex" << endl;
cout << fixed << std::setprecision(1)
<< float(b / (2.0 * a)) << " + i"
<< sqrt_val << endl;
cout << fixed << std::setprecision(1)
<< float(b / (2.0 * a)) << " - i"
<< sqrt_val << endl;
}
}
};
// Driver code
int main()
{
Quadratic obj;
// Given value of coefficients
int a = 1, b = -7, c = 12;
obj.findRoots(a, b, c);
}
// This code is contributed by SURENDRA_GANGWAR
Java
// Java program to find roots
// of a quadratic equation
import java.io.*;
import static java.lang.Math.*;
class Quadratic {
// Prints roots of quadratic
// equation ax * 2 + bx + x
void findRoots(int a, int b, int c)
{
// If a is 0, then equation is not
// quadratic, but linear
if (a == 0) {
System.out.println("Invalid");
return;
}
int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));
// Real Roots
if (d > 0) {
System.out.println(
"Roots are real"
+ " and different \n");
System.out.println(
(double)(-b + sqrt_val)
/ (2 * a)
+ "\n"
+ (double)(-b - sqrt_val)
/ (2 * a));
}
// Imaginary Roots
else {
System.out.println(
"Roots are complex \n");
System.out.println(
-(double)b / (2 * a)
+ " + i"
+ sqrt_val + "\n"
+ -(double)b / (2 * a)
+ " - i" + sqrt_val);
}
}
// Driver code
public static void main(String args[])
{
Quadratic obj = new Quadratic();
// Given value of coefficients
int a = 1, b = -7, c = 12;
obj.findRoots(a, b, c);
}
}
Python3
# Python3 program to find roots
# of a quadratic equation
from math import sqrt
# Prints roots of quadratic
# equation ax * 2 + bx + x
def findRoots(a, b, c):
# If a is 0, then equation is not
# quadratic, but linear
if (a == 0):
print("Invalid")
return
d = b * b - 4 * a * c
sqrt_val = sqrt(abs(d))
# Real Roots
if (d > 0):
print("Roots are real and different")
print((-b + sqrt_val) / (2 * a))
print((-b - sqrt_val) / (2 * a))
# Imaginary Roots
else:
print("Roots are complex \n")
print(-b / (2 * a), " + i",
sqrt_val, "\n",
-b / (2 * a), " - i",
sqrt_val)
# Driver code
if __name__ == '__main__':
# Given value of coefficients
a = 1
b = -7
c = 12
findRoots(a, b, c)
# This code is contributed by mohit kumar 29
C#
// C# program to find roots
// of a quadratic equation
using System;
class Quadratic{
// Prints roots of quadratic
// equation ax * 2 + bx + x
void findRoots(int a,
int b, int c)
{
// If a is 0, then equation
// is not quadratic, but linear
if (a == 0)
{
Console.WriteLine("Invalid");
return;
}
int d = b * b - 4 *
a * c;
double sqrt_val =
Math.Sqrt(Math.Abs(d));
// Real Roots
if (d > 0)
{
Console.WriteLine("Roots are real" +
" and different \n");
Console.WriteLine((double)
(-b + sqrt_val) /
(2 * a) + "\n" +
(double)
(-b - sqrt_val) /
(2 * a));
}
// Imaginary Roots
else
{
Console.WriteLine("Roots are complex \n");
Console.WriteLine(-(double)b / (2 * a) +
" + i" + sqrt_val +
"\n" + -(double)b /
(2 * a) + " - i" +
sqrt_val);
}
}
// Driver code
public static void Main(String []args)
{
Quadratic obj = new Quadratic();
// Given value of coefficients
int a = 1, b = -7, c = 12;
obj.findRoots(a, b, c);
}
}
// This code is contributed by Princi Singh
Javascript
Roots are real and different
4.0
3.0
时间复杂度: O(√N)
辅助空间: O(1)