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 />

4.2.6 A Matlab program<br />

Here is a rough Matlab program that does the job. You might like <strong>to</strong> look at this code<br />

very carefully.<br />

x_old = 0.5; % initial guess<br />

loop = 0; % set loop <strong>to</strong> zero<br />

loop_max = 50;<br />

% maximum number of iterations<br />

small_number = 0.0001; % target accuracy<br />

looping = (loop < loop_max);<br />

while looping<br />

% start of iterations<br />

x_new = exp(-x_old);<br />

f_old = x_old - exp(-x_old);<br />

f_new = x_new - exp(-x_new);<br />

loop = loop + 1;<br />

if loop > loop_max<br />

looping = false;<br />

end<br />

if abs(x_new-x_old) < small_number<br />

looping = false;<br />

end<br />

if abs(f_new) < small_number<br />

looping = false;<br />

end<br />

x_old = x_new;<br />

disp([loop x_new f_new ]);<br />

% next iteration<br />

% <strong>to</strong>o many iterations, exit<br />

% x values converged, exit<br />

% f values very small, exit<br />

% prepare for next iteration<br />

% display current approximation<br />

end;<br />

4.3 New<strong>to</strong>n-Raphson iteration<br />

Once again we have the problem of finding x such 0 = f(x). And once again we will<br />

generate a sequence x 1 , x 2 , x 3 , · · · which we hope will converge <strong>to</strong> x.<br />

How? Okay, let’s suppose we have a guess, call it x n and we wish <strong>to</strong> create a new<br />

(improved) guess x n+1 . This new guess will only be an approximation <strong>to</strong> x. Let δx be<br />

the error in x n , that is<br />

x n = x + δx<br />

Our game now is <strong>to</strong> compute (or estimate) δx.<br />

16-Feb-2014 46

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

Saved successfully!

Ooh no, something went wrong!