There are a number of Open Source alternatives to the program MatLab, one of the most known alternatives is GNU Octave. When using Octave you use a command line to enter commands. For a novice, the lack of a graphical interface might be intimidating; QtOctave provides a somewhat user friendly graphical interface to Octave.
There is a cloud-version of Octave at http://www.verbosus.com/; from this place you can run Octave from the Internet.
Install QtOctave from Ubuntu Software Center.
QtOctave is just a graphical user interface for Octave. In order to run QtOctave, you must first downloadOctave, which can be downloaded from SourceForgehttp://octave.sourceforge.net/. Download it and install it.
First you need Qt which is a cross-platform C++ application framework. Download Qt fromhttp://qt.nokia.com/downloads/qt-for-open-source-cpp-development-on-mac-os-x/; the file neededis encircled in the picture below, it is the "Cocoa"-file.
You also need the QtOctave-file which can be downloaded from http://sites.google.com/site/davidecittaro/software.
Double click on the zip-file to extract it, then move the QtOctave-file to the Application folder.
Download QtOctave from http://www.outsch.org/2011/01/29/qtoctave-0-10-1-for-windows/.
Download the zip-file from the link above. Right-click on the zip-file and chooseExtract all.
Pick a place where you want the qtoctave-directory. After extracting all files, go to the directory and then into qtoctave/bin, find the file qtoctave.exe, this is the executable file, don't move this file.
You can make a short-cut to the desk top by right-clicking on qtoctave.exe.
When you start QtOctave for the first time you might get an error message saying that it cannot find Octave; the program will start anyway but you will not be able to do anything without Octave. Find the file Octave.exe on your computer, the file is in a directory called bin
(binary). Right click on the file and choose properties to see the file path. It might look like this:
C:\Octave\3.2.4_gcc-4.4.0\bin
You must now specify this file path in QtOctave. Choose Config->General configuration
and click on Octave.
Click on Select...
and navigate to the executable Octave-file.
You can also write the file path.
Note that Windows uses "\" to separate directories whereas Unix/Linux uses "/"!
If you need help, Google on either MatLab or Octave plus whatever you command/feature you want to know more about. Most of the homepages are made for viewers having a mathematical knowledge exceeding that of a student at school. MatLab is an abbreviation of Matrix Laboratory and the program itself is based on matrices; you can however use the program even though you lack a deeper understanding of linear algebra, in many cases you can think of the matrices simply as tables of numbers.
The picture below shows the window when starting QtOctave.
There are five tools shown: Variable list, Command List, Navigator, Terminal and Editor. To begin with, you will only need the terminal. The terminal is used when writing commands on one line. If you want to write commands on several lines, you do it using the editor-tool. The commands written in the editor are then saved in so called M-files.
You hide a tool by clicking on the small cross at the top of its window.
You show a tool by clicking on the corresponding icon in the tool bar.
You maximize a tool, it will fill up the window, by clicking on the maximize icon or by double clicking atthe menu bar of the tool.
You restore the size by right clicking at the menu bar of the tool and choosingRestore
.
The easiest way to get started, is to use Octave as a calculator.
At the bottom of the terminal there is a command prompt >>
followed by an input bar, this is where you enter the commands. After entering a command you press enter and the output will appear above the input bar. You can view the commands you have written in the Command List.
The arithmetic operators are:
+ - * / ^
(to the power of)
There are predefined constants in Octave, some of them are listed below:
pi
, e
, i
Inf
(infinity) NaN
(not a number)There are also a number of predefined mathematical functions in Octave, some of them are listed below:
sin, cos, tan
asin, acos, atan
exp, log, log10
sqrt
abs
round, floor, ceil
rem
You can show more decimals by typing format long
. Go back to showing few decimals by typingformat short
.
Calculate following expressions
Guess the output of following commands, then check the answers given by Octave
1/0 0/0 tan(pi/2)
inf+inf inf-inf inf/inf inf*inf
nan+2 nan+nan
When evaluating the value of an expression, the result is stored in a variable calledans
(answer). You can also introduce variables of your own.
Various programs have different rules for naming entities. Although some modern programs allow for all kind of characters in names, you may run into trouble if you use characters like "-" or blank. If you want to stay on the safe side, never having to bother about naming-rules for specific programs, follow these old-school rules:
a
-z
,A
- Z
0
- 9
_
These rules apply to Octave and a number of other programs.
When writing a=5
at the command prompt, the equal-sign should not be thought of as a logical equality;a
is not equal to 5, it is assigned the value 5. Having that in mind, one can write assignments like this
a=a+1
The statement above would be false if it was a logical statement, it is however not a logical statement but an assignment. Theright-hand-side of the statement is calculated first, the result is then assigned to the variable on the left-hand-side.
The recording below shows how to
Calculate the series
The answer is 1.5962
A sequence is a function with the domain being the natural numbers .
A sequence can be written like this a0, a2, a2,... or like this
A geometric progression can be defined in different ways. Consider the infinite geometric progression
You can describe the geometric progression in two ways.
A recurrence equation
An explicit formula
Use QtOctave to find the limit as of following sequences. Typeformat long
to show more decimals. Try different initial values c, do you get different limits?
Try to explain your answers. If you fail, do the exercises on fixpoints.
There is a complete documentation of Octave at http://www.gnu.org/software/octave/doc/interpreter/
You can format numbers in three different ways; format long
, format short
and format bank
(2 decimals)
>>> format long >>> pi ans = 3.14159265358979 >>> format short >>> pi ans = 3.1416 >>> format bank >>> pi ans = 3.14
Just writing the command format
will give you the default format short
.
When handling text instead of numbers you use strings. A string is enclosed in either "-signs or '-signs.
>>> my_string_variable="Hello World!" my_string_variable = Hello World! >>> my_string_variable='Hello World!' my_string_variable = Hello World!
If you want to find information about a command or a function you can use the help
command.
>>> help asin `asin' is a built-in function -- Mapping Function: asin (X) Compute the inverse sine in radians for each element of X. See also: sin, asind
You can use the disp
command together with an argument enclosed in brackets to display data. When usingdisp
the output is displayed without the ans=
.
>>> disp(pi) 3.1416 >>> disp(my_string_variable) Hello World! >>> disp("The value of pi is:"), disp(pi) The value of pi is: 3.1416
When measuring time on a computer it is a convention to measure the time since 00:00:00UTC 1 January 1970. The command time
will give you the number of seconds since then; the commandnow
will give you the number of days. The command date
will give you the current date as a string. The commandclock
will give you the current year-month-day-hour-minute-second as a row-matrix.
>>> time ans = 1.2737e+009 >>> now ans = 7.3427e+005 >>> date ans = 12-May-2010 >>> clock ans = 2010.0000 5.0000 12.0000 15.0000 41.0000 40.3795
You can store a number of commands in a so called m-file. You enter the commands in the Editor, pickView->Dock Tools->Editor
or use the icon at the tool-bar.
The recording below shows how to
disp
commandIt looks slightly different on the portable version of QtOctave.
视频地址:http://www.malinc.se/math/octave/mfilesen.php
Repeating the same operation over and over again is called iterating. One way to iterate when programming is to use afor
statement. A for
statement in its simplest form uses a variable representing an index that is increased by 1 in each step.
In the picture to the right the the index is called i
. The command betweenfor
and end
is repeated 10 times. The first time row 2 is executed,i
is equal to 0, the second time i
is equal to 1, and so on.
Row 4 starts with a %
-sign, everything to the right of the %
-sign is ignored when the program is run; this is a comment intended for those reading the actual code.
You can increment your index with something else than 1 by adding a number between the starting index and the last index.
1 for i=0:0.1:2 2 disp(i) 3 end 4 %21 numbers are displayed, the numbers 0,0.1,0.2,...,2
The recording below shows various features of the for
statement.
Write for-
statements in m-files to solve following exercises. Useformat long
.
for-
statement to iterate the recurrence equation 10 times. Continued fractions
A series is said to converge if has a finite limit as.
Harmonic series
Madhava-Leibniz series
When doing a comparison in Octave you apply a comparison operator on two numbers and the result is eithertrue or false, represented by 1 or 0.
operation
arithmetic operations
logical operations
comparisons
operators
+ - * / ^
&& || !
> >= < <= == !=
operands
numbers
logical values
numbers
result
a number
a logical value
a logical value
The comparison operators are:
>
>=
<
<=
==
!=
greater than, >
greater than or equal to, ≥
less than, <
less than or equal to, ≤
equal to, =
not equal to, ≠
>>> a=1; b=2; c=2; d=3; >>> a>=b ans = 0 >>> b>=c ans = 1 >>> (a < b) && (c!=d) ans = 1 >>> a < b && c!=d >>>parse error: syntax error >>> a < b && c!=d ^
In Boolean algebra you represent the logical values true and false by the numbers 1 and 0 respectively.
>>> true ans = 1 >>> false ans = 0
The basic operators in logic are and, or andnot, these are written using the symbols ∧, ∨ and ¬ respectively. Ifp and q are statements that are either true or false, then you get the truth table
The operators ∧ and ∨ are binary operators, they are applied to two operands. The operator ¬ is anunary operator, it applies to one operand.
In Octave (and most other programming languages) the operators ∧, ∨ and ¬ are written using the symbols&&
, ||
and !
; giving the truth table
&&
q||
qIn Octave (and most other programming languages) all numbers that are not 0 are thought of as beingtrue.
>>> a=4.5; >>> b=0; >>> (a && b) || !b ans = 1
Note that you can perform logical operations by using arithmetics.
p &&
q = pq
p ||
q = p+q-pq
!p = 1-p
Fuzzy Logics: This article was published in Scientific American 1993, A Partly True Story, by Ian Stewart
That is true → ← That is false
You create a row matrix consisting of the numbers 4 5 6 7 by entering the numbers inside []-brackets and separating the numbers by a comma or a blank.
>>> r1=[4, 5, 6, 7] r1 = 4 5 6 7 >>> r2=[4 5 6 7] r2 = 4 5 6 7
You create a column matrix consisting of the numbers 0.1 0.2 0.3 by entering the numbers inside []-brackets and separating the numbers by a semicolon.
>>> c1=[0.1; 0.2; 0.3] c1 = 0.10000 0.20000 0.30000 >>> c2=[0.4; 0.5] c2 = 0.40000 0.50000
Row and column matrices are sometimes called vectors.
You can combine row matrices or column matrices
>>> r=[r1, r2]
r =
4 5 6 4 5 6
>>> c=[c1; c2]
c =
0.10000
0.20000
0.30000
0.40000
0.50000
>>> try_a_mix=[r1, c1]
>>>error: number of rows must match (3 != 1) near line 43, column 16
The first number in a vector has index 1. You can get the number at a specified index.
>>> r(2)
ans = 5
>>> c(5)
ans = 0.50000
>>> r(7)
error: A(I): Index exceeds matrix dimension.
You can use the same notation that creates numbers in for-statements for creating vectors.
>>> r3=3:7 r3 = 3 4 5 6 7 >>> r4=1:3:10 r4 = 1 4 7 10
You can use the same notation for extracting a vector from a vector.
>>> format bank; >>> r5=0.1:0.1:1 r5 = 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00 >>> subvector1=r5(2:4) subvector1 = 0.20 0.30 0.40 >>> subvector2=r5(1:2:10) subvector2 = 0.10 0.30 0.50 0.70 0.90
You can turn a row into a column or the other way around by doing a transpose using the operator '.
>>> r=[1, 2, 3] r = 1.00 2.00 3.00 >>> c=r' c = 1.00 2.00 3.00
A scalar is just a fancy word for a number; it is used to distinguish numbers from vectors or matrices.
You can use the arithmetic operators +
, -
, *
and/
on a matrix and a scalar. The operation is applied to each element of the matrix.
>>> r=[4, 5, 6] r = 4.00 5.00 6.00 >>> r1=r*6 r1 = 24.00 30.00 36.00 >>> r2=(r1-20)/2 r2 = 2.00 5.00 8.00 >>> c=[0.5; 3.5] c = 0.50 3.50 >>> c1=c+0.5 c1 = 1.00 4.00
You can apply functions on matrices, the function is then applied to each element of the matrix.
>>> c=[4;16] c = 4.00 16.00 >>> sqrt(c) ans = 2.00 4.00
A general matrix consists of n rows and m columns.
The recording below shows how to
size(matrix)
matrix(row,column)
Try the commands ones(3), zeros(4), eye(5)
!
视频地址:http://www.malinc.se/math/octave/nmmatricesen.php
You can perform element by element operations on two matrices having the same dimension, i.e. having the same number of rows and columns respectively. When performing an element by element operation the result is a new matrix having the same dimension as the two operands.
When doing an element by element addition, the element on place (row, col)
in the resulting matrix will be the sum of the two elements at(row, col)
in the operand matrices.
The regular arithmetic operators will become element-by-element operators if you place a dot in front of them.
.+ .- .* ./ .^
>>> m1 m1 = 1 2 3 4 5 6 >>> m2 m2 = 0 1 1 2 2 0 >>> m1./m2 ans = Inf 2.0000 3.0000 2.0000 2.5000 Inf >>> m1.^m2 ans = 1 2 3 16 25 1
When applying a function on a matrix the function is applied to each element of the matrix
>>> m=[0 pi pi/2;-pi -pi/2 0] m = 0.00000 3.14159 1.57080 -3.14159 -1.57080 0.00000 >>> sin(m) ans = 0.00000 0.00000 1.00000 -0.00000 -1.00000 0.00000
Using element by element operations you can combine comparisons and arithmetic.
>>> m=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15] m = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 >>> m1=m>4 m1 = 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 >>> m2=m1.*m m2 = 0 0 0 0 5 6 7 8 9 10 11 12 13 14 15
In regular mathematics, matrix addition and subtraction are defined to be element by element operations. Since using the Octave operators without any dot means "regular" usage, there is no difference between+
and .+
, or between -
and .-
. When it comes to multiplication, division and to-the-power-of, there is a difference between "regular" usage and the element-by-element usage; hence do not use these operators without a dot in front of them (unless you actually know linear algebra).
GNU Octave, 8.3 Arithmetic Operators bydelorie software
When plotting in Octave you plot points having their x-values stored in one vector and they-values in another vector. The two vectors must be the same size.
You can use a x-vector to store the x-values; then you use element by element operations on thex-vector to store the function values in a y-vector. Having two vectors like this, you then use the command
plot(x_vector, y_vector)
>>> x=-2:2 x = -2 -1 0 1 2 >>> y=x.^2 y = 4 1 0 1 4 >>> plot(x,y)
Octave inserts lines between the points. If you want a smoother graph, make a longerx-vector.
>>> x=-2:0.5:2; >>> y=x.^2; >>> plot(x,y)
If you know how many points you want to plot in an interval, you can let Octave space the points linearly by using the command
linspace(first x-value, last x-value, number of evenly spaced points)
>>> x=linspace(-2, 2, 500); >>> y=x.^2; >>> plot(x,y)
You can use Octave commands to change the appearance of your graphs and to add informative text. If using QtOctave you can use the graphical user interface (GUI) instead of commands.
The recording below shows how to
linspace(first value, last value, number of evenly spaced points)
to make a vectorxplot(x,y)
hold on
to draw several graphs in one window (the command hold off
turns it off)If we have a function of two variables , we need three axes to display the graph.
When plotting in 2D we use evenly spaced x-values and function values of these stored in ay-vector.
When plotting in 3D we need evenly spaced x- and y-values, spaced on a grid where each function valuez is taken of a point (x, y) on the grid. In order to achieve this we use the command
meshgrid
.
>>> x=linspace(-2,2,5) x = -2 -1 0 1 2 >>> y=linspace(-2,2,5) y = -2 -1 0 1 2 >>> [xx,yy]=meshgrid(x,y) xx = -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 yy = -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2
Each point on the grid is made by taken an element from the xx
-matrix as thex-value and the corresponding element from the yy
-matrix as they-value. All in all there are 25 points in this grid.
After having made a grid you can plot a 3D graph using the command
mesh(xx,yy,z)
where xx
and yy
are the matrices made by meshgrid
and wherez is a function of x and y. You get the function values ofz by using element by element operations on matrices xx
andyy
.
>>> x=linspace(-2,2,5); >>> y=linspace(-2,2,5); >>> [xx,yy]=meshgrid(x,y); >>> mesh(xx,yy,4-(xx.^2+yy.^2))
If you want a smoother graph, make a longer x-vector and a longer y-vector.
>>> x=linspace(-2,2,50); >>> y=linspace(-2,2,50); >>> [xx,yy]=meshgrid(x,y); >>> mesh(xx,yy,4-(xx.^2+yy.^2))
You can get a contour plot by using the command meshc
.
>>> x=linspace(-2,2,50); >>> y=linspace(-2,2,50); >>> [xx,yy]=meshgrid(x,y); >>> meshc(xx,yy,4-(xx.^2+yy.^2))