Docsity

MATH 128A Summer 2009 Assignment 2: Adaptive Simpson Integrator, Assignments of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
MATH 128A, SUMMER 2009: PROGRAMMING ASSIGNMENT 2
In this assignment, you will write an adaptive Simpson integrator that draws its piecewise-quadratic
estimate of the integrand.
You may use the following function to plot three points (described as vectors of x- and y-coordinates),
the parabola passing through them, and vertical lines bounding the parabola, all on top of an existing plot:
function plot_parabola(xi,yi)
xdata = linspace(xi(1), xi(end));
ydata = polyval( polyfit(xi, yi, 2), xdata);
hold on
plot(xi, yi, ’o’, xdata, ydata)
plot([xi( 1 ) xi( 1 )], [0 yi( 1 )]);
plot([xi(end) xi(end)], [0 yi(end)]);
hold off
The “hold” commands tell MATLAB not to overwrite existing plots.
(1) Write “function q=adaptive_simpson(f,a,b,tol)” implementing the adaptive Simpson’s algo-
rithm. Your code should:
•Only evaluate f(at x=a,x=b, etc.) at the beginning of the function (to make #2 easier.)
•Compute the Simpson’s rule estimates simpL ≈Rm
af(x)dx and simpR ≈Rb
mf(x)dx.
•Compute q=simpL +simpR and draw the two relevant parabolas, if this agrees with a simple
Simpson’s rule estimate to within 10 ∗tol.
•Compute qby recursively using adaptive_simpson to estimate Rm
af(x)dx +Rb
mf(x)dx, if not.
(2) Optimize your adaptive_simpson so that it requires only two evaluations of fper step, by adding
an extra parameter evals.
Your modified function should:
•Take the form “function q=adaptive_simpson(f,a,b,tol,evals)”.
•Re-use the evaluations evals(1) =f(a), evals(2) =f(m=a+b
2), evals(3) =f(b).
•Begin with the following code to make the parameter evals optional. This code will also plot
a graph of fif evals is not passed, so that the graph will be drawn when the function is first
called.
if nargin < 5
evals = [ f(a) f((a+b)/2) f(b)];
plot(linspace(a,b), f(linspace(a,b)))
end
(3) Use your adaptive_simpson to estimate R2
−2|x2−3|dx and R3
0sin(x3)dx.
In each case, use tol = 0.01. Report the estimate, the absolute error, and the resulting picture.
If you did not complete problem 2, make sure you plot the function before calling adaptive_simpson.
Is the absolute error smaller than the tolerance? Could this happen even if your code is correct?
Your report should contain all code needed to reproduce your results, including commands used in #3.
Numerical output should be accurate to six significant digits. If you finish #2 and your function works as
expected, you do not need to include code for #1.
Date: Due Tuesday 8/4.
1

Partial preview of the text

Download MATH 128A Summer 2009 Assignment 2: Adaptive Simpson Integrator and more Assignments Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

MATH 128A, SUMMER 2009: PROGRAMMING ASSIGNMENT 2

In this assignment, you will write an adaptive Simpson integrator that draws its piecewise-quadratic estimate of the integrand. You may use the following function to plot three points (described as vectors of x- and y-coordinates), the parabola passing through them, and vertical lines bounding the parabola, all on top of an existing plot:

function plot_parabola(xi,yi) xdata = linspace(xi(1), xi(end)); ydata = polyval( polyfit(xi, yi, 2), xdata); hold on plot(xi, yi, ’o’, xdata, ydata) plot([xi( 1 ) xi( 1 )], [0 yi( 1 )]); plot([xi(end) xi(end)], [0 yi(end)]); hold off

The “hold” commands tell MATLAB not to overwrite existing plots.

(1) Write “function q=adaptive_simpson(f,a,b,tol)” implementing the adaptive Simpson’s algo- rithm. Your code should:

  • Only evaluate f (at x = a, x = b, etc.) at the beginning of the function (to make #2 easier.)
  • Compute the Simpson’s rule estimates simpL ≈

∫ (^) m a f^ (x)^ dx^ and^ simpR^ ≈^

∫ (^) b m f^ (x)^ dx.

  • Compute q = simpL + simpR and draw the two relevant parabolas, if this agrees with a simple Simpson’s rule estimate to within 10 ∗ tol.
  • Compute q by recursively using adaptive_simpson to estimate

∫ (^) m a f^ (x)^ dx^ +^

∫ (^) b m f^ (x)^ dx, if not. (2) Optimize your adaptive_simpson so that it requires only two evaluations of f per step, by adding an extra parameter evals. Your modified function should:

  • Take the form “function q=adaptive_simpson(f,a,b,tol,evals)”.
  • Re-use the evaluations evals(1) = f (a), evals(2) = f (m = a+ 2 b), evals(3) = f (b).
  • Begin with the following code to make the parameter evals optional. This code will also plot a graph of f if evals is not passed, so that the graph will be drawn when the function is first called. if nargin < 5 evals = [ f(a) f((a+b)/2) f(b)]; plot(linspace(a,b), f(linspace(a,b))) end (3) Use your adaptive_simpson to estimate

− 2 |x

(^2) − 3 | dx and ∫^3 0 sin(x

(^3) ) dx. In each case, use tol = 0.01. Report the estimate, the absolute error, and the resulting picture. If you did not complete problem 2, make sure you plot the function before calling adaptive_simpson. Is the absolute error smaller than the tolerance? Could this happen even if your code is correct? Your report should contain all code needed to reproduce your results, including commands used in #3. Numerical output should be accurate to six significant digits. If you finish #2 and your function works as expected, you do not need to include code for #1.

Date: Due Tuesday 8/4. 1