到目前為止,我們已經(jīng)看到所有的例子都在MATLAB以及它的GNU,或者稱(chēng)為Octave。 但是,為了求解基本代數(shù)方程,MATLAB和Octave都不同,所以這里將分別介紹MATLAB和Octave。
我們還將討論代數(shù)表達(dá)式的分解和簡(jiǎn)化。
solve
函數(shù)用于求解代數(shù)方程。 在其最簡(jiǎn)單的形式中,solve
函數(shù)將引用中的方程式作為參數(shù)。
例如,在等式x-5 = 0
中求解x
,參考以下代碼實(shí)現(xiàn) -
solve('x-178=0')
MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果 -
Trial>> solve('x-178=0')
ans =
178
也可以這樣調(diào)用solve
函數(shù) -
Trial>> solve('x-110 = 0')
ans =
110
甚至可以不用包括方程的右側(cè)部分 -
Trial>> solve('x-110')
ans =
110
如果方程式涉及多個(gè)符號(hào),則默認(rèn)情況下,MATLAB假定正在求解x
,但是,solve
函數(shù)具有另一種形式 -
solve(equation, variable)
其中,也可以涉及到變量。
例如,要求解v - u - 3t^2 = 0
(這里為t
的平方),對(duì)于v
,在這種情況下,應(yīng)該書(shū)寫(xiě)為 -
solve('v-u-3*t^2=0', 'v')
MATLAB執(zhí)行上述語(yǔ)句將返回以下結(jié)果 -
ans =
3*t^2 + u
roots
函數(shù)用于求解代數(shù)中的代數(shù)方程,可以重寫(xiě)上面的例子如下:
例如,要在等式x-5 = 0
中求解x
的值 -
roots([1, -5])
執(zhí)行上面示例代碼,得到以下結(jié)果 -
Trial>> roots([1, -5])
ans =
5
也可以這樣調(diào)用roots
函數(shù) -
y = roots([1, -5])
執(zhí)行上面示例代碼,得到以下結(jié)果 -
Trial>> y = roots([1, -5])
y =
5
solve
函數(shù)也可以用來(lái)求解高階方程。通常用于求解二次方程。 該函數(shù)返回?cái)?shù)組中方程的根。
以下示例求解二次方程x^2 -7x +12 = 0
(注:x^2
表示x
的平方)。創(chuàng)建腳本文件并鍵入以下代碼 -
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));
執(zhí)行上面示例代碼,得到以下結(jié)果 -
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中的二次方程x^2-7x +12 = 0
。創(chuàng)建腳本文件并鍵入以下代碼 -
s = roots([1, -7, 12]);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
執(zhí)行上面示例代碼,得到以下結(jié)果 -
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
solve
函數(shù)也可以解決高階方程。例如,下面演示求解(x-3)^2(x-7)= 0
(注:(x-3)^2
表示(x-3)
的平方)的三次方程 -
MATLAB執(zhí)行上述語(yǔ)句將返回以下結(jié)果 -
ans =
3
3
7
在較高階方程的情況下,根很長(zhǎng),包含很多項(xiàng)??梢酝ㄟ^(guò)將這些根的數(shù)值轉(zhuǎn)換為double
來(lái)獲得數(shù)值。 以下示例解決四階方程x^4 - 7x^3 + 3x^2 - 5x + 9 = 0
(注:x^4
表示x
的4
次方)。
創(chuàng)建腳本文件并鍵入以下代碼 -
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執(zhí)行上述語(yǔ)句將返回以下結(jié)果 -
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
請(qǐng)注意,最后兩個(gè)根是復(fù)數(shù)。
以下示例示解四階方程:x^4 - 7x3 + 3x^2 - 5x + 9 = 0
。
創(chuàng)建腳本文件并鍵入以下代碼 -
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執(zhí)行上述語(yǔ)句將返回以下結(jié)果 -
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
solve
函數(shù)也可用于生成包含多個(gè)變量的方程組的解。下面來(lái)看一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明這一點(diǎn)。
下面來(lái)求解方程式 -
5x + 9y = 5
3x – 6y = 4
創(chuàng)建腳本文件并鍵入以下代碼 -
s = solve('5*x + 9*y = 5','3*x - 6*y = 4');
x = s.x
y = s.y
MATLAB執(zhí)行上述語(yǔ)句將返回以下結(jié)果 -
x =
22/19
y =
-5/57
同樣,可以示解決更大的線性系統(tǒng)。 考慮以下一組方程式 -
x + 3y -2z = 5
3x + 5y + 6z = 7
2x + 4y + 3z = 8
還可以使用不同的方法來(lái)示解n
未知數(shù)的n
線性方程組。下面來(lái)看一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明這一點(diǎn)。
假設(shè)要示解方程式 -
5x + 9y = 5
3x – 6y = 4
這種線性方程組可以寫(xiě)成單矩陣方程Ax = b
,其中A
是系數(shù)矩陣,b
是包含線性方程右邊的列向量,x
是表示解的方法的列向量。如下圖所示 -
創(chuàng)建腳本文件并鍵入以下代碼 -
A = [5, 9; 3, -6];
b = [5;4];
A \ b
執(zhí)行上面示例代碼,得到以下結(jié)果 -
ans =
1.157895
-0.087719
同樣,可以示解下面給出的較大的方程組 -
x + 3y -2z = 5
3x + 5y + 6z = 7
2x + 4y + 3z = 8
expand
和 collect
函數(shù)分別擴(kuò)展和集合方程。以下示例演示了這些概念 -
當(dāng)使用許多符號(hào)功能時(shí),應(yīng)該聲明變量為符號(hào)。
創(chuàng)建腳本文件并鍵入以下代碼 -
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))
執(zhí)行上面示例代碼,得到以下結(jié)果 -
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
需要有symbolic
包,它提供了expand
和collect
函數(shù)來(lái)分別擴(kuò)展和集合方程。 以下示例演示了這些概念 -
當(dāng)使用許多符號(hào)功能時(shí),應(yīng)該聲明變量是符號(hào),但是Octave
具有不同的方法來(lái)定義符號(hào)變量。注意使用的是Sin
和Cos
,它們是定義在symbolic
包中的。
創(chuàng)建腳本文件并鍵入以下代碼 -
% 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)
運(yùn)行文件時(shí),會(huì)顯示以下結(jié)果 -
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)
因子函數(shù)將表達(dá)式分解,簡(jiǎn)化函數(shù)簡(jiǎn)化表達(dá)式。 以下示例演示了這一概念 -
示例
創(chuàng)建腳本文件并鍵入以下代碼 -
syms x
syms y
factor(x^3 - y^3)
f = factor(y^2*x^2,x)
simplify((x^4-16)/(x^2-4))
執(zhí)行上面示例代碼,得到以下結(jié)果 -
Trial>> factorization
ans =
[ x - y, x^2 + x*y + y^2]
f =
[ y^2, x, x]
ans =
x^2 + 4