GNU Octave是一种以高级编程语言为特色的软件,主要用于数值计算和绘图。通过Octave可以非常方便的进行矩阵运算、求解联立方程组、计算矩阵特征值和特征向量等。本篇文档主要内容是Octave基础命令、数据格式、绘制图形、Octave脚本编写、Octave函数编程等内容。另外,此文档会不断补充常见使用场景。
octave:1> who octave:2> a = 10; b = 20; c = a + b; octave:3> who Variables visible from the current scope:
a b c
octave:4> d = 200; octave:5> who Variables visible from the current scope:
a b c d
octave:6> clear octave:7> who octave:8> d = 200; octave:9> clear d octave:10> who octave:11> clear octave:12> a = 10; b = 20; c = a + b; octave:13> who Variables visible from the current scope:
a b c
octave:14> clear b octave:15> who Variables visible from the current scope:
octave:1> help clear 'clear' is a built-in function from the file libinterp/corefcn/variables.cc
-- clear -- clear PATTERN ... -- clear OPTIONS PATTERN ... Delete the names matching the given PATTERNs thereby freeing memory...
octave:2> help sin 'sin' is a built-in function from the file libinterp/corefcn/mappers.cc -- sin (X) Compute the sine for each element of X in radians. See also: asin, sind, sinh...
zchanglin@mbp ~ % octave GNU Octave, version 6.4.0 Copyright (C) 2021 The Octave Project Developers. This is free software; see the source code for copying conditions. There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'.
Octave was configured for "x86_64-apple-darwin19.6.0".
Additional information about Octave is available at https://www.octave.org.
Please contribute if you find this software useful. For more information, visit https://www.octave.org/get-involved.html
Read https://www.octave.org/bugs.html to learn how to submit bug reports. For information about changes from previous versions, type 'news'.
octave:1> (2+8)/5 * 2 - 3 ans = 1 octave:2> a = (2+8)/5 * 2 - 3; octave:3> a a = 1
Octave 提供了一系列的常用数学函数,其中的常用部分函数如下所示。像 C++ 中调用函数一样,Octave 通过输入函数名和括号中的输入参数来调用函数,其中自然对数e、圆周率pi 都是Octave已经定义的常量(ans代表上一次的计算结果,就像一个普通的科学计算器一样),例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
octave:1> sin(90 * pi/180) ans = 1 octave:2> sin(30 * pi/180) ans = 0.5000 octave:3> cos(60 * pi/180) ans = 0.5000 octave:4> pi ans = 3.1416 octave:5> e ans = 2.7183 octave:6> log(e) ans = 1 octave:7> 2 * ans ans = 2 octave:8> abs(5) + abs(-5) ans = 10 octave:9>
%Script to calculate and plot a rectified sine wave t=linspace(0,10,100); y=abs(sin(t)); % get abs plot(t,y); title('Rectified Sine Wave'); xlabel('t');
>> help abssin % 查看脚本的帮助文档,其实就是自己定义的注释内容 'abssin' is a script from the file ...\OctaveWK\abssin.m
Script to calculate and plot a rectified sine wave
Additional help for built-in functions and operators is available in the online version of the manual. Use the command 'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW at https://www.octave.org and via the help@octave.org mailing list.
假设你使用了很多的脚本之后你会对这些脚本混淆不清。要想知道你拥有哪些脚本,你可以输入 what 命令来获取一个你当前所有的脚本和数据的列表:
1 2 3 4
>> what M-files in directory xxx\Desktop\OctaveWK: