Page 57 - Numerical Analysis Using MATLAB and Excel
P. 57

Chapter 2  Root Approximations


                %  \n prints a blank line before printing x1

                fprintf('The next approximation is: %9.6f \n', x1)
                fprintf('\n');                      % Inserts another blank line
                %
                fprintf('Rerun the program using this value as your next....
                approximation \n');
                The following lines show MATLAB’s inquiries and our responses (inputs) for the first two
                approximations.


                Enter coefficients of P(x) in descending order:
                [1  0 −5]
                Enter starting value: 2
                The next approximation is:  2.250000
                Rerun the program using this value as your
                next approximation
                Enter polynomial coefficients in
                descending order: [1  0 −5]
                Enter starting value: 2.25
                The next approximation is:  2.236111
                We observe that this approximation is in close agreement with (2.6).



                In Chapter 1 we discussed script files and function files. We recall that a function file is a user−
                defined function using MATLAB. We use function files for repetitive tasks. The first line of a
                function file must contain the word function followed by the output argument, the equal sign (=),
                and the input argument enclosed in parentheses. The function name and file name must be the
                same but the file name must have the extension .m. For example, the function file consisting of
                the two lines below


                function y = myfunction(x)
                y=x .^ 3 + cos(3 .* x)
                is a function file and must be saved as myfunction.m
                We will use the while end loop, whose general form is

                while expression
                      commands ...
                end
                where the commands ... in the second line are executed as long as all elements in expression of the
                first line are true.

                We will also be using the following commands:




               2−4                              Numerical Analysis Using MATLAB® and Excel®, Third Edition
                                                                             Copyright © Orchard Publications
   52   53   54   55   56   57   58   59   60   61   62