MATLAB - 代數



到目前為止,我們已經看到所有示例都可以在 MATLAB 以及其 GNU 版本(也稱為 Octave)中執行。但是,對於解決基本的代數方程,MATLAB 和 Octave 略有不同,因此我們將嘗試在單獨的部分中介紹 MATLAB 和 Octave。

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

在 MATLAB 中解決基本的代數方程

solve 函式用於解決代數方程。在最簡單的形式中,solve 函式將用引號括起來的方程作為引數。

例如,讓我們求解方程 x-5 = 0 中的 x

solve('x-5=0')

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

ans =
   5

您還可以這樣呼叫 solve 函式:

y = solve('x-5 = 0')

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

y =
   5

您甚至可以不包含方程的右側:

solve('x-5')

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

ans =
   5

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

solve(equation, variable)

其中,您也可以提及變數。

例如,讓我們求解方程 v – u – 3t2 = 0 中的 v。在這種情況下,我們應該寫:

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

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

ans =
   3*t^2 + u

在 Octave 中解決基本的代數方程

roots 函式用於在 Octave 中解決代數方程,您可以將上述示例編寫如下:

例如,讓我們求解方程 x-5 = 0 中的 x

roots([1, -5])

Octave 將執行上述語句並返回以下結果:

ans = 5

您還可以這樣呼叫 solve 函式:

y = roots([1, -5])

Octave 將執行上述語句並返回以下結果:

y = 5

在 MATLAB 中解決二次方程

solve 函式還可以解決高階方程。它通常用於解決二次方程。該函式返回方程的根,以陣列的形式。

以下示例求解二次方程 x2 -7x +12 = 0。建立一個指令碼檔案並鍵入以下程式碼:

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 中求解二次方程 x2 -7x +12 = 0。建立一個指令碼檔案並鍵入以下程式碼:

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

solve('(x-3)^2*(x-7)=0')

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

ans =
   3
   3
   7

對於高階方程,根很長,包含許多項。您可以透過將它們轉換為雙精度數來獲得此類根的數值。以下示例求解四階方程 x4 − 7x3 + 3x2 − 5x + 9 = 0。

建立一個指令碼檔案並鍵入以下程式碼:

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)));

執行檔案時,它會返回以下結果:

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

以下示例求解四階方程 x4 − 7x3 + 3x2 − 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)));

執行檔案時,它會返回以下結果:

Numeric value of first root
 6.6304
Numeric value of second root
-0.34509 + 1.07784i
Numeric value of third root
-0.34509 - 1.07784i
Numeric value of fourth root
 1.0598

在 MATLAB 中解決方程組

solve 函式還可以用於生成涉及多個變數的方程組的解。讓我們舉一個簡單的例子來演示此用法。

讓我們求解以下方程:

5x + 9y = 5

3x – 6y = 4

建立一個指令碼檔案並鍵入以下程式碼:

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

執行檔案時,它會顯示以下結果:

ans =
   22/19
ans =
   -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)

代數表示式的因式分解和化簡

factor 函式對錶達式進行因式分解,simplify 函式對錶達式進行化簡。以下示例演示了該概念:

示例

建立一個指令碼檔案並鍵入以下程式碼:

syms x
syms y
factor(x^3 - y^3)
factor([x^2-y^2,x^3+y^3])
simplify((x^4-16)/(x^2-4))

執行檔案時,它會顯示以下結果:

ans =
   (x - y)*(x^2 + x*y + y^2)
ans =
   [ (x - y)*(x + y), (x + y)*(x^2 - x*y + y^2)]
ans =
   x^2 + 4
廣告
© . All rights reserved.