MATLAB 代数
到目前为止,我们已经看到了所有的示例都可以在MATLAB以及它的GNU(又称Octave)中工作。但是为了解决基本的代数方程,MATLAB和Octave有一点不同,所以我们单独讨论MATLAB和 Octave。
用MATLAB求解基本代数方程
solve函数用于求解代数方程。在最简单的形式中,solve函数将用引号括起来的方程作为参数。
例如,求解方程x-5 = 0中的x :
solve('x-5=0')
%结果:
ans =
5
也可以写作:
y = solve('x-5 = 0')
% 结果:
y =
5
甚至可以不包括方程的右边 :
solve('x-5')
% 结果:
ans =
5
如果方程包含多个符号,那么MATLAB默认假设你在解x,但是,解函数有另一种形式 :
solve(equation, variable)
可以看到,表达式汇总还可以提到变量。
例如,求解 v – u – 3t2 = 0 :
solve('v-u-3*t^2=0', 'v')
% 结果
ans =
3*t^2 + u
用Octave求解基本代数方程
在Octave中,roots函数用来解决代数方程,可以写将上面的改写如下:
roots([1, -5])
% 结果:
ans = 5
第二个例子:
y = roots([1, -5])
% 结果:
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 求解二次方程
同样,求解二次方程 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求解高阶方程
求解函数也可以求解高阶方程。例如,求解一个三次方程为 (x-3)2(x-7) = 0
solve('(x-3)^2*(x-7)=0')
% 结果:
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));
% 将根转换为双精度类型
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个未知数的线性方程组。让我们用一个简单的例子来演示这种用法。
- 5x + 9y = 5
- 3x – 6y = 4
这样系统的线性方程可以写成一个矩阵方程Ax = b,其中一个是系数矩阵,b是包含右边的列向量的线性方程,x是列向量表示解的一个列向量如下所示的程序
A = [5, 9; 3, -6];
b = [5;4];
A \ b
% 结果
ans =
1.157895
-0.087719
MATLAB展开和合并方程
expand和collect函数分别展开收集方程。下面用一个例子来说明:
当使用许多符号函数时,应该声明变量是符号的。
创建一个脚本文件,并键入以下代码
syms x %符号变量 x
syms y %符号变量 y
% 展开方程
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(sin(2*x))
expand(cos(x+y))
% 合并方程
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
MATLAB展开和合并方程
需要有symbolic包,以提供expand和collect函数来展开和收集等式。下面用一个例子来说明-的概念。
当使用许多符号函数时,应该声明变量是符号的,但Octave有不同的方法来定义符号变量。注意Sin和Cos的使用,它们也是在符号包中定义的。
创建脚本文件 :
% 首先加载软件包,确保已安装。
pkg load symbolic
% 激活符号模块可用
symbols
% 定义符号变量
x = sym ('x');
y = sym ('y');
z = sym ('z');
% 展开方程
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(Sin(2*x))
expand(Cos(x+y))
% 合并方程
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函数分解表达式,简化函数简化表达式。下面用一个例子来说明-这个概念
实例
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

关注公众号,获取一手资讯
“ MATLAB 代数 ” comments 0