# Matlab代數(方程求解)

## 在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執行上述語句將返回以下結果 -

``````The first root is:
root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 1)

The second root is:
root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 2)

The third root is:
root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 3)

The fourth root is:
root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 4)

Numeric value of first root
1.0598

Numeric value of second root
6.6304

Numeric value of third root
-0.3451 - 1.0778i

Numeric value of fourth root
-0.3451 + 1.0778i``````

## 在Octave中求解高階方程

``````v = [1, -7,  3, -5, 9];

s = roots(v);
% 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執行上述語句將返回以下結果 -

``````Trial>> v = [1, -7,  3, -5, 9];

s = roots(v);
% 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)));
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``````

## MATLAB中求解方程組

`solve`函數也可用於生成包含多個變量的方程組的解。下面來看一個簡單的例子來說明這一點。

``````5x + 9y = 5

3x – 6y = 4``````

``````s = solve('5*x + 9*y = 5','3*x - 6*y = 4');
x = s.x
y = s.y``````

MATLAB執行上述語句將返回以下結果 -

``````x =

22/19

y =

-5/57``````

``````x + 3y -2z = 5

3x + 5y + 6z = 7

2x + 4y + 3z = 8``````

## 在Octave中求解方程組

``````5x + 9y = 5

3x – 6y = 4``````

``````A = [5, 9; 3, -6];
b = [5;4];
A \ b``````

``````ans =

1.157895
-0.087719``````

``````x + 3y -2z = 5

3x + 5y + 6z = 7

2x + 4y + 3z = 8``````

## 在MATLAB中擴展和集合方程

`expand``collect`函數分別擴展和集合方程。以下示例演示了這些概念 -

``````syms x %symbolic variable x
syms y %symbolic variable x
% expanding equations
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(sin(2*x))
expand(cos(x+y))

% collecting equations
collect(x^3 *(x-7))
collect(x^4*(x-3)*(x-5))``````

`````` ans =
x^2 + 4*x - 45
ans =
x^4 + x^3 - 43*x^2 + 23*x + 210
ans =
2*cos(x)*sin(x)
ans =
cos(x)*cos(y) - sin(x)*sin(y)
ans =
x^4 - 7*x^3
ans =
x^6 - 8*x^5 + 15*x^4``````

## 在Octave擴展和集合方程

``````% first of all load the package, make sure its installed.

% make symbols module available
symbols

% define symbolic variables
x = sym ('x');
y = sym ('y');
z = sym ('z');

% expanding equations
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(Sin(2*x))
expand(Cos(x+y))

% collecting equations
collect(x^3 *(x-7), z)
collect(x^4*(x-3)*(x-5), z)``````

``````ans =

-45.0+x^2+(4.0)*x
ans =

210.0+x^4-(43.0)*x^2+x^3+(23.0)*x
ans =

sin((2.0)*x)
ans =

cos(y+x)
ans =

x^(3.0)*(-7.0+x)
ans =

(-3.0+x)*x^(4.0)*(-5.0+x)``````

## 代數表達式的因式分解和簡化

``````syms x
syms y
factor(x^3 - y^3)
f = factor(y^2*x^2,x)
simplify((x^4-16)/(x^2-4))``````

``````Trial>> factorization

ans =

[ x - y, x^2 + x*y + y^2]

f =

[ y^2, x, x]

ans =

x^2 + 4``````