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.

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

octave:16> P = L .* M<br />

P =<br />

1.1669e+00 4.0557e+00 0.0000e+00<br />

1.1119e+01 1.8499e+01 0.0000e+00<br />

0.0000e+00 0.0000e+00 1.1120e+05<br />

octave:17> x = M \ b<br />

x =<br />

-6.0000000<br />

5.5000000<br />

0.0090090<br />

octave:18> err = M * x - b<br />

err =<br />

0<br />

0<br />

0<br />

Note the difference between L * M and L .* M; the former is matrix multiplication, the latter<br />

is element by element multiplication, i.e.,<br />

(L .∗ M) i,j = L i,j M i,j .<br />

The command rand(m,n) gives an m × n matrix with each element “uniformly distributed” on<br />

[0, 1].<br />

In line 17 we asked octave to solve the linear system<br />

by setting<br />

Mx = b,<br />

x = M\b = M −1 b.<br />

Note that you can construct matrices directly as you did vectors:<br />

octave:19> B = [1 3 4 5;2 -2 2 -2]<br />

B =<br />

1 3 4 5<br />

2 -2 2 -2<br />

You can also create row vectors as a sequence, either using the form c:d or the form c:e:d, which<br />

give, respectively, c, c + 1, . . . , d, and c, c + e, . . . , d, (or something like it if e does not divide d − c)<br />

as follows:<br />

octave:20> z = 1:5<br />

z =<br />

1 2 3 4 5<br />

octave:21> z = 5:(-1):1<br />

z =<br />

5 4 3 2 1

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

Saved successfully!

Ooh no, something went wrong!