Matlab代數(方程求解)


到目前為止,我們已經看到所有的例子都在MATLAB以及它的GNU,或者稱為Octave。 但是,為了求解基本代數方程,MATLAB和Octave都不同,所以這裡將分別介紹MATLAB和Octave。

我們還將討論代數表示式的分解和簡化。

在MATLAB中求解基本代數方程

solve函式用於求解代數方程。 在其最簡單的形式中,solve函式將參照中的方程式作為引數。

例如,在等式x-5 = 0中求解x,參考以下程式碼實現 -

solve('x-178=0')

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

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

178

也可以這樣呼叫solve函式 -

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

110

甚至可以不用包括方程的右側部分 -

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

110

如果方程式涉及多個符號,則預設情況下,MATLAB假定正在求解x,但是,solve函式具有另一種形式 -

solve(equation, variable)

其中,也可以涉及到變數。

例如,要求解v - u - 3t^2 = 0(這裡為t的平方),對於v,在這種情況下,應該書寫為 -

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

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

ans =
 3*t^2 + u

求解代數中的基本代數方程

roots函式用於求解代數中的代數方程,可以重寫上面的例子如下:

例如,要在等式x-5 = 0中求解x的值 -

roots([1, -5])

執行上面範例程式碼,得到以下結果 -

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

ans =

     5

也可以這樣呼叫roots函式 -

y = roots([1, -5])

執行上面範例程式碼,得到以下結果 -

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

y =

     5

在MATLAB中求解二次方程

solve函式也可以用來求解高階方程。通常用於求解二次方程。 該函式返回陣列中方程的根。

以下範例求解二次方程x^2 -7x +12 = 0(註:x^2表示x的平方)。建立指令碼檔案並鍵入以下程式碼 -

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中求解二次方程

以下範例解決Octave中的二次方程x^2-7x +12 = 0。建立指令碼檔案並鍵入以下程式碼 -

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

在較高階方程的情況下,根很長,包含很多項。可以通過將這些根的數值轉換為double來獲得數值。 以下範例解決四階方程x^4 - 7x^3 + 3x^2 - 5x + 9 = 0(註:x^4表示x4次方)。

建立指令碼檔案並鍵入以下程式碼 -

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中求解高階方程

以下範例示解四階方程:x^4 - 7x3 + 3x^2 - 5x + 9 = 0

建立指令碼檔案並鍵入以下程式碼 -

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中求解方程組

還可以使用不同的方法來示解n未知數的n線性方程組。下面來看一個簡單的例子來說明這一點。

假設要示解方程式 -

5x + 9y = 5

3x – 6y = 4

這種線性方程組可以寫成單矩陣方程Ax = b,其中A是係數矩陣,b是包含線性方程右邊的列向量,x是表示解的方法的列向量。如下圖所示 -

建立指令碼檔案並鍵入以下程式碼 -

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中擴充套件和集合方程

expandcollect函式分別擴充套件和集合方程。以下範例演示了這些概念 -

當使用許多符號功能時,應該宣告變數為符號。

建立指令碼檔案並鍵入以下程式碼 -

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擴充套件和集合方程

需要有symbolic包,它提供了expandcollect函式來分別擴充套件和集合方程。 以下範例演示了這些概念 -

當使用許多符號功能時,應該宣告變數是符號,但是Octave具有不同的方法來定義符號變數。注意使用的是SinCos,它們是定義在symbolic包中的。

建立指令碼檔案並鍵入以下程式碼 -

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

% 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