03.03.2014 Views

Numerical Methods Course Notes Version 0.1 (UCSD Math 174, Fall ...

Numerical Methods Course Notes Version 0.1 (UCSD Math 174, Fall ...

Numerical Methods Course Notes Version 0.1 (UCSD Math 174, Fall ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

18 CHAPTER 2. A “CRASH” COURSE IN OCTAVE/MATLAB<br />

for var=vector<br />

statements<br />

end<br />

while expr<br />

statements<br />

end<br />

The test expressions may use the logical conditionals: >, =, 0<br />

s = 1;<br />

elseif x == 0<br />

s = 0;<br />

else<br />

s = -1;<br />

end<br />

%a ‘regular’ for loop<br />

for i=1:10<br />

sm = sm + i;<br />

end<br />

%an ‘irregular’ for loop<br />

for i=[1 2 3 5 8 13 21 34]<br />

fsm = fsm + i;<br />

end<br />

while (sin(x) > 0)<br />

x = x * pi;<br />

end<br />

2.4 Plotting<br />

Plotting is one area in which there are some noticeable differences between octave and Matlab.<br />

The commands and examples given herein are for octave, but the commands for Matlab are not<br />

too different. octave ships its plotting commands to Gnuplot.<br />

The main plot command is plot. You may also use semilogx, semilogy,loglog for 2D plots<br />

with log axes, and contour and mesh for 3D plots. Use the help command to get the specific<br />

syntax for each command. We present some examples:<br />

n = 100;<br />

X = pi .* ((1:n) ./ n);<br />

Y = sin(X);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!