2 Constructing a Composite Trapezoidal Rule Quadrature function TInthabf f is a

# 2 constructing a composite trapezoidal rule

• Homework Help
• 6
• 100% (10) 10 out of 10 people found this document helpful

This preview shows page 1 - 5 out of 6 pages.

2. %Constructing a Composite Trapezoidal Rule Quadrature function T=Int(h,a,b,f) %f is a function N=(b-a)/h; for j=1:N+1 x(j)=a+(j-1)*h; %assign value to x(j) end y=f(x); %create value for function f at x(j) T=h*(sum(y)-0.5*(y(1)+y(N+1))); %approximate the definite integral end 3. clear; clc; y=0.5*(exp(1)-1); %the actual value of definite integral
Norbert Shao 1/25/17 Tuesday 10am for i=1:4 h(i)=1/(10*i); z(i)=Int(h(i),0,1,inline( 'x.*exp(x.^2)' )); e(i)=abs(z(i)-y); % error for different step sizes end plot(h,e); xlabel ( 'step size' ); ylabel ( 'error' ); 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0 1 2 3 4 5 6 x 10 -3 step size error T h has a convergent trend at the expected quadratic rate since the plot shows a quadratic trend. 4.
Norbert Shao 1/25/17 Tuesday 10am clc; clear; for j=1:20 N(j)=10*j; h(j)=1/N(j); % assign value to h q(j)=qf(h(j)); % compute value of q given h end plot(h,q); xlabel ( 'step size' ); ylabel ( 'q' ); h=0.005 for which q(h) is approximately equal to 4 %(a) T1=Int(0.005,0,1,inline( 'exp(-x.^2)' )); T2=Int(0.0025,0,1,inline( 'exp(-x.^2)' )); error=(4/3)*(T2-T1); % approximated error disp( 'approximated error is' ); disp(error) Approximated error is 1.5328e-06 %(b) s=T1+error; disp( 'extrapolated, improved, approximation is' ); disp(s); Extrapolated, improved, approximation is 0.7468
Norbert Shao 1/25/17 Tuesday 10am 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 4 4.0005 4.001 4.0015 step size q