Header Ads

Header ADS

Octave Programming Tutorial

Octave is a Matlab clone on Minux

he aim of this tutorial is to give you a quick introduction to basic Octave and to show that you know a lot of it already. If you should ever get stuck or need more information on an Octave function or command, type
help command
at the Octave prompt. command is the name of the Octave command or function on which to find help. Be warned though that the descriptions can sometimes be a bit technical and filled with jargon.

Starting Octave[edit]

Type octave in a terminal window to get started. You should see something like the following, depending on the version that you have installed:
GNU Octave, version 3.6.4.0
Copyright (C) 2013 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTIBILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  For details, type `warranty'.

Additional information about Octave is available at http://www.octave.org.

Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html

Report bugs to <bug@octave.org> (but first, please read
http://www.octave.org/bugs.html to learn how to write a helpful report).

octave:1>

Entering commands[edit]

The last line above is known as the Octave prompt and, much like the prompt in Linux, this is where you type Octave commands. To do simple arithmetic, use + (addition), - (subtraction), * (multiplication), / (division), and ^ (exponentiation).
Many mathematical functions are available and have obvious names (with many of those similar to other programming languages). For example, we have:
  • trigonometric functions[1]sincostan
  • inverse trigonometric functions: asinacosatan
  • natural and base 10 logarithms: loglog10
  • exponentiation: exp
  • absolute value: abs
Various constants are also pre-defined: pie (Euler's number), i and j (the imaginary number sqrt(-1)), inf (Infinity), NaN (Not a Number - resulting from undefined operations, such as Inf/Inf.)
Here are some examples showing the input typed at the prompt and the output returned by Octave.
octave:1> 2 + 3
ans = 5
octave:2> log10(100)/log10(10)
ans = 2
octave:3> floor((1+tan(1.2)) / 1.2)
ans = 2
octave:4> sqrt(3^2 + 4^2)
ans = 5
octave:5> e^(i*pi)
ans = -1.0000e+00 + 1.2246e-16i
octave:6> # Comment: From Euler's famous formula
octave:6> # extremely close to the correct value of -1
Some things to note:
  • Octave requires parentheses around the input of a function (so, log(10) is fine, but (log 10) is not).
  • Any spacing before and after arithmetic operators is optional, but allowed.
  • Not all Octave functions have obvious names (e.g. sqrt above). Don't panic for now. You will get to know them as we go along.

Plotting[edit]

You are going to plot the following pictures using Octave:
Octave plot sin.png
Figure 1
Octave plot step.png
Figure 2
Octave plot trigonometric.png
Figure 3
Octave plot trigonometric sum.png
Figure 4
Figure 1 contains a plot of sin x vs x and is generated with the following commands. While this is a simple example, it is meant to illustrate the basic functionality. We will see more elaborate examples later.
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(x, y);
figure;
The command that actually generates the plot is, of course, plot(x, y). Before executing this command, we need to set up the variables, x and y. The plot function simply takes two vectors of equal length as input, interprets the values in the first as x-coordinates and the second as y-coordinates and draws a line connecting these coordinates.
The first command above, x = linspace(0, 2*pi, 100), uses the linspace function to make a vector of equally spaced intervals (sometimes also called "linearly spaced values"). The first value in the vector is 0, the final value is 2Ï€ and the vector contains 100 values. This vector is assigned to the variable named x.
The second command computes the sin of each value in the vector variable, x, and stores the resulting vector in the variable y.
(As an aside: the name of a variable can be any sequence of letters, digits and underscores that does not start with a digit. There is no maximum length for variable names, and the case of alphabetical characters is important, i.e. a and A are two different variable names.)

Exercise[edit]

Plot the function  for . (This is Figure 2).

More on commands[edit]

The following commands and functions are useful for setting up variables for plotting 2D graphs.
  • linspace creates a vector of evenly (linearly) spaced values.
Usage: linspace(start, stop, length). The length parameter is optional and specifies the number of values in the returned vector. If you leave out this parameter, the vector will contain 100 elements with start as the first value and stop as the last.
  • plot plots a 2-dimensional graph.
Usage: plot(x, y) where x and y are vectors of equal length.

To learn more click on the following link:

https://en.wikibooks.org/wiki/Octave_Programming_Tutorial/Getting_started

No comments

Powered by Blogger.