E7_Lab10_Solutions_Fall_2010

E7_Lab10_Solutions_Fall_2010 - Solutions_Lab10

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

View Full Document Right Arrow Icon
Solutions_Lab10 file:///D:/work/acad/PG/SEM_1/E%207/lab%2010/html/Solutions_Lab10. .. 1 of 7 12-Nov-2010 11:29 PM Contents 1a. 1b. 2 3 4a. 4b. 5a. Alternate problem 5 solutions (acceptable for only students from Sec.16) 5b. 5c. clear all format compact 1a. type trapezoid function I=trapezoid(fun, a, b, n) % This is just one implementation of the trapezoid function using 'for' % loop I = 0; h = (b-a)/(n+1); x = a:h:b; for i = 1:n+1; I = I + 0.5*h*(fun(x(i)) + fun(x(i+1))); end end 1b. n_array=[11 21 51 101 201 501 1001]; I_exact=2; I_n = zeros(1,length(n_array)); e = zeros(1,length(n_array)); for i=1:length(n_array) I_n(i)=trapezoid(@sin, 0, pi, n_array(i)); e(i)=abs(I_exact-I_n(i))/abs(I_exact); end loglog(n_array, e) xlabel( 'Number of Points' ) ylabel( 'Relative Error' ) title( 'By trapezoid rule' )
Background image of page 1

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

View Full DocumentRight Arrow Icon
Solutions_Lab10 file:///D:/work/acad/PG/SEM_1/E%207/lab%2010/html/Solutions_Lab10. .. 2 of 7 12-Nov-2010 11:29 PM 2 for tol=[1e-2 1e-4 1e-6] [q,fctn]=quad(@test_fun,1,3,tol); disp([ 'For tolerance = ' ,num2str(tol), ' integral = ' , num2str(q), ... ' and number of function evaluations = ' ,num2str(fctn)]); end type test_fun For tolerance = 0.01 integral = -25.8547 and number of function evaluations = 21
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 01/21/2011.

Page1 / 7

E7_Lab10_Solutions_Fall_2010 - Solutions_Lab10

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online