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/
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.
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.*aNote the difference between a*a (matrix multiplication) and a.*a (componentwise multiplication)!
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
function ret=f(t,x); ret=x^2; endfunction; # Test it: see what f(3,7) isTo solve the equation, we will use rk4.m (download this file to your home directory).
# t will be on the interval [0,1]; x(0)=0.9
[t,x]=rk4('f',[0,1],0.9);
# 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;
function ret=f(x,t); ret=x^2; endfunction;
# Test it: see what f(3,7) is!
# Note that now we need x to come first in the list of arguments of f(),
# because lsode expects it this way!
t=(0:0.01:1)';
x=lsode('f',0.9,t);
plot(t,x)
To feed this script to Octave, say octave myode.m on the shell prompt.
## Octave script for solving dx/dt=f(t,x)=x^2 ## with the Runge-Kutta method (implemented in rk4.m) function ret=f(t,x); ret=x^2; endfunction; [t,x]=rk4('f',[0,1],0.9); plot(t,x); pause(60); ## Press Ctrl-C to stop
Here is an example of solving a first order system:
## Octave script for solving dx/dt=v, dv/dt=-x-v/5
## with the Runge-Kutta method (implemented in rk4.m).
function ret = f(t,X); ret = [X(2),-X(1)-X(2)/5]; endfunction
[T,X]=rk4('f',[0,50],[-2,0],0.2); # added timestep parameter, dt=0.2
plot(X(:,1),X(:,2))
pause(60);
print('fig.eps','-deps')
For more graphics formats,
see
Printing Plots
in
Matlab manual.
\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.
Written by Andrew Comech
ACKNOWLEDGMENT: This page resulted from the research that has been partially supported by the National Science Foundation under Grants DMS-0621257 and DMS-0600863.