1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
<html lang="en">
<head>
<title>Simple Examples - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Introduction.html#Introduction" title="Introduction">
<link rel="prev" href="Running-Octave.html#Running-Octave" title="Running Octave">
<link rel="next" href="Conventions.html#Conventions" title="Conventions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Simple-Examples"></a>
Next: <a rel="next" accesskey="n" href="Conventions.html#Conventions">Conventions</a>,
Previous: <a rel="previous" accesskey="p" href="Running-Octave.html#Running-Octave">Running Octave</a>,
Up: <a rel="up" accesskey="u" href="Introduction.html#Introduction">Introduction</a>
<hr>
</div>
<h3 class="section">1.2 Simple Examples</h3>
<p>The following chapters describe all of Octave's features in detail, but
before doing that, it might be helpful to give a sampling of some of its
capabilities.
<p>If you are new to Octave, I recommend that you try these examples to
begin learning Octave by using it. Lines marked with ‘<samp><span class="samp">octave:13></span></samp>’
are lines you type, ending each with a carriage return. Octave will
respond with an answer, or by displaying a graph.
<h4 class="subsection">1.2.1 Elementary Calculations</h4>
<p>Octave can easily be used for basic numerical calculations. Octave
knows about arithmetic operations (+,-,*,/), exponentiation (^),
natural logarithms/exponents (log, exp), and the trigonometric
functions (sin, cos, <small class="dots">...</small>). Moreover, Octave calculations work
on real or imaginary numbers (i,j). In addition, some mathematical
constants such as the base of the natural logarithm (e) and the ratio
of a circle's circumference to its diameter (pi) are pre-defined.
<p class="noindent">For example, to verify Euler's Identity,
<pre class="display">
i*pi
e = -1
</pre>
<p class="noindent">type the following which will evaluate to <code>-1</code> within the
tolerance of the calculation.
<pre class="example"> octave:1> exp(i*pi)
</pre>
<h4 class="subsection">1.2.2 Creating a Matrix</h4>
<p>Vectors and matrices are the basic building blocks for numerical analysis.
To create a new matrix and store it in a variable so that you can
refer to it later, type the command
<pre class="example"> octave:1> A = [ 1, 1, 2; 3, 5, 8; 13, 21, 34 ]
</pre>
<p class="noindent">Octave will respond by printing the matrix in neatly aligned columns.
Octave uses a comma or space to separate entries in a row, and a
semicolon or carriage return to separate one row from the next.
Ending a command with a semicolon tells Octave not to print the result
of the command. For example,
<pre class="example"> octave:2> B = rand (3, 2);
</pre>
<p class="noindent">will create a 3 row, 2 column matrix with each element set to a random
value between zero and one.
<p>To display the value of a variable, simply type the name of the
variable at the prompt. For example, to display the value stored in the
matrix <code>B</code>, type the command
<pre class="example"> octave:3> B
</pre>
<h4 class="subsection">1.2.3 Matrix Arithmetic</h4>
<p>Octave has a convenient operator notation for performing matrix
arithmetic. For example, to multiply the matrix <code>A</code> by a scalar
value, type the command
<pre class="example"> octave:4> 2 * A
</pre>
<p class="noindent">To multiply the two matrices <code>A</code> and <code>B</code>, type the command
<pre class="example"> octave:5> A * B
</pre>
<p class="noindent">and to form the matrix product
<code>transpose (A) * A</code>,
type the command
<pre class="example"> octave:6> A' * A
</pre>
<h4 class="subsection">1.2.4 Solving Systems of Linear Equations</h4>
<p>Systems of linear equations are ubiquitous in numerical analysis.
To solve the set of linear equations <code>A</code><var>x</var><code> = b</code>,
use the left division operator, ‘<samp><span class="samp">\</span></samp>’:
<pre class="example"> <var>x</var> = A \ b
</pre>
<p class="noindent">This is conceptually equivalent to
<code>inv (a) * b</code>,
but avoids computing the inverse of a matrix directly.
<p>If the coefficient matrix is singular, Octave will print a warning
message and compute a minimum norm solution.
<p>A simple example comes from chemistry and the need to obtain balanced
chemical equations. Consider the burning of hydrogen and oxygen to
produce water.
<pre class="example"> H2 + O2 --> H2O
</pre>
<p class="noindent">The equation above is not accurate. The Law of Conservation of Mass requires
that the number of molecules of each type balance on the left- and right-hand
sides of the equation. Writing the variable overall reaction with
individual equations for hydrogen and oxygen one finds:
<pre class="example"> x1*H2 + x2*O2 --> H2O
H: 2*x1 + 0*x2 --> 2
O: 0*x1 + 2*x2 --> 1
</pre>
<p class="noindent">The solution in Octave is found in just three steps.
<pre class="example"> octave:1> A = [ 2, 0; 0, 2 ];
octave:2> b = [ 2; 1 ];
octave:3> x = A \ b
</pre>
<h4 class="subsection">1.2.5 Integrating Differential Equations</h4>
<p>Octave has built-in functions for solving nonlinear differential
equations of the form
<pre class="example"> dx
-- = f (x, t)
dt
</pre>
<p class="noindent">with the initial condition
<pre class="example"> x(t = t0) = x0
</pre>
<p class="noindent">For Octave to integrate equations of this form, you must first provide a
definition of the function
<code>f(x,t)</code>.
This is straightforward, and may be accomplished by entering the
function body directly on the command line. For example, the following
commands define the right-hand side function for an interesting pair of
nonlinear differential equations. Note that while you are entering a
function, Octave responds with a different prompt, to indicate that it
is waiting for you to complete your input.
<pre class="example"> octave:1> function xdot = f (x, t)
>
> r = 0.25;
> k = 1.4;
> a = 1.5;
> b = 0.16;
> c = 0.9;
> d = 0.8;
>
> xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
> xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
>
> endfunction
</pre>
<p class="noindent">Given the initial condition
<pre class="example"> octave:2> x0 = [1; 2];
</pre>
<p class="noindent">and the set of output times as a column vector (note that the first
output time corresponds to the initial condition given above)
<pre class="example"> octave:3> t = linspace (0, 50, 200)';
</pre>
<p class="noindent">it is easy to integrate the set of differential equations:
<pre class="example"> octave:4> x = lsode ("f", x0, t);
</pre>
<p class="noindent">The function <code>lsode</code> uses the Livermore Solver for Ordinary
Differential Equations, described in A. C. Hindmarsh, <cite>ODEPACK, a
Systematized Collection of ODE Solvers</cite>, in: Scientific Computing, R. S.
Stepleman et al. (Eds.), North-Holland, Amsterdam, 1983, pages 55–64.
<h4 class="subsection">1.2.6 Producing Graphical Output</h4>
<p>To display the solution of the previous example graphically, use the
command
<pre class="example"> octave:1> plot (t, x)
</pre>
<p class="noindent">If you are using a graphical user interface, Octave will automatically create
a separate window to display the plot.
<p>To save a plot once it has been displayed on the screen, use the print
command. For example,
<pre class="example"> print -deps foo.eps
</pre>
<p class="noindent">will create a file called <samp><span class="file">foo.eps</span></samp> that contains a rendering of
the current plot in Encapsulated PostScript format. The command
<pre class="example"> help print
</pre>
<p class="noindent">explains more options for the <code>print</code> command and provides a list
of additional output file formats.
<h4 class="subsection">1.2.7 Editing What You Have Typed</h4>
<p>At the Octave prompt, you can recall, edit, and reissue previous
commands using Emacs- or vi-style editing commands. The default
keybindings use Emacs-style commands. For example, to recall the
previous command, press <kbd>Control-p</kbd> (written <kbd>C-p</kbd> for
short). Doing this will normally bring back the previous line of input.
<kbd>C-n</kbd> will bring up the next line of input, <kbd>C-b</kbd> will move
the cursor backward on the line, <kbd>C-f</kbd> will move the cursor forward
on the line, etc.
<p>A complete description of the command line editing capability is given
in this manual in <a href="Command-Line-Editing.html#Command-Line-Editing">Command Line Editing</a>.
<h4 class="subsection">1.2.8 Help and Documentation</h4>
<p>Octave has an extensive help facility. The same documentation that is
available in printed form is also available from the Octave prompt,
because both forms of the documentation are created from the same input
file.
<p>In order to get good help you first need to know the name of the command
that you want to use. This name of the function may not always be
obvious, but a good place to start is to just type <code>help</code>.
This will show you all the operators, reserved words, functions,
built-in variables, and function files. An alternative is to search the
documentation using the <code>lookfor</code> function. This function is
described in <a href="Getting-Help.html#Getting-Help">Getting Help</a>.
<p>Once you know the name of the function you wish to use, you can get more
help on the function by simply including the name as an argument to help.
For example,
<pre class="example"> help plot
</pre>
<p class="noindent">will display the help text for the <code>plot</code> function.
<p>Octave sends output that is too long to fit on one screen through a
pager like <code>less</code> or <code>more</code>. Type a <RET> to advance one
line, a <SPC> to advance one page, and <q> to exit the pager.
<p>The part of Octave's help facility that allows you to read the complete
text of the printed manual from within Octave normally uses a separate
program called Info. When you invoke Info you will be put into a menu
driven program that contains the entire Octave manual. Help for using
Info is provided in this manual in <a href="Getting-Help.html#Getting-Help">Getting Help</a>.
</body></html>
|