目录

MATLAB - Algebra

到目前为止,我们已经看到所有示例都在MATLAB以及GNU中工作,或者称为Octave。 但是对于求解基本代数方程,MATLAB和Octave都没什么不同,所以我们将尝试在单独的部分中介绍MATLAB和Octave。

我们还将讨论代数表达式的分解和简化。

在MATLAB中求解基本代数方程

solve函数用于求解代数方程。 在最简单的形式中,solve函数将引号括起来的等式作为参数。

例如,让我们在等式x-5 = 0中求解x

solve('x-5=0')

MATLAB将执行上述语句并返回以下结果 -

ans =
   5

您也可以将求解函数称为 -

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

MATLAB将执行上述语句并返回以下结果 -

y =
   5

您甚至可能不包括等式的右侧 -

solve('x-5')

MATLAB将执行上述语句并返回以下结果 -

ans =
   5

如果方程涉及多个符号,那么默认情况下MATLAB假设您正在求x,但是,求解函数有另一种形式 -

solve(equation, variable)

在哪里,你还可以提到变量。

例如,让我们解决方程v - u - 3t 2 = 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

您也可以将求解函数称为 -

y = roots([1, -5])

Octave将执行上述声明并返回以下结果 -

y = 5

在MATLAB中求解二次方程

solve函数也可以求解高阶方程。 它通常用于求解二次方程。 该函数返回数组中方程的根。

以下示例解决了二次方程式x 2 -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中求解二次方程x 2 -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

在高阶方程的情况下,根很长,包含许多项。 您可以通过将它们转换为double来获得此类根的数值。 以下示例解决了四阶方程x 4 - 7x 3 + 3x 2 - 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中的高阶方程

以下示例解决了四阶方程x 4 - 7x 3 + 3x 2 - 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的使用,它们也在符号包中定义。

创建一个脚本文件并键入以下代码 -

% 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函数是表达式的factorsimplify函数简化了表达式。 以下示例演示了该概念 -

例子 (Example)

创建一个脚本文件并键入以下代码 -

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 - Graphics
MATLAB - Calculus.下一篇>
↑回到顶部↑
WIKI教程 @2018