到本节为止,我们已经看到,所有的例子 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 ,可以写上面的例子如下:
例如,让我们在方程求解x , x-5 = 0
roots([1, -5])
Octave 执行上述语句,返回以下结果:
ans = 5
还可以调用求解函数为:
y = roots([1, -5])
Octave 执行上述语句,返回以下结果:
y = 5
solve 命令也可以解决高阶方程。它经常被用来求解二次方程,该函数返回在数组中的方程的根。
下面举例子解决二次方程 x2 -7x +12 = 0。
在MATLAB中建立一个脚本文件,并输入下述代码:
s = solve(x^2 -7*x + 12 == 0);
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
下面的例子解决二次方程 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
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
请注意,在过去的两个根是复数。
下面的例子解决了四阶方程 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
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
我们有一点点不同的方法来解决系统 "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 命令用于扩展,并分别收集一个方程。下面的示例演示的概念:
当工作中有许多象征性的函数,你应当声明你的变量是象征意义的。
在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
你需要 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
运行外部程序Julia 使用倒引号 ` 来运行外部程序:julia `echo hello``echo hello`它有以下几个特性:倒引号并不直接运行程序,...
本节介绍 ASP.NETMVC 应用程序中每个页面的布局以及样式该如何设置。为了学习 ASP.NET MVC,我们将构建一个 Internet 应用程序。...
File 表示一个连接到普通文件的 stdio 对象。open 为普通文件返回该类的一个实例。 类方法 序号方法描述1File::atime( path)返回...
HTTP协议是无状态协议。但对于一个商业网站,它需要保持不同的页面间的会话信息。如用户在网站注册过程中需要跳转页面,但又要保...
RubyGems 是 Ruby 的一个包管理器,它提供一个分发 Ruby 程序和库的标准格式,还提供一个管理程序包安装的工具。RubyGems 旨在方...