如何在Octave中声明一个符号矩阵?

26
在MatLab中,您可以很容易地声明符号:
syms a,b
mat = [a,b]

我在尝试复制这段代码到 Octave 中时出现了错误:

> symbols
> a = sym("a")
a =

a
> b = sym("b")
b =

b
> mat = [a,b]
error: octave_base_value::resize (): wrong type argument `ex'
error: octave_base_value::resize (): wrong type argument `<unknown type>'
octave-3.2.3.exe:4:C:\Octave\3.2.3_gcc-4.4.0\bin

如何在Octave中声明一个符号矩阵?

6个回答

17

如果您尚未安装符号计算包,请下载它。可以从Octave命令行或GUI命令行下载。

octave> pkg install -forge symbolic
如果已经安装了Python和SymPy,那么它将会从Octave Forge为您安装该软件包。我使用Google找出如何安装SymPy,如果需要帮助,请联系我。
安装符号软件包后,使用"pkg load"导入包函数,然后使用syms函数来声明符号。
octave> pkg load symbolic

octave> syms a b

这里定义了符号a和b。

octave> syms
Symbolic variables in current scope:
  a
  b

“syms”单独使用会打印出您定义的所有符号。

octave> mat = [a,b]
mat = (sym) [a  b]  (1×2 matrix)

octave:34> mat * 2
ans = (sym) [2⋅a  2⋅b]  (1×2 matrix)

我发现这个软件包在我的机器人操纵课程中计算旋转矩阵非常有帮助。希望对你有所帮助。

以下是我脚本的一部分,提供更多例子:

pkg load symbolic
syms psi phi theta psidot phidot thetadot

RzPsi = [[cos(psi), -sin(psi), 0]; [sin(psi), cos(psi), 0]; [0,0,1]]
RyTheta = [[cos(theta), 0, sin(theta)];[0,1,0];[-sin(theta), 0, cos(theta)]]
RzPhi = [[cos(phi), -sin(phi), 0]; [sin(phi), cos(phi), 0]; [0,0,1]]

RzPsi = (sym 3×3 matrix)

  ⎡cos(ψ)  -sin(ψ)  0⎤
  ⎢                  ⎥
  ⎢sin(ψ)  cos(ψ)   0⎥
  ⎢                  ⎥
  ⎣  0        0     1⎦

RyTheta = (sym 3×3 matrix)

  ⎡cos(θ)   0  sin(θ)⎤
  ⎢                  ⎥
  ⎢   0     1    0   ⎥
  ⎢                  ⎥
  ⎣-sin(θ)  0  cos(θ)⎦

RzPhi = (sym 3×3 matrix)

  ⎡cos(φ)  -sin(φ)  0⎤
  ⎢                  ⎥
  ⎢sin(φ)  cos(φ)   0⎥
  ⎢                  ⎥
  ⎣  0        0     1⎦

octave> RzPhi * RyTheta
ans = (sym 3×3 matrix)

  ⎡cos(φ)⋅cos(θ)   -sin(φ)  sin(θ)⋅cos(φ)⎤
  ⎢                                     ⎥
  ⎢sin(φ)⋅cos(θ)   cos(φ)   sin(φ)⋅sin(θ)⎥
  ⎢                                     ⎥
  ⎣   -sin(θ)        0        cos(θ)    ⎦

9

1
不需要了,我已经安装了符号计算包,但问题仍然存在。 - user649198
1
@Gryllida 很抱歉我已经有一段时间没有使用Octave了,而且不太擅长解决问题。我强烈建议您也在Octave邮件列表上提问:那里有很多乐于助人和知识渊博的人。 - George Profenza
此链接中的页面已不存在。 - Marta Cz-C
@MartaCz-C 谢谢你让我知道,我已经更新了链接,但是在看到这个页面之后,我不确定Symbolic包目前有多可靠 :( http://wiki.octave.org/Symbolic_package - George Profenza
请查看我的答案,使用Octave结构数组:https://dev59.com/UXE95IYBdhLWcg3wEpvo#40824167 - user

7
在安装符号工具箱后(您可以通过发出sudo apt-get install octave-symbolic命令在某些环境中进行安装),您需要执行以下操作:
symbols
x = sym('x')

但要注意,Octave用于操作符号表达式的函数比MATLAB差得多。


1

Octave的符号工具箱基本上是无用的。您无法像在您的情况下那样调整矩阵大小,也无法使用“-”运算符。 例如,您可以对简单的符号操作进行求导:

octave:1> symbols
octave:2> q1 = sym("q1");
octave:3> differentiate(Sin(q1)*Cos(q1),q1)
ans =

-sin(q1)^2+cos(q1)^2

但是,如果您尝试这样做:
octave:6> -Sin(q1)
error: unary operator `-' not implemented for `ex' operands
octave:6> -q1
error: unary operator `-' not implemented for `ex' operands

同样的情况在你的例子中也会发生,即调整包含符号值的矩阵的大小。

1

为了后人,再举一个例子。

我使用 http://octave-online.net/ 来开发和运行这个 Octave 脚本。

注意:我将输出作为注释包含在内以显示结果。

disp("2-state markov chain symbolic analysis");

syms lambda mu

L = [lambda,0]
# L = (sym) [λ  0]  (1×2 matrix)

U = [1;0]
#U =
#   1
#   0

C = [ [1,1]; [lambda,-mu]]
#C = (sym 2×2 matrix)
#  ⎡1  1 ⎤
#  ⎢     ⎥
#  ⎣λ  -μ⎦

C^-1
#ans = (sym 2×2 matrix)
#  ⎡  λ          -1   ⎤
#  ⎢────── + 1  ──────⎥
#  ⎢-λ - μ      -λ - μ⎥
#  ⎢                  ⎥
#  ⎢   -λ         1   ⎥
#  ⎢  ──────    ──────⎥
#  ⎣  -λ - μ    -λ - μ⎦

P = C^-1 * U
#P = (sym 2×1 matrix)
#
#  ⎡  λ       ⎤
#  ⎢────── + 1⎥
#  ⎢-λ - μ    ⎥
#  ⎢          ⎥
#  ⎢   -λ     ⎥
#  ⎢  ──────  ⎥
#  ⎣  -λ - μ  ⎦

lambda_sys = L * C^-1 * U
#lambda_sys = (sym)
#
#    ⎛  λ       ⎞
#  λ⋅⎜────── + 1⎟
#    ⎝-λ - μ    ⎠

0

句柄数组

您可以使用Octave结构数组创建一个类似于符号矩阵的数组:

b(1,1).vector = @sin;
b(1,2).vector = @cos;
b(2,1).vector = @sec;
b(2,2).vector = @csc;

b
b.vector

printf( "\n\nCalling each element:\n" )
b(1,1).vector
b(1,2).vector
b(2,1).vector
b(2,2).vector

printf( "\nCalculatin the sin of 1:\n" )
b(1,1).vector(1)

运行以上代码将返回:

b =

  2x2 struct array containing the fields:

    vector

ans = @sin
ans = @sec
ans = @cos
ans = @csc


Calling each element:
ans = @sin
ans = @cos
ans = @sec
ans = @csc

Calculatin the sin of 1:
ans =  0.841470984807897

符号数组

您可以使用sym("a")sym("b")sym("c")等代替@sin@cos等。

pkg load symbolic;

b(1,1).vector = sym("a");
b(1,2).vector = sym("b");
b(2,1).vector = sym("c");
b(2,2).vector = sym("d");

b
b.vector

printf( "\n\nCalling each element:\n" )
b(1,1).vector
b(1,2).vector
b(2,1).vector
b(2,2).vector

运行以上代码将返回:

b =

  2x2 struct array containing the fields:

    vector

ans = (sym) a
ans = (sym) c
ans = (sym) b
ans = (sym) d


Calling each element:
ans = (sym) a
ans = (sym) b
ans = (sym) c
ans = (sym) d

参考资料:

  1. https://www.gnu.org/software/octave/doc/v4.0.0/Structure-Arrays.html
  2. http://mattpap.github.io/scipy-2011-tutorial/html/installing.html
  3. https://github.com/cbm755/octsympy
  4. https://askubuntu.com/questions/737746/installing-symbolic-package-in-octave
  5. https://github.com/sympy/sympy

这是一个句柄数组,而不是符号变量。 - rayryeng
我还加入了一个符号数组的示例,保证它能够正常工作。 - user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接