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(x,t); ret=x^2; end; # Test it: see what f(3,7) is
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)
# 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; 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)
function ret=f(t,x); ret=x^2;
To feed this script to Octave, say octave myode.m on the shell prompt.
## 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
## 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')
For more graphics formats,
see
Printing Plots
in
Matlab manual.
figure (1, 'visible', 'off'); plot (randn (10, 1)); print -deps fig.eps
\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