{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

erftrap

# erftrap - erftrap.m efunc = inline'2/sqrt(pi exp x.^2 a = 0...

This preview shows pages 1–3. Sign up to view the full content.

% erftrap.m efunc = inline( '2/sqrt(pi) * exp( - x.^2 )' ); a = 0; b = 1; true = erf(1); % compute composite trapezoid and error for multiple n powers = 2:8; N = 2.^powers + 1; I = []; err = []; for n = N h = (b-a)/(n-1); x = a:h:b; f = efunc( x ); trap = h * ( f(1)/2 + sum(f(2:(n-1))) + f(n)/2 ); abserr = abs( trap - true ); I = [I, trap]; err = [err, abserr]; end figure(1); clf; subplot(2,1,1); plot( N, I, 'o-', 'LineWidth', 1.5 ); xlabel( 'n' ); title( 'Integral: erf(x) using trapezoid rule' ); subplot(2,1,2); semilogy( N, err, 'o-', 'LineWidth', 1.5 ); xlabel( 'n' ); title( 'Absolute error' );

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
% erftrap.m efunc = inline( '2/sqrt(pi) * exp( - x.^2 )' ); a = 0; b = 1; true = erf(1); % compute composite trapezoid and error for multiple n powers = 2:8; N = 2.^powers + 1; I = []; I2 =[]; err = []; err2 = []; for n = N h = (b-a)/(n-1); x = a:h:b; f = efunc( x ); trap = h * ( f(1)/2 + sum(f(2:(n-1))) + f(n)/2 ); simp = h/3 * ( (f(1) + 4*sum(f(2:2:(n-1))) + 2*sum(f(3:2:(n-2))) + f(n) )); abserr = abs( trap - true ); abserr2 = abs( simp - true ); I = [I, trap]; I2 = [I2, simp ]; err = [err, abserr]; err2 = [err2, abserr2]; end figure(1); clf; subplot(2,1,1); plot( N, I, 'o-', 'LineWidth', 1.5 );
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern