## 在MATLAB中求解根基代数方程

`solve`函数用于求解代数方程。 在其最简朴的形式中，`solve`函数将引用中的方程式作为参数。

``````solve('x-178=0')
``````

MATLAB将执行上述语句并返回以下功效 -

``````Trial>> solve('x-178=0')
ans =

178
``````

``````Trial>> solve('x-110 = 0')
ans =

110
``````

``````Trial>> solve('x-110')
ans =

110
``````

``````solve(equation, variable)
``````

``````solve('v-u-3*t^2=0', 'v')
``````

MATLAB执行上述语句将返回以下功效 -

``````ans =
3*t^2 + u
``````

## 求解代数中的根基代数方程

`roots`函数用于求解代数中的代数方程，可以重写上面的例子如下：

``````roots([1, -5])
``````

``````Trial>> roots([1, -5])

ans =

5
``````

``````y = roots([1, -5])
``````

``````Trial>> y = roots([1, -5])

y =

5
``````

## 在MATLAB中求解二次方程

`solve`函数也可以用来求解高阶方程。凡是用于求解二次方程。 该函数返回数组中方程的根。

``````eq = 'x^2 -7*x + 12 = 0';
s = solve(eq);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
``````

``````Trial>> eq = 'x^2 -7*x + 12 = 0';
s = solve(eq);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));

The first root is:
3

The second root is:
4
``````

## 在Octave中求解二次方程

``````s = roots([1, -7, 12]);

disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
``````

``````Trial>> s = roots([1, -7, 12]);

disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
The first root is:
4

The second root is:
3
``````

## 求解MATLAB中的高阶方程

`solve`函数也可以办理高阶方程。譬喻，下面演示求解`(x-3)^2(x-7)= 0`(注：`(x-3)^2`暗示`(x-3)`的平方)的三次方程 -

``````

``````

MATLAB执行上述语句将返回以下功效 -

``````ans =
3
3
7
``````

``````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));
% converting the roots to double type
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)));
``````

Matlab教程

2017-11-02