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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

You now have a command line. The basic octavian data structure is a matrix; a scalar is a 1×1<br />

matrix, a vector is an n × 1 matrix. Some simple matrix constructions are as follows:<br />

octave:2> a = [1 2 3]<br />

a =<br />

1 2 3<br />

octave:3> b = [5;4;3]<br />

b =<br />

5<br />

4<br />

3<br />

octave:4> c = a’<br />

c =<br />

1<br />

2<br />

3<br />

octave:5> d = 5*c - 2 * b<br />

d =<br />

-5<br />

2<br />

9<br />

You should notice that octave “echoes” the lvalues it creates. This is either a feature or an<br />

annoyance. It can be prevented by appending a semicolon at the end of a command. Thus the<br />

previous becomes<br />

octave:5> d = 5*c - 2 * b;<br />

octave:6><br />

For illustration purposes, I am leaving the semicolon off. To access an entry or entries of a<br />

matrix, use parentheses. In the case where the variable is a vector, you only need give a single<br />

index, as follows; when the variable is a matrix, you need give both indices. You can also give a<br />

range of indices, as what follows:<br />

octave:6> a(1) = 77<br />

a =<br />

77 2 3<br />

octave:7> a(2) = -400<br />

a =<br />

77 -400 3<br />

octave:8> a(2:3) = [22 333]<br />

a =

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

Saved successfully!

Ooh no, something went wrong!