二次方程的求解(重写)
复数的价值体现在它能使运算简化。
例如,我们在例3.2中已解决的二次方程的求解问题,但它根据判别式用到3个选项的选择结构,由于复数的出现,负数的平方根的处理将不困难。所以能够大大简化我们的计算。编写一个普通的程序,解一元二次方程的根,不管是什么类型的。用复变量,而不用选择结构。
1. 陈述问题
编写一个程序,解一元二次方程的根,不管有两个不同的实根,还是用两个相同的实根或两个不同复根。不需要检测判别式。
2. 定义输入输出
本程序所需要方程式
ax2 + bx + c = 0 (3.1)
的三个系数a,b,c。输出是这个方程式的所有根。
3. 设计算法
这个程序从整体上可以分为三大步,即输入,计算,输出
Read the input data
Calculate the roots
Write out the roots
我们现在把每一步进行逐步细化。这时判别式的值对程序的执行过程不产生影响。伪代码如下:
Prompt the user for the coefficients a, b, and c.
Read a, b, and c
discriminant ← b^2 - 4 * a * c
x1 ←( -b +sqrt(discriminant) ) / (2*a)
x2 ←( -b -sqrt(discriminant) ) / (2*a)
Print 'x1 = ' , real(x1), ' + i ', imag(x1)
Print 'x2 = ' , real(x2), ' + i ', imag(x2)
4. 将算法转化为MATLAB语句
% Script file: calc_roots2.m
%
% Purpose:
% This program solves for the roots of a quadratic equation
% of the form a*x**2 + b*x + c = 0. It calculates the answers
% regardless of the type of roots that the equation possesses.
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 12/06/98 S. J. Chapman Original code
%
% Define variables:
% a -- Coefficient of x^2 term of equation
% b -- Coefficient of x term of equation
% c -- Constant term of equation
% discriminant -- Discriminant of the equation
% x1 -- First solution of equation
% x2 -- Second solution of equation
% Prompt the user for the coefficients of the equation
disp ('This program solves for the roots of a quadratic ');
disp ('equation of the form A*X^2 + B*X + C = 0. ');
a = input ('Enter the coefficient A: ');
b = input ('Enter the coefficient B: ');
c = input ('Enter the coefficient C: ');
% Calculate discriminant
discriminant = b^2 - 4 * a * c;
% Solve for the roots
x1 = ( -b + sqrt(discriminant) ) / ( 2 * a );
x2 = ( -b - sqrt(discriminant) ) / ( 2 * a );
% Display results
disp ('The roots of this equation are:');
fprintf ('x1 = (%f) +i (%f)\n', real(x1), imag(x1));
fprintf ('x2 = (%f) +i (%f)\n', real(x2), imag(x2));
5.检测程序下一步,我们必须输入检测来检测程序。我们要有三组数据进行检测,其判别式分别大于0,等于0,小于0。根据方程式(3。1),用下面的方程式验证程序。
x2 + 5x + 6 = 0 x= -2, x = -3
x2 + 4x + 4 = 0 x= -2
x2 + 2x + 5 = 0 x= -1 ± 2i
我们把它们的系数分别输入程序,结果如下
>> calc_root2
This program solves for the roots of a quadratic
equation of the form A*X^2 + B*X + C = 0.
Enter the coefficient A: 1
Enter the coefficient B: 5
Enter the coefficient C: 6
The roots of this equation are:
x1 = (-2.000000) +i (0.000000)
x2 = (-3.000000) +i (0.000000)
>> calc_root2
This program solves for the roots of a quadratic
equation of the form A*X^2 + B*X + C = 0.
Enter the coefficient A: 1
Enter the coefficient B: 4
Enter the coefficient C: 4
The roots of this equation are:
x1 = (-2.000000) +i (0.000000)
x2 = (-2.000000) +i (0.000000)
>> calc_root2
This program solves for the roots of a quadratic
equation of the form A*X^2 + B*X + C = 0.
Enter the coefficient A: 1
Enter the coefficient B: 2
Enter the coefficient C: 5
The roots of this equation are:
x1 = (-1.000000) +i (2.000000)
x2 = (-1.000000) +i (-2.000000)
在三种不同的情况下,程序均给出了正确的结果。注意此程序与例3.2中的程序相比有多简单。复数数据的应用可大大简化我们的程序。