matlab 代数表达式,MATLAB代数

到目前为止,我们已经看到,所有的例子MATLAB 方式工作以及GNU或者称为Octave 。但解决基本的代数方程,MATLAB和Octave 是有点不同的,所以我们会尽量在单独的章节包括MATLAB和Octave 。

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

在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

解决基本在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. 创建一个脚本文件,并键入下面的代码:

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.

创建一个脚本文件,并键入下面的代码:

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

创建一个脚本文件,并键入下面的代码:

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

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

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

创建一个脚本文件,并输入下面的代码:

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一个简化命令简化表达。下面的例子演示了这一概念:

例子

创建一个脚本文件,并输入下面的代码:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值