The graph of the ellipse looks like this:

Using calculus, we can compute the volume enclosed by this surface to be 8.377580409572781 You'll use this value to verify your answer later.
The randomized method that we'll use to approximate the volume comes from observing that the shape is completely contained in the region where x and y range from -1 to 1, and z ranges from -2 to 2. If we randomly generate a ton of points in that region, and see what fraction of the points are inside the ellipse, then we can guess the volume.
inEllipse should return a vector called inout_vec which consists of 0s and 1s and has the same length as the input vectors. The element inout_vec(i) will be 0 if the corresponding point [xr(i), yr(i),zr(i)] is outside the ellipse, and 1 if it's inside.
The function as given below is almost complete. Only the last line needs to be finished. You should make use of r in the last line. Use an inequality like in the Monte Carlo lecture. Test your inequality on some points like (0,0,0) to make sure you assign the 1s and 0s to the right points.
function inout_vec = inEllipse(xr,yr,zr)Note that the formula that will be used here will be the formula given above, not the one in the slide (that's a unit sphere, which is certainly not what the ellipse looks like). Remember to save your file as inEllipse.m. Also remember to suppress all output by adding a semicolon at the end of the line.
% function inout_vec = inEllipse(xr,yr,zr)
% inEllipse: Determines if points are inside the ellipse.
% xr, yr, zr are all row vectors of same length. Returns a
% row-vector inout_vec of the same as the inputs.
inout_vec = ______________________________________;
When using 'vol', we must specify a box that contains the object we're measuring. We'll get the best results if we use the smallest box which contains the object. In our case, the range of the smallest box which contains our ellipse is:
| x from -1 to 1 |
| y from -1 to 1 |
| z
from -2 to 2 |
Make sure to enclose 'inEllipse' in single quotes when using it in the call to 'vol'.
In this portion of the MP, we will be writing MATLAB code
to compute the definite integral

using the recursive method called adaptive
integration. First we use the trapizoidal rule to
perform the integration over an interval [a,b], for a given
function y=f(x). For example see the figure below that shows
a trapezoid with A_approx = (1/2)*(f(a)+f(b))*(b-a) =
(f(0)+f(1))/2 where a = 0 , b=1 and f(x) = x2+2.

If the difference between the true area A and the
area in the trapezoida A_approx is small we are done.
But if we don't know A then how can we know that A_approx is
good? One way to test our approximation is to subdivide the
x axis into two intervals [a (a+b)/2] and [(a+b)/2, b] (or
[0 1/2] and [1/2 1] in our probleam) and form two trapezoids
(as shown in the figure below).

Our method is to compare the sum of the areas of the left
trapezoid and the right trapezoid AL_approx +
AR_approx with the area of the single trapezoid
A_approx and if the difference is small then we are done. If
the difference isn't small then we will use recursion
applied to AL and AR respectively.
We are now going to write a pair of functions to compute
definite integrals!
Open a new editor window by typing the command:
>> edit adaptive.m
Copy and paste the following code into a file called adaptive.m
function area = adaptive(fcn, a, b,N,axis_vec,tol)
% function area = adaptive(fcn, a, b,N,axis_vec,tol)
% Integrate the function y = fcn(x) on the interval [a,b] using N points and the trapezoidal rule.
% axis_vec is a vector [xmin xmax ymin ymax] that sets the size of the graph in the figure window.
hold on
x = linspace(a, b, N);
y = feval(fcn, x);
x2= [a,x,b];
y2 =[0,y,0];
fill(x2,y2,'g') % the fill command fills a polygon in the plane. The closed region contains the points (a,0) and (b,0)
% 'g' stands for green
axis(axis_vec)
pause(.3)
A = trapz (x, y); % compute the Area using the trapezoidal formula
x = linspace(a, (a + b)./2, N);
y = feval(fcn, x);
AL = trapz(x, y); % compute the area on the left half of the interval.
x = linspace((a+b)./2, b, N);
y = feval(fcn, x);
AR = trapz(x, y); % compute the area on the right half of the interval
if abs(((AR + AL)-A)) < tol % if close enough done so assign output variable 'area' and fill the area in with red
area = ________________________________;% assign correct value to area
fill(x2,y2,'r')
axis(axis_vec)
else % else we haven't satisifed the tolerance set by the user so compute the area of the left half and add the area
% computed by the right half. Fill in the blanks with recursive calls. You will have two calls to the function
% adaptive.
area = ________________________________________________________; % use recursion to sum areas between a , (a+b)/2 and (a+b)/2 and b.
end
Now, let's test it! At the Matlab prompt try running the
commands:
>> close all
>> format long
>> area = adaptive(@(x)sin(1./x).^2./x.^2,1/(30*pi),1/(10*pi),925,[1/(30*pi) 1/(10*pi) 0 10000], 1.0e-10)
Executing the above command may take several minutes.
Answer Part 2:
Question 2 on your answer sheet.

When you submit your
solution you can leave either one of the
three choices listed above.
Assume that at t =
0 , x = 1 and v = 0. Use the time
interval [0, 10].
3. Develop Algorithm (processing steps to solve
problem)
The ODE is described by
the harmonic oscillator:

Answer Part 3:
Question 1 on your answer sheet.
4. Write the
Function (Code)
The first
line should be
function xvp = xvprime(t , xv)
%
use m = 1 k = 100 b = 2 as values for now then after you
have done a plot then change b = 20 and later b = 5
5. Test and Debug the Code
Answer Part 3:
Question 2 on your answer sheet.
6. Run the Code
After you have run the
code, plot the solution. Do this for each of the three cases
for b = 2,20,5.
Answer Part 3:
Question 3 on your answer sheet.
You will not handin
the plots. Answer
Part 3: Question 4 on your answer sheet.
>> ray_trace('circle')
>> ray_trace('ellipse')
>> ray_trace('parabola')
>> ray_trace('circle')
>> ray_trace('ellipse')
If you have errors then go back to your code for the ray_trace function.
>> ray_trace('parabola')