
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

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:
∫ (^) m a f^ (x)^ dx^ and^ simpR^ ≈^
∫ (^) b m f^ (x)^ dx.
∫ (^) 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:
− 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