Introduction to Matlab

Mathematics University of Queensland Mathematics

10  Using Functions

We continue to work with the function
f(x) = 1
(x-.3)2+.01
+ 1
(x-.9)2+.04
-6.
coded in the previous section.

10.1  1D Minimization.

Matlab can find maxima and minima, but first we should read off the answers from a graph.

Exercise 28

From the graph of f, write down the values of x which give

(a.) the minimum of f on the interval [.25, 1],

(b.) the minimum of f on the interval [.5, 1],

(c.) the maximum of f on the interval [0, .5], and

(d.) the maximum of f on the interval [0, 4].

The point at which f takes its minimum is found using the function fmin.

    xmin = fmin( 'f', .25, 1) , ymin = f( xmin )

This asks for the minimum of `f' in the interval [.25,1]. The answer coincides with the value expected from the graph. The procedure fmin uses a numerical method to find the minimum. It repeatedly evaluates the function until it is confident the minimum has been found to the required accuracy. In the above call to fmin the user did not specify the accuracy required, so the standard (or default) accuracy was used. Reading the information in help fmin tells us the default is a relative error of £ 1×10-4.

10.1.1  Accuracy

A better answer can be found by specifying the required accuracy in an extra argument in fmin.

    xmin_8 = fmin( 'f', .25,1, [0,1.e-8]), ymin_8 = f( xmin_8 )

(1.e-8 is shorthand for 1×10-8.) With this call to fmin, xmin_8 should be accurate to within a relative error of ±1×10-8; that is to about 8 significant digits. However, we cannot see this extra accuracy at the moment, as answers are only displayed to 4 or 5 digits. To see the answer fully we must change the display format. (That is the way Matlab displays numbers.) This can be done two ways:-

Now Matlab displays enough significant digits in the numbers to see that the answers are different.

    [xmin ; xmin_8]     % Compare the two answers

Displayed in the long format, we see the two answers differ after the 6th decimal place, showing fmin has done more computations in the second case to get a slightly better answer.

 

It is important to realise that format does not change the accuracy of the numbers or the calculations: it just changes the number of digits displayed on the screen. Our first answer xmin is still only accurate to about 6 significant figures, even though it may be displayed to 15 figures.

 

Exercise 29

There are two ways to verify our answer

(1.)    Evaluate f at xmin and xmin_8 and see which answer gives the smaller value of f.

(2.)    Evaluate f at xmin_8-1.e-8 and xmin_8+1.e-8. The values either side of the computed minimum should be larger.

To save space on the screen, we can change back to the short display using the `options' menu or the `format short e' command. Other useful formats are long f and short f. Do not use the rational format for serious computations. The fractions displayed are only approximations to the numerical answers.

Exercise 30

   

(1.)    Find the minimum of f on [.5,1]. Choose an error tolerance so that the minimum is correct to ±10-6. What is the value of f at this point? If x* denotes the computed solution, check the accuracy of the answer by evaluating f at x*+10-6 and x*-10-6. f should be larger at both these neighbouring points. This shows the true minimum should lie between x*-10-6 and x*+10-6

(2.)    Use fmin to find the maximum of f(x). (Hint: There is no function fmax! Instead, to find the maximum of f(x), we can find the minimum of -f(x). This means altering the code in the file `f.m' so that it calculates -f(x) rather than f(x).) Use [0,.5] as the original interval. Find the maximum point and the value of f at the maximum correct to ±1×10-6.

(3.)    Repeat the previous part and find the maximum of f(x) in the wider interval [0, 4]. How accurate is the answer? Explain by looking at the graph of f ! (It is useful to graph a function before looking for a maximum or minimum!!)

10.2  Finding Zeros

Matlab also finds the zeros of functions; that is values of x for which
f(x) = 0.
For example, to find a zero of the function f near the point x = -1 use the command

    xzero = fzero( 'f' , -1)

The answer should be accurate to at least the default tolerance. (Which is in fact around 1×10-16, even though the help file claims it is 1×10-4!!) The command help fzero gives more information about changing the default tolerance.

Exercise 31

   

(1.)    Check the previous result from fzero by

    1. Checking the answer is approximately true from the graph of f,

    2. Checking f is approximately 0 at xzero.

    3. Checking f is negative at xzero-10-4 and positive at xzero+10-4. This shows that f crosses the axis somewhere in the small interval between xzero-10-4 and xzero+10-4.

(2.)    Find the zero of f near x = 1 using the default tolerance. What answer is given when the default tolerance is changed to 1×10-6 and then to 1×10-12. Check carefully to find which answer is the most accurate.

10.3  Integration.

Matlab also uses functions when it evaluates definite integrals numerically. This is known as quadrature or numerical integration. Suppose we want to evaluate the integral of f over [-1,2], that is to find
ó
õ
2

-1 
f(x) dx.
(2)
(Or equivalently the area under the curve f between x = -1 and x = 2.) This is done by the function quad8; for example.

    integral1 = quad8( 'f', -1,2 )

This command used quad8 to compute the answer to within the default accuracy. For quad8 this is a relative accuracy of ±1×10-3, or ±.1%. It is always wise to check, so we can now get quad8 to do more work and try to achieve a relative accuracy of 1×10-8. The two answers can then be compared, either by displaying them in a long format or by dinding the difference between them.

    integral2 = quad8( 'f', -1,2, 1.e-8 )
    [ integral1 ; integral2 ]    % Use a long formats
                                 %  to show the difference
    (integral2-integral1)/integral2

The relative difference between these two answers is 8.5×10-9, suggesting the first answer was much more accurate than expected! (Of course they could both be wrong; but this is unusual.)

10.3.1  Accuracy

It is hard to check the accuracy of Matlab's integration routine. One way is to code up a simple method of numerical integration in Matlab. In this case we use the midpoint rule. We divide the interval [-1,2] up into 15 smaller subintervals using the grid points
-1,-.8,-.6,¼,1.6,1.8,2.
The area under the curve on each intervalis approximated by
width of the interval ×  f(   midpoint of the interval )
These individual approximations are then summed to get the overall area. That is we approximate the area under f by
ó
õ
2

-1 
f(x) dx    »     2-(-1)
15
(f(-.9)+f(-.7)+f(-.5)... +f(1.7)+f(1.9))
This rule is concisely coded in Matlab by

x = ( -.9 : .2 : 1.9 )' ; fx = f( x ) ;
integral = sum( fx ) * 3 / 15

This crude approximation is not as accurate as that computed by quad8. However it is an independent way to show us that our answer is approximately correct.

 Graph. The Midpoint Rule.

Exercise 32

   

(a.)    Find the value of
1

Ö

2p
ó
õ
3

-3 
e-x2/2 dx
to a relative accuracy of .01% using quad8.

(b.)    Check the answer using the midpoint rule with 30 points. (Hint: Evaluate f at the points x=(-2.9:.2:2.9). Use a 60 point midpoint rule to give a better answer.