# Assignment 6

- Time
- 25min
- Activity
- Discussion

Apparently people are confused about A6. Let's discuss.

- For splitting data, remember indexing with arrays (last lab).
- The bounding box calculations turn out to be easy, because of the way the builtins work.
- Bucketing is the first tricky part.
- Computing the hash function is actually a standard cs1083 programming problem, let's do a couple examples.
- My solution to
`tally`

relies on the (two return) use of`sort`

used in last lab.

# Minmax

- Time
- 25min
- Activity
- Small Groups

- Save the following skeleton as
`~/fcshome/cs2613/labs/L27/minmax.m`

```
function [minf, maxf] = minmax(fun, a,b)
endfunction
```

Complete the

`minmax`

function to find the approximate minimum and maximum of a function in some interval by sampling. Don't use any loops.Add a usage block so that

`help minmax`

works in Octave.Increase the number of samples until the following tests pass. Can you guess what the important difference is between these two tests, with respect to accuracy?

```
%!test
%! [in,ax] = minmax(@(x)(x),-1,1);
%! assert([in,ax]==[-1,1])
%!test
%! [in,ax] = minmax(@(x)(x.^2),-1,1);
%! assert([in,ax],[0,1],1e-6)
```

- Why does the second test use
`.^`

instead of`^`

. What's the difference in Octave?

## Monte Carlo Integration I: Error bounds

- Time
- 25min
- Activity
- Small Groups

Save mcintgr.m as

`~/fcshome/cs2613/labs/L24/mcintgr.m`

.This function is discussed in

*More advanced function programming: Monte Carlo integration*in Chapter 5 of*Gnu Object Beginner's Guide*.In order to write some tests for

`mcintgr`

, we need to need two things. The first is a way to compute the "real answer", and the second is an error tolerance. We can integrate numerically (which is what mcintgr is supposed to do) with the built in function`quad`

.In order to get some idea about error tolerance, we need to run

`mcintgr`

many times, because it's behaviour is non-deterministic.Use the variable argument version of

`timeit`

as a base for the following function:

```
function [max_err, mean_err, std_err]= error_rate(reps, answer, fun, varargin)
endfunction
```

- Your function should compute the
`max`

,`mean`

, and standard deviation of the difference between`answer`

and the value of calling`fun`

. You can use`demo error_rate`

to run the following

```
%!demo
%! answer=quad(@sin, 0, pi)
%! [mx, mn, std] = error_rate(100, answer, @mcintgr, @sin, 0, pi, 100)
%!demo
%! answer=quad(@sin, 0, pi)
%! [mx, mn, std] = error_rate(100, answer, @mcintgr, @sin, 0, pi, 1000)
%!demo
%! fun=@(x)(x.^(4/3).*log(x)./cosh(x.^2 + x));
%! answer=quad(fun, 1, e)
%! [mx, mn, std] = error_rate(100, answer, @mcintgr, fun, 1, e, 100)
%!demo
%! fun=@(x)(x.^(4/3).*log(x)./cosh(x.^2 + x));
%! answer=quad(fun, 1, e)
%! [mx, mn, std] = error_rate(100, answer, @mcintgr, fun, 1, e, 1000)
```

- You will notice that the errors for the second, more complicated
function has a much smaller error values. What happens if you
divide both sets of error values by
`answer`

? This is called the*relative error*. Which value is important depends on the application.

# Vectorized Monte Carlo integration

- Time
- 25min
- Activity
- Small Groups

As usual, we want to try to vectorize our function. This is roughly
based on function `mcintgrv`

in GOBG Chapter 5.

Save following as `~fcshome/cs2613/labs/L24/mcintgrv.m`

, and follow
the comments to complete the code.

```
function I = mcintgrv(fun, a, b, mcloops)
[minf, maxf] = minmax(fun,a,b);
## find width of interval
width= b-a;
## compute area of bounding rectangle
area= width * maxf
## Choose mcloops places to sample, by generating random x values within the box
x = a + _______ ___ rand(mcloops,1);
## Choose mcloops y values within the box
y = maxf _________________________________________
## Evaluate the function on all of our x positions
fx = _______________________________________
## Counts the number of sample points that lie under the graph
counter = _______________________________________
## Esimate the integral
I = (counter/mcloops)*area
endfunction
```

- How can you determine if your vectorized version is getting (roughly) the right answer?