CS 107 - Introduction to Scientific Computation Homework #3 |

Submit using the following commands in the directory where you have saved these files on our system:

submit107 trapezoidIntegrate.m

submit107 simpleDifferentiate.m

**1. Polynomial: **A polynomial is a function of the form c_{0} *
*x*^{0} + c_{1} * *x*^{1} + c_{2}
* *x*^{2} + ... + c_{n} * *x*^{n}.
Since *x*^{0}=1, this is equivalent to c_{0} + c_{1}
* *x* + c_{2} * *x*^{2} + ... + c_{n} *
*x*^{n}. All polynomial curve can be expressed in this
form. The familiar line *y* = m*x* + b has n=1 with c_{1}
being slope m and c_{0} being *y*-intercept b. A quadratic
curve *y* = a*x*^{2} + b*x* + c has n=2 with c_{0},
c_{1}, and c_{2} being c, b, and a, respectively.

You will create a polynomial function "polynomial" in file polynomial.m that
takes in (1) a *scalar* x, and (2) a vector coeff of coefficients (e.g.
[c0 c1 c2]) and returns the y for that given polynomial. Note: The degree
n of the polynomial is implicitly given by the length of the coefficient vector.

Example usage:

>> polynomial(7, [5])

ans =

5

>> polynomial(7, [5 2])

ans =

19

>> polynomial(7, [5 2 3])

ans =

166

**2. Numerical Integration: **While symbolic integration, the symbolic
manipulation we
learn in Calculus classes, is important and has valuable applications, there are
many occasions where one is given raw data to integrate rather than a function.
Given a function y = f(*x*), integration attempts to find the area underneath the
curve over some interval. When one is given raw (*x*,*y*) values, a common
simple method for integration is, for each adjacent pair of points, (*x _{i}*,

For this exercise, we'll take a function sin(*x*) with a known integral
function -cos(*x*), and compare the results of this trapezoidal numerical integration
technique with the known correct results.

Begin by downloading the m-file called ```
trapezoidIntegrate.m
```

. and follow
the implementation instructions therein.

From 0 to 1.5708 with step 0.1, the approximate area is 0.928488. The actual area is 1, so the error is 0.0715117. From 0 to 1.5708 with step 0.001, the approximate area is 0.999204. The actual area is 1, so the error is 0.00079641. From 0 to 1.5708 with step 1e-005, the approximate area is 0.999994. The actual area is 1, so the error is 6.3268e-006.

Imagine you are standing at a point on a mountain slope, and you wish to measure the slope where you're standing. A single point altitude doesn't help. However, suppose you take an accurate altitude reading both one small step uphill and one small step downhill. By dividing the rise (change in altitude between the steps) by the run (distance horizontally between the two steps), we gain a good estimate for the slope where you're standing.

This analogy describes the simple *three-point estimation method*. Given an
*x*
value, a function *f*(*x*), and a small step *h*, one can estimate the derivative
(i.e. slope) of *f* at *x* as (*f*(*x* + *h*) - *f*(*x* -
*h*)) / (2 * *h*).

For this exercise, we'll again take the function sin(*x*) with a known
derivative function cos(*x*), and compare the results of this numerical
differentiation
technique with the known correct results along a sequence of points.

Begin by creating a function called `simpleDifferentiate.m`

.
Function `simpleDifferentiate `

will take dx and h as parameters (in
that order). It will return the greatest (maximum absolute) error. First, assign values to a set of
variables:

xMin = 0; % minimum x test value xMax = 2 * pi; % maximum x test value x = xMin:dx:xMax; % x test values

Next, compute a vector of *y* values for sin(*x*).

Plot *x* versus *y* as dots and hold the graph output.

Compute estimates and exact values for the derivative (cos(x)) at each *x* test
value, plotting each with "x" marks and lines, respectively. An example
plot for the variable values above appears below.

Finally, compute the maximum absolute error in the estimation, and display the
following output (with <placeholders> substituted with values):

From <xMin> to <xMax> with step <dx>, we numerically differentiate with h = <h>. The greatest error is <maximum absolute error> at x = <x value for maximum absolute error>.For example, here is the output of three m-file executions with

From 0 to 6.28319 with step 0.1, we numerically differentiate with h = 0.5. The greatest error is 0.0411489 at x = 0. From 0 to 6.28319 with step 0.1, we numerically differentiate with h = 0.05. The greatest error is 0.000416615 at x = 0. From 0 to 6.28319 with step 0.1, we numerically differentiate with h = 0.005. The greatest error is 4.16666e-006 at x = 0.