The codes found here should work for Octave 2.1 and 2.9

The latest version of this page is located at http://www.math.tamu.edu/~comech/tools/octave-basics/Start

Matrices

Plotting

Solving ODEs

Writing scripts

Incorporating graphics into LaTeX files

References

(If you are off campus, you can use ssh to connect:

Open the shell window (click the "console" button at the bottom). You will get the shell window with calclab1's prompt,

Type `octave` (and then ``enter'') in the shell window;
you will see Octave's prompt, `octave:1>`

If you want Matlab, you can start it with `matlab-nojvm`;
you will see Matlab's prompt, `>>`

Try basic commands:

Use UpArrow and DownArrow to browse through the commands you already gave. Check that1+1 a=1+1 a=1+5; a sin(3.14) sin(pi)

v=[3,4,5] size(v) w=[3,4,5]' size(w) a=[0,7,0;3,0,0;0,0,0] size(a) a(1,2) col2=a(:,2) size(col2) a*a a.*a

x=[0,1,2,3]' y=x.^2 plot(x,y) # To discard the plot, point a mouse on it and typeq. x=(0:0.1:3)' # To discard the rest of the output, typeq. y=x.^2 plot(x,y) # To discard the plot, point a mouse on it and typeq. z=cos(x); w=sin(x); plot(x,y,x,z,x,w) # Here we plot y vs. x, z vs. x, and w vs. x

function ret=f(x,t); ret=x^2; end; # Test it: see what f(3,7) is

function ret=g(t,x); ret=x^2; end;

# t will be on the interval [0,1]; x(0)=0.5 [t,x]=rk4('g',[0,1],0.5); # t will be the set of moments of time; # x will be the values of the function at these moments of time. plot(t,x)

# Define the right-hand side of the equation: function ret=f(x,t); ret=x^2; end; # t will be on the interval [0,1]; x(0)=0.5 # t will be the set of moments of time: t=(0:0.1:1)'; # x will be the values of the function at these moments of time. x=lsode('f',0.5,t); plot(t,x)

function ret=g(t,x); ret=x^2; endfunction; # t will be on the interval [0,1]; x(0)=0.5 t=(0:0.1:1)'; x=ode(0.5,0,t,g); # t will be the set of moments of time; # x will be the values of the function at these moments of time. plot(t,x)

function ret=g(t,x) ret=x^2; end

# t will be on the interval [0,1]; x(0)=0.5 [t,x]=ode45('g',[0 1],0.5); # t will be the set of moments of time; # x will be the values of the function at these moments of time. plot(t,x)

function ret=f(t,x); ret=x^2;

## Octave script for solving dx/dt=f(x,t)=x^2 ## with the Runge-Kutta method (implemented in rk4.m) function ret=f(x,t); ret=x^2; end; t=(0:0.1:1)'; x=lsode('f',0.5,t); plot(t,x); pause(60); ## Press Ctrl-C to stop

Or use the pipe:

## Octave script for solving dx/dt=v, dv/dt=-x-v/5 ## with the Runge-Kutta method (implemented in rk4.m). function ret = g(t,X); ret = [X(2),-X(1)-X(2)/5]; end [T,X]=rk4('g',[0,50],[-2,0],0.2); # added timestep parameter, dt=0.2 plot(X(:,1),X(:,2)) pause(60);

## Octave script for solving dx/dt=v, dv/dt=-x-v/5 ## with the built-in lsode solver. function ret = f(X,t); ret = [X(2),-X(1)-X(2)/5]; end T=(0:0.2:50)'; X=lsode('f',[-2,0],T); plot(X(:,1),X(:,2)) pause(60);

print('fig.eps','-deps')

To generate graphics files without displaying them on the screen, try something like

figure (1, 'visible', 'off'); plot (randn (10, 1)); print -deps fig.eps

The resulting picture could be incorporated into a LaTeX file. Here is a short example of such a file:

\documentclass{article} \usepackage{graphicx,color} \begin{document} Equation $$ \frac{dx}{dt}=x^2 $$ \begin{figure}[htbp] \includegraphics[width=4in]{fig.eps} \end{figure} \end{document}

Here is an example of this.
Download the files
oscillator.tex,
fig.m,
rk4.m.

You can view the resulting picture withoctave fig.m # This produces fig.eps

This picture is accessed from the LaTeX file

latex oscillator.tex latex oscillator.tex dvips -Ppdf oscillator.dvi -o oscillator.ps ps2pdf oscillator.ps

For more examples on incorporating graphics and creating ``PowerPoint-like'' presentations, see Using Octave to prepare figures for LaTeX papers.

Written by Andrew Comech