Homework #3
 CS 107 - Introduction to Scientific ComputationHomework #3

Due: Wednesday 2/9 at the beginning of class

1. Cooling Coffee: Exercise 5.5, based on 5.7 Project: "Time for a Cool Cup of Coffee".  Call your m-file `coffeecooling.m`.  You will need the text data file `coffeecooling.csv` in your working directory, although you need not submit it.  Do not make a printed copy of the graph.  (Go paperless!) Instead, place appropriate plot commands in your m-file.   Try to achieve a result similar to Figure 5.5.  NOTE: Units for the equations are in minutes, while the CSV time data is in seconds.

Here’s a step-by-step process for easily adding an m-file instruction for an inserting an arrow (or similar elements) into your graph.

• Create an m-file that generates the raw graph.
• In the Figure window, choose the “Insert” menu item, then “Arrow”.
• Click and drag to draw your arrow.
• Right-click on the resulting arrow and choose (“Show M-Code”).
• Take the last line of the generated file, removing the “figure1, ” first parameter.

For instance, a quick arrow draw yielded the following m-code:

function createarrow(figure1)
%CREATEARROW(FIGURE1)
%  FIGURE1:  annotation figure
%  Auto-generated by MATLAB on 04-Feb-2010 11:29:08
% Create arrow
annotation(figure1,'arrow',[0.5253 0.311],[0.7252 0.3591]);

I would then add the following to my plotting m-file:

annotation('arrow',[0.5253 0.311],[0.7252 0.3591]);

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, (xi, f(xi)) and (xi+1, f(xi+1)) to consider the trapezoid formed by these points and (xi, 0) and (xi+1, 0).  A simple, good numerical integration approximation of the integral of a function can be formed by computing and summing the area of these trapezoids.  Note: Area below the axis y = 0 is considered negative area.

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 creating an m-file called `trapezoidIntegrate.m`. First, assign values to a set of variables:

```dx = .1; % x step size
xMin = 0; % minimum x value
xMax = pi/2; % maximum x value
x = xMin:dx:xMax; % all x values
```

Next, compute a vector of y values for sin(x).
Plot x versus y.
Compute and sum the areas of the trapezoids.
Given that the exact area under the curve is cos(xMin) - cos(xMax), display the following output (with <placeholders> substituted with values):

```From <xMin> to <xMax> with step <dx>, the approximate area is <area estimate>.
The actual area is <exact area>, so the error is <difference between exact and estimate>.
```
For example, here is the output of three m-file executions with dx assigned .1, .001, and .00001, respectively.
```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.```
3. Numerical Differentiation: Differentiation is the process of computing the rate of change, or slope, of a function.  In cases where we are given a function that has no simple symbolic differentiation, we can turn to numerical differentiation

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 an m-file called `simpleDifferentiate.m`. First, assign values to a set of variables:

```h = .5; % fixed small step
dx = .1; % step between our x test values
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 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 h assigned .5, .05, and .005, respectively.
```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.```