MATLAB 中的微分或導數
在數學中,微分或導數是用於確定函式變化率的基本運算之一。因此,導數對於分析各種科學和工程過程或函式至關重要。我們可以使用各種數學工具(如 MATLAB)來執行導數運算。
在本教程中,我們將探討如何使用 MATLAB 計算給定數學函式的導數。但在開始之前,讓我們先概述一下導數的概念。
什麼是導數?
導數是用於計算函式或過程中發生的變化率的數學運算。在幾何方面,導數被定義為給定數學函式曲線在某一點處的切線的斜率。
在數學上,如果 f 是 x 的函式,即 f(x),則其導數可以表示為 f'(x) 或 df/dx。這裡,f'(x) 表示函式 f(x) 在每個 x 值處的變化率。
在 MATLAB 中,可以使用內建函式“diff()”計算給定數學函式的導數。
“diff”函式可以計算給定數值資料或符號表達式的導數。根據不同的用例,“diff”函式可以採用以下各種不同的語法:
f' = diff(f);
f' = diff(f, n);
f'= diff(f, n, dim);
現在,我們將透過示例程式來探索“diff”函式的每種語法。
(1). 計算函式的一階導數
語法
可以使用以下“diff”函式語法計算函式的一階導數:
f' = diff(f);
請考慮以下 MATLAB 程式,以瞭解如何使用“diff”函式計算函式的一階導數。
示例
% MATLAB program to calculate first derivative of a function
% Declare a symbolic variable
syms x;
% Define a function for variable x
f = x^2 + 5*x + 3;
% Calculate the derivative of the function f
dfdx = diff(f);
% Display the result
disp('Derivative of the function f is:');
disp(dfdx);
輸出
Derivative of the function f is: 2*x + 5
解釋
在此 MATLAB 程式碼中,我們首先建立一個符號變數“x”,然後在此變數中定義函式“f”。接下來,我們使用函式“diff”計算函式“f”的導數,並將結果儲存在變數“dfdx”中。最後,我們使用“disp”函式顯示結果。
(2). 計算函式相對於指定變數的導數
語法
以下“diff”函式語法用於計算函式相對於指定變數的導數。
f' = diff(f, b);
這將計算函式“f”相對於變數“b”的導數。
現在讓我們在 MATLAB 中舉一個示例程式來了解此語法的實現。
示例
% MATLAB program to calculate derivative of function with respect to specified variable
% Declare symbolic variables
syms x y;
% Define a function for variable x and y
f = x^2 + 5*x*y + 3*y^2;
% Calculate the derivative of the function f with respect to y
dfdy = diff(f, y);
% Display the result
disp('Derivative of the function f with respect to y is:');
disp(dfdy);
輸出
Derivative of the function f with respect to y is: 5*x + 6*y
解釋
在此 MATLAB 程式中,我們首先建立兩個變數“x”和“y”。然後,我們為這兩個變數定義一個函式,並將其儲存在變數“f”中。之後,我們使用“diff”函式計算函式“f”相對於變數“y”的導數,並將結果儲存在變數“dfdy”中。最後,我們使用“disp”函式顯示結果。
(3). 計算函式的指定階導數
語法
以下“diff”函式語法用於計算函式的指定階導數:
f' = diff(f, n);
這裡,“f”是函式,“n”是導數的階數。
示例
請考慮以下 MATLAB 程式,以瞭解此函式的實現。
% MATLAB program to calculate derivative of specified order of a function
% Declare a symbolic variable
syms x;
% Define a function for variable x
f = x^3 + 5*x^2 + 3*x;
% Calculate first-order derivative of the function f (n = 1)
dfdx1 = diff(f, 1);
% Calculate second-order derivative of the function f (n = 2)
dfdx2 = diff(f, 2);
% Calculate third-order derivative of the function f (n = 3)
dfdx3 = diff(f, 3);
% Display the results
disp('First order derivative of the function f is:');
disp(dfdx1);
disp('Second order derivative of the function f is:');
disp(dfdx2);
disp('Third order derivative of the function f is:');
disp(dfdx3);
輸出
First order derivative of the function f is: 3*x^2 + 10*x + 3 Second order derivative of the function f is: 6*x + 10 Third order derivative of the function f is: 6
解釋
此程式碼的實現和執行與上述程式碼類似。
(4). 計算向量元素之間的差值
語法
我們可以使用以下“diff”函式語法計算向量元素之間的差值:
D = diff(A);
這裡,A 是輸入向量。
示例
讓我們舉一個例子來了解“diff”函式的此語法。
% MATLAB program to calculate differences between vector elements
% Create a vector
A = [1 2 3 4 5 6 7 8 9 10];
% Calculate the differences between elements of the vector
D = diff(A);
% Display the result
disp('Differences between elements of the vector A is:');
disp(D);
輸出
Differences between elements of the vector A is:
1 1 1 1 1 1 1 1 1
解釋
此 MATLAB 程式碼計算從左開始的輸入向量中兩個連續元素之間的差值。
(5). 計算多維陣列沿指定維度的導數
語法
以下“diff”函式語法用於計算多維陣列沿指定維度的導數:
D = diff(A, n, dim);
這裡,A 是多維陣列,n 是導數的階數,“dim”是指定的維度。
如果“dim = 1”,則沿陣列的行計算導數或差值。如果“dim = 2”,則沿陣列的列計算導數或差值。
示例
以下 MATLAB 程式演示了“diff”函式此語法的實現。
% Create a multidimensional array
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
% Calculate the differences along rows (dim = 1)
D_Row = diff(A, 1, 1);
% Calculate the differences along columns (dim = 2)
D_Column = diff(A, 1, 2);
% Display the original array and differences
disp('The original array A is:');
disp(A);
disp('Differences of A along rows is:');
disp(D_Row);
disp('Differences of A along columns is:');
disp(D_Column);
輸出
The original array A is:
1 2 3
4 5 6
7 8 9
Differences of A along rows is:
3 3 3
3 3 3
Differences of A along columns is:
1 1
1 1
1 1
解釋
此 MATLAB 程式碼計算陣列 A 的行(第一維度)和列(第二維度)之間的差值。
結論
以上是關於 MATLAB 中的導數和微分的所有內容。我們藉助 MATLAB 示例解釋了導數或微分的概念。總之,MATLAB 是一種數字工具,它提供了一個內建函式“diff”來計算給定符號表達式或數值資料集的微分或導數或差值。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP