## Octave/MATLAB basics

The codes found here should work for Octave 2.1 and 2.9

Start
Matrices
Plotting
Solving ODEs
Writing scripts
Incorporating graphics into LaTeX files
References

### Start

(If you are off campus, you can use ssh to connect: ssh YourLogin@calclab1.math.tamu.edu )
Open the shell window (click the "console" button at the bottom). You will get the shell window with calclab1's prompt, @calclab1:~>

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:

1+1
a=1+1
a=1+5;
a
sin(3.14)
sin(pi)

Use UpArrow and DownArrow to browse through the commands you already gave. Check that e and exp(1) are the same. Make sure that e^3 is close to 20 and that i^2 is -1.

### Matrices

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

Note the difference between a*a (matrix multiplication) and a.*a (componentwise multiplication)!

### Plotting

x=[0,1,2,3]'
y=x.^2
plot(x,y)
# To discard the plot, point a mouse on it and type q.
x=(0:0.1:3)'
# To discard the rest of the output, type q.
y=x.^2
plot(x,y)
# To discard the plot, point a mouse on it and type q.
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


### Solving ODEs

We will solve equations of the form dx/dt=f(x(t),t), or dx/dt=g(t,x(t)), where x(t) could be a scalar-valued or vector-valued function. Define the right-hand side of the equation:
function ret=f(x,t); ret=x^2; end;
# Test it: see what f(3,7) is


#### WAY 1 (Octave/MATLAB)

In either Octave or MATLAB, one can use rk4.m (download this file to your home directory). We need to write the equation as dx/dt=g(t,x). Create the following file g.m:
function ret=g(t,x); ret=x^2; end;

Now tell the following to Matlab or Octave:
# 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)


#### WAY 2 (Octave only)

In Octave, we can use the built-in solver lsode. We need to write the equation as dx/dt=f(x(t),t).
# 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)


#### WAY 3 (SCILAB)

In SCILAB, one can use the built-in ODE solver ode. We need to write the equation as dx/dt=g(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)


#### WAY 4 (MATLAB)

In MATLAB, one can use the built-in ODE solver ode45. We need to write the equation as dx/dt=g(t,x). Create the following file g.m:
function ret=g(t,x)
ret=x^2;
end

Now tell the following to Matlab:
# 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)

Remark 1: In Matlab, function ret=f(t,x); ret=x^2; end; generates an error message (Error: Function definitions are not permitted at the prompt or in scripts). You would need to describe a function f(t,x) in a separate file, f.m, with the following contents:
function ret=f(t,x); ret=x^2;


Remark 2: The ODE solvers lsode, rk4, ode45... use different syntax. In lsode, we need x to come first in the list of arguments of f(), # because lsode expects it this way!

### Writing scripts

For a more serious work, it is much better to write a script with the commands and then to feed it to Octave. Open Emacs (on the shell prompt, type emacs) and create a file; say, myode.m, with the following contents:
## 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


To feed this script to Octave, say octave myode.m on the shell prompt.
Or use the pipe: cat myode.m | octave

#### Here is an example of solving a first order system (Octave/MATLAB):

## 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);



#### Here is an example of solving a first order system (Octave):

## 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);



### Incorporating graphics into LaTeX files

From Octave version 2.9, to send the graphical output to an EPS file, execute the following command:
print('fig.eps','-deps')

For more graphics formats, see Printing Plots in Matlab manual.
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.

octave fig.m
# This produces fig.eps

You can view the resulting picture with gv fig.eps
This picture is accessed from the LaTeX file oscillator.tex. To produce the Postscript and PDF files from oscillator.tex, say the following in the shell window:
latex oscillator.tex
latex oscillator.tex
dvips -Ppdf oscillator.dvi -o oscillator.ps
ps2pdf oscillator.ps

oscillator.pdf is the resulting PDF file.

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

Remark: If you use Matlab, once you produced a graphical output in a separate window, in that window go to File'' -> Save As...'' and in Save as type:'' choose EPS file''.

### Reference materials

Written by Andrew Comech