28.11.2014 Views

MTH3051 Introduction to Computational Mathematics - User Web ...

MTH3051 Introduction to Computational Mathematics - User Web ...

MTH3051 Introduction to Computational Mathematics - User Web ...

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.

School of Mathematical Sciences<br />

Monash University<br />

n = 100; % set number of terms<br />

sum = 0; % set initial value for sum<br />

sign = 1; % an integer +/- 1<br />

for k = 1 : n<br />

% loop over k from 1 <strong>to</strong> n<br />

term = sign/(2*k-1); % compute the term<br />

sum = sum + term; % update rolling sum<br />

sign = - sign; % flip the sign<br />

end;<br />

disp(n);<br />

% print n<br />

disp(4*sum);<br />

% print the approximation <strong>to</strong> π<br />

disp(4*sum-pi);<br />

% print the error<br />

This is an example of Matlab syntax. Matlab is a programming language well suited<br />

<strong>to</strong> numerical computations. There are many other languages such as Fortran, C, Java,<br />

Maple and Mathematica. They all have a similar flavour and they all serve the one<br />

purpose of getting the computer <strong>to</strong> do useful work for us. We will use Matlab throughout<br />

this course as it is (arguably) the easiest <strong>to</strong> learn for someone with no programming<br />

experience. But don’t let this s<strong>to</strong>p you from learning about other programming languages<br />

(in your spare time).<br />

In reading the above code you must keep in mind one extremely important fact the<br />

equals sign is not what you might expect it <strong>to</strong> be. The computer will treat the equal<br />

sign (usually) as a replacement opera<strong>to</strong>r. Thus in executing a line like x = 2*y the<br />

computer will first evaluate the right-hand side then assign that value <strong>to</strong> the left-hand<br />

side. Whatever value x had before, it will be wiped out. Its value after the line is<br />

executed will be 2*y. This use of the equals sign allows us <strong>to</strong> write lines like x = x +<br />

1 <strong>to</strong> increment the current value of x by 1. In contrast, if you showed a mathematician<br />

a line like x = x + 1 he or she would look at you very very strangely (why?).<br />

The above Matlab code is fairly easy <strong>to</strong> read. The first three lines set initial values <strong>to</strong><br />

various symbols (aka variables). Then we encounter a for-loop. Matlab will repeat the<br />

code in this loop for each value of k from 1 <strong>to</strong> n in strict order. Each time through this<br />

loop we are calculating one term in the series. After the loop finishes we print out the<br />

various numbers that interest us (using the Matlab command disp which is short for<br />

display ).<br />

If the above code baffles you then one way <strong>to</strong> understand it is <strong>to</strong> pretend you are the<br />

computer and follow the Matlab commands. Get out a pencil and paper and start<br />

following the instructions. Work your way through the first 5 or so terms. You should<br />

see that it is correct and does compute the series.<br />

16-Feb-2014 10

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

Saved successfully!

Ooh no, something went wrong!