eq = 'x^4 - 7*x^3 + 3*x^2 - 5*x + 9 = 0'; s = solve(eq); disp('The first root is: '), disp(s(1)); disp('The second root is: '), disp(s(2)); disp('The third root is: '), disp(s(3)); disp('The fourth root is: '), disp(s(4));
%将根转换为double类型 disp('Numeric value of first root'), disp(double(s(1))); disp('Numeric value of second root'), disp(double(s(2))); disp('Numeric value of third root'), disp(double(s(3))); disp('Numeric value of fourth root'), disp(double(s(4)));
运行文件时,它返回以下结果-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
The first root is: 6.630396332390718431485053218985 The second root is: 1.0597804633025896291682772499885 The third root is: - 0.34508839784665403032666523448675 - 1.0778362954630176596831109269793*i The fourth root is: - 0.34508839784665403032666523448675 + 1.0778362954630176596831109269793*i Numeric value of first root 6.6304 Numeric value of second root 1.0598 Numeric value of third root -0.3451 - 1.0778i Numeric value of fourth root -0.3451 + 1.0778i
请注意,最后两个根是复数。
在Octave中求解高阶方程
以下示例解决了四阶方程x 4 − 7x 3 + 3x 2 − 5x + 9 = 0。
创建一个脚本文件并输入以下代码-
示例
1 2 3 4 5 6 7 8
v = [1, -7, 3, -5, 9]; s = roots(v);
%将根转换为double类型 disp('Numeric value of first root'), disp(double(s(1))); disp('Numeric value of second root'), disp(double(s(2))); disp('Numeric value of third root'), disp(double(s(3))); disp('Numeric value of fourth root'), disp(double(s(4)));