- Matlab 教程
- MATLAB - 首頁
- MATLAB - 概述
- MATLAB - 特性
- MATLAB - 環境設定
- MATLAB - 編輯器
- MATLAB - 線上
- MATLAB - 工作區
- MATLAB - 語法
- MATLAB - 變數
- MATLAB - 命令
- MATLAB - 資料型別
- MATLAB - 運算子
- MATLAB - 日期和時間
- MATLAB - 數字
- MATLAB - 隨機數
- MATLAB - 字串和字元
- MATLAB - 文字格式化
- MATLAB - 時間表
- MATLAB - M 檔案
- MATLAB - 冒號表示法
- MATLAB - 資料匯入
- MATLAB - 資料輸出
- MATLAB - 歸一化資料
- MATLAB - 預定義變數
- MATLAB - 決策
- MATLAB - 決策
- MATLAB - If End 語句
- MATLAB - If Else 語句
- MATLAB - If…Elseif Else 語句
- MATLAB - 巢狀 If 語句
- MATLAB - Switch 語句
- MATLAB - 巢狀 Switch
- MATLAB - 迴圈
- MATLAB - 迴圈
- MATLAB - For 迴圈
- MATLAB - While 迴圈
- MATLAB - 巢狀迴圈
- MATLAB - Break 語句
- MATLAB - Continue 語句
- MATLAB - End 語句
- MATLAB - 陣列
- MATLAB - 陣列
- MATLAB - 向量
- MATLAB - 轉置運算子
- MATLAB - 陣列索引
- MATLAB - 多維陣列
- MATLAB - 相容陣列
- MATLAB - 分類陣列
- MATLAB - 元胞陣列
- MATLAB - 矩陣
- MATLAB - 稀疏矩陣
- MATLAB - 表格
- MATLAB - 結構體
- MATLAB - 陣列乘法
- MATLAB - 陣列除法
- MATLAB - 陣列函式
- MATLAB - 函式
- MATLAB - 函式
- MATLAB - 函式引數
- MATLAB - 匿名函式
- MATLAB - 巢狀函式
- MATLAB - Return 語句
- MATLAB - 空函式
- MATLAB - 區域性函式
- MATLAB - 全域性變數
- MATLAB - 函式控制代碼
- MATLAB - Filter 函式
- MATLAB - 階乘
- MATLAB - 私有函式
- MATLAB - 子函式
- MATLAB - 遞迴函式
- MATLAB - 函式優先順序順序
- MATLAB - Map 函式
- MATLAB - Mean 函式
- MATLAB - End 函式
- MATLAB - 錯誤處理
- MATLAB - 錯誤處理
- MATLAB - Try...Catch 語句
- MATLAB - 除錯
- MATLAB - 繪圖
- MATLAB - 繪圖
- MATLAB - 繪製陣列
- MATLAB - 繪製向量
- MATLAB - 條形圖
- MATLAB - 直方圖
- MATLAB - 圖形
- MATLAB - 2D 線性圖
- MATLAB - 3D 圖
- MATLAB - 格式化繪圖
- MATLAB - 對數軸圖
- MATLAB - 繪製誤差條
- MATLAB - 繪製 3D 等值線圖
- MATLAB - 極座標圖
- MATLAB - 散點圖
- MATLAB - 繪製表示式或函式
- MATLAB - 繪製矩形
- MATLAB - 繪製頻譜圖
- MATLAB - 繪製網格曲面
- MATLAB - 繪製正弦波
- MATLAB - 插值
- MATLAB - 插值
- MATLAB - 線性插值
- MATLAB - 2D 陣列插值
- MATLAB - 3D 陣列插值
- MATLAB - 多項式
- MATLAB - 多項式
- MATLAB - 多項式加法
- MATLAB - 多項式乘法
- MATLAB - 多項式除法
- MATLAB - 多項式的導數
- MATLAB - 變換
- MATLAB - 變換
- MATLAB - 拉普拉斯變換
- MATLAB - 拉普拉斯濾波器
- MATLAB - 高斯-拉普拉斯濾波器
- MATLAB - 逆傅立葉變換
- MATLAB - 傅立葉變換
- MATLAB - 快速傅立葉變換
- MATLAB - 2D 逆餘弦變換
- MATLAB - 為座標軸新增圖例
- MATLAB - 面向物件
- MATLAB - 面向物件程式設計
- MATLAB - 類和物件
- MATLAB - 函式過載
- MATLAB - 運算子過載
- MATLAB - 使用者定義類
- MATLAB - 複製物件
- MATLAB - 代數
- MATLAB - 線性代數
- MATLAB - 高斯消去法
- MATLAB - 高斯-約旦消去法
- MATLAB - 簡化行階梯形式
- MATLAB - 特徵值和特徵向量
- MATLAB - 積分
- MATLAB - 積分
- MATLAB - 二重積分
- MATLAB - 梯形法則
- MATLAB - 辛普森法則
- MATLAB - 雜項
- MATLAB - 微積分
- MATLAB - 微分
- MATLAB - 矩陣的逆
- MATLAB - GNU Octave
- MATLAB - Simulink
- MATLAB - 有用資源
- MATLAB - 快速指南
- MATLAB - 有用資源
- MATLAB - 討論
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 中展開和收集方程
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 中展開和收集方程
您需要安裝 symbolic 包,該包分別提供 expand 和 collect 函式來展開和收集方程。以下示例演示了這些概念:
當您使用許多符號函式時,您應該宣告您的變數是符號變數,但 Octave 定義符號變數的方法有所不同。請注意 Sin 和 Cos 的用法,它們也在 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