03.05.2014 Views

Introduction to Numerical Math and Matlab ... - Ohio University

Introduction to Numerical Math and Matlab ... - Ohio University

Introduction to Numerical Math and Matlab ... - Ohio University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

12 LAB 4. CONTROLLING ERROR AND WHILE LOOPS<br />

In this program if checks if abs(y) > <strong>to</strong>l is true or not. If it is true then it does everything<br />

between there <strong>and</strong> end. If not true, then it skips ahead <strong>to</strong> end.<br />

In the comm<strong>and</strong> window define the function<br />

> f = inline(’x^3-5’,’x’)<br />

<strong>and</strong> its derivative<br />

> f1 = inline(’3*x^2’,’x’).<br />

Then use the program with n = 5 <strong>and</strong> <strong>to</strong>l = .01. Next, change <strong>to</strong>l <strong>to</strong> 10 −10 <strong>and</strong> repeat.<br />

The loop: while ... end<br />

While the previous program will tell us if it worked or not, we still have <strong>to</strong> input n, the number of<br />

steps <strong>to</strong> take. Even for a well-behaved problem, if we make n <strong>to</strong>o small then the <strong>to</strong>lerance will not<br />

be attained <strong>and</strong> we will have <strong>to</strong> go back <strong>and</strong> increase it, or, if we make n <strong>to</strong>o big, then the program<br />

may run for longer than necessary.<br />

One way <strong>to</strong> control the number of steps taken is <strong>to</strong> iterate until the residual |r n | = |f(x)| = |y| is<br />

small enough. In <strong>Matlab</strong> this is easily accomplished with a while ... end loop.<br />

function x = mynew<strong>to</strong>n<strong>to</strong>l(f,f1,x0,<strong>to</strong>l)<br />

% Solves f(x) = 0 by doing New<strong>to</strong>n’s method starting at x0.<br />

% Inputs: f -- the function, input as an inline<br />

% f1 -- it’s derivative, input as an inline<br />

% x0 -- starting guess, a number<br />

% <strong>to</strong>l -- desired <strong>to</strong>lerance, goes until |f(x)| <strong>to</strong>l % Do until the <strong>to</strong>lerence is reached.<br />

x = x - y/f1(x) % New<strong>to</strong>n’s formula<br />

y = f(x);<br />

end<br />

The statement while ... end is a loop, similar <strong>to</strong> for ... end, but instead of going through the<br />

loop a fixed number of times it keeps going as long as the statement abs(y) > <strong>to</strong>l is true.<br />

One obvious drawback of the program is that abs(y) might never get smaller than <strong>to</strong>l. If this<br />

happens, the program would continue <strong>to</strong> run over <strong>and</strong> over until we s<strong>to</strong>p it. Try this by setting the<br />

<strong>to</strong>lerance <strong>to</strong> a really small number:<br />

> <strong>to</strong>l = 10^(-100)<br />

then run the program again for f(x) = x 3 − 5.<br />

You can use Ctrl-c <strong>to</strong> s<strong>to</strong>p a program that’s stuck in an endless loop.

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

Saved successfully!

Ooh no, something went wrong!