MATLAB - 代数(Algebra)

MATLAB - 代数(Algebra) 首页 / MATLAB入门教程 / MATLAB - 代数(Algebra)

到目前为止,我们已经看到所有示例都可以在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的方程v – u – 3t 2 =0。在这种情况下,我们应该写成-

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 函数也可以求解高阶方程,它通常用于求解二次方程,该函数以数组形式返回方程式的根。

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

下面的示例以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+8y=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   % 符号变量 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

Octave扩展和收集方程式

您需要具有符号软件包,该软件包提供 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 函数将表达式分解,而 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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构核心20讲 -〔杨波〕

Nginx核心知识150讲 -〔陶辉〕

.NET Core开发实战 -〔肖伟宇〕

编译原理实战课 -〔宫文学〕

体验设计案例课 -〔炒炒〕

林外 · 专利写作第一课 -〔林外〕

Vue 3 企业级项目实战课 -〔杨文坚〕

Rust 语言从入门到实战 -〔唐刚〕

徐昊 · AI 时代的软件工程 -〔徐昊〕

好记忆不如烂笔头。留下您的足迹吧 :)