×
MATLAB 介绍MATLAB 安装MATLAB 基本语法MATLAB 变量MATLAB 命令MATLAB M-FilesMATLAB 数据类型MATLAB 运算符MATLAB 算术运算MATLAB 逻辑运算MATLAB 关系运算MATLAB 位运算MATLAB 集合操作MATLAB 决策制定MATLAB if...end 语句MATLAB if...else...end 语句MATLAB if...elseif...else...endMATLAB 嵌套if语句MATLAB switch语句MATLAB 嵌套switch语句MATLAB 循环类型MATLAB while循环MATLAB for循环MATLAB 嵌套循环MATLAB break语句MATLAB continue语句MATLAB 向量MATLAB 向量的加法和减法MATLAB 标量向量乘法MATLAB 转置向量MATLAB 追加向量MATLAB 向量的模MATLAB 向量点积MATLAB 等差元素向量MATLAB 矩阵MATLAB 矩阵的加法和减法MATLAB 除法(左,右)矩阵MATLAB 矩阵标量操作MATLAB 矩阵的转置MATLAB 串联矩阵MATLAB 矩阵乘法MATLAB 矩阵的行列式MATLAB 逆矩阵MATLAB 数组MATLAB 冒号符号MATLAB 数字MATLAB 字符串MATLAB 函数MATLAB 数据导入MATLAB 数据导出MATLAB 绘图MATLAB 图形MATLAB 代数MATLAB 微积分MATLAB 多项式MATLAB 变换MATLAB GNU Octave教程MATLAB Simulink 仿真

MATLAB代数


到本节为止,我们已经看到,所有的例子 MATLAB 方式工作以及GNU(或者称为Octave)。但是在解决基本的代数方程的问题上,MATLAB 和 Octave 有点差别,因此对于 MATLAB 和 octave 会单独分开介绍。

对于因式分解以及简化代数表达式,我们也会进=进行接触。

在MATLAB解决基本的代数方程组

MATLAB 中使用 solve 命令求解代数方程组。在其最简单的形式,solve 函数需要括在引号作为参数方程。

例如,让我们在方程求解 x, x-5 = 0

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 – 3t2 = 0, 或 v 在这种情况下,我们应该这样写:

solve('v-u-3*t^2=0', 'v')

MATLAB执行上述语句,返回以下结果:

ans =
 3*t^2 + u

MATLAB解决基本在Octave中代数方程组

根命令用于求解代数方程组 Octave ,可以写上面的例子如下:

例如,让我们在方程求解x , x-5 = 0

roots([1, -5])

Octave 执行上述语句,返回以下结果:

ans =
 5

还可以调用求解函数为:

y = roots([1, -5])

Octave 执行上述语句,返回以下结果:

y =
 5

在MATLAB解决二次方程

solve 命令也可以解决高阶方程。它经常被用来求解二次方程,该函数返回在数组中的方程的根。

下面举例子解决二次方程 x2 -7x +12 = 0。

在MATLAB中建立一个脚本文件,并输入下述代码:

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 在 Octave 中。创建立一个脚本文件,并输入下述代码:

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

在高阶方程的情况下,根长含有许多术语。可以得到的数值如根,把它们转换成一倍。

下面的例子解决了四阶方程 x4 − 7x3 + 3x2 − 5x + 9 = 0.

在MATLAB中建立一个脚本文件,并输入下述代码:

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求解高阶方程

下面的例子解决了四阶方程 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

在MATLAB中建立一个脚本文件,并输入下述代码:

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扩大和收集方程

MATLAB中 expand 和 collect 命令用于扩展,并分别收集一个方程。下面的示例演示的概念:

当工作中有许多象征性的函数,你应当声明你的变量是象征意义的。

在MATLAB中建立一个脚本文件,并输入下述代码:

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 包,它提供了expand 和 collect 命令来扩大和收集方程。下面的示例演示的概念:

当工作中有许多象征意义的函数,应该声明变量是象征性的,但八度有不同的方法来定义符号变量。注意使用 sin 和 cos,他们还象征意义性的包中定义。

建立一个脚本文件,并输入下述代码:

% 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 命令表达式 factorizes 是一个简化命令的简化表达。

具体例子

建立一个脚本文件,并输入下述代码:

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

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)