![]() |
||
![]() |
Department of
Mathematics and Computer Science |
![]() |
![]() |
CS/Math 166 -- Winter 2018
|
|
20 points DUE: noon: Wednesday, January 17, 2018 |
BACKGROUND: If you remember from Calculus I, limx->0 sin(x)/x = 1. Thus, near x=0, sin x and x are approximately equal, though never exactly the same, except at x=0. This Machine Problem tries to show the problems one may have when using two nearly identical numbers in software.
HERE IS THE PROBLEM:
Using three different expressions, evaluate the function,
at the following values of x (put these in an array):
.05, .04, .03, .02, .01, .001, .0001, .00001, .000001, .0000001 .
FIRST, print out all the values using the expression for f(x) as given (i.e., directly) using the library function for sine and, initially, using single precision arithmetic. (See function step1 below.)
NEXT, print out the values after doing the following transformation.
First, transform sin x by using its Taylor (Maclaurin) expansion (try up to the 9th power).
Next, perform the obvious subtraction to eliminate the two occurrences of x in the algebraic expression. This algebraic simplification should be performed before any encoding of the expression.
Then, code the resulting algebraic expression, but keep the function in its series (i.e., summation) form.
THIRD, print out the values after using Horner's rule on the final algebraic expression derived in the previous step, and re-writing the expression into a nested, product form. Remember that Horner's rule requires that any expression be rearranged so that the highest power is first!
FOURTH, redo the FIRST PART, this time using double precision arithmetic (which is the Matlab/Octave default), by invoking a modified version of function step1 (see below) on array a rather than on asing.
HERE IS HOW I WOULD LIKE THE PROBLEM COMPLETED:
I would like you to use Matlab or Octave (a downloadable freeware clone of Matlab) and to use only single precision arithmetic to do the first three sections and then to use Matlab with (default) double precision for the fourth part.
To make your life easier, the following are running Matlab code segments for a "driver" program and the first section.
This is the "driver program," which invokes the function "step1" which does the first step. It also prints out the values, using standard C-like print statements. You should merely add more lines to this driver program as you complete the other "steps" for the problem, as described below. In the end there should be only one driver, in one file, and 4 other files, each contained a modified version of the "step1.m" file below.
%file mp1driver.m %driver for MP1 a = [.05, .04, .03, .02, .01, .001, .0001, .00001, .000001, .0000001]; asing = single(a); out1 = step1(asing); table1 = [asing; out1]; fprintf(' Step 1, original formula: f(x)= x-sin(x)\n x f(x)'); fprintf('\n %9.7f %14.7e',table1); fprintf('\n\n'); fprintf('The end\n');Next is the function "step1" which does the "first step." (You should write additional functions "step2," "step3," and "step4" modeled on this function, as well as modify the driver program to include calls to these functions and the print outs of the resulting values.)
% file step1.m (name taken from function name) function [out1] = step1(a) for i=1:10, out1(i)=a(i)-single(sin(a(i))); end; return;After you create these two files in your active Matlab directory and the other needed files, then in the Matlab "command window" you merely type the name of the driver routine, mp1driver, and the answers will appear on the screen in that window.
After testing and debugging your code, you should record the execution using the "diary" function (as in Homework 0) and turn in hard copies of your final driver program, all functions, and the diary (with the output).
If you have difficulty getting access to Matlab or Octave on Campus, please see me and you can use another environment (e.g., Maple) or a programming language (C, C++, Java, Fortran-95). Answers should be printed out in scientific notation (E-format). For the purposes of this problem, use only single precision arithmetic (if possible) for the first three steps and fifth step, even if you use other languages.
Whenever you use a programming language, turn in your program (using good style---indentation, comments) and the output (comment it as well).
Your program may be turned in to my office, O'Connor 7, or to
my mailbox in O'Connor 31.
Please staple the pages yourself
and do not turn in disconnected pages!
This page is maintained by Dennis C. Smolarski, S.J.
Email: dsmolarski at scu.edu
Last changed: 21 October 2017.