教程 > > 阅读:31

matlab 代数——迹忆客-ag捕鱼王app官网

到目前为止,我们已经看到所有的例子都可以在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(方程,变量)

在其中,你也可以提到变量。

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

我们也可以调用 solve 函数

y = roots([1, -5])

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

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中解决二次方程

以下示例在 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中解决高阶方程

solve 函数也可以解决高阶方程。例如,让我们解决一个立方方程 (x-3)2(x-7) = 0

solve('(x-3)^2*(x-7)=0')

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

ans =
   3
   3
   7

在高阶方程的情况下,根是长的,包含许多术语。您可以通过将它们转换为double来获得这些根的数值。以下示例解决了四次方程 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);
%将根转换为double类型
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 中扩展和收集方程

在matlab中展开和收集方程的功能由 expandcollect 函数提供。以下示例演示了这些概念:

当我们使用许多符号函数时,应声明我们的变量是符号变量。

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

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中展开和收集方程的功能也由 expandcollect 函数提供,但需要安装符号软件包。以下示例演示了这些概念

当您使用许多符号函数时,应声明您的变量是符号变量,但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

查看笔记

扫码一下
查看教程更方便
网站地图