hw1 - 2%Construct Composite Trapezoidal Rule Quadrature function T=Int(h,a,b,f 0 is a function N=(b-a\/h for j=1:N 1 x(j)=a(j-1*h%assign value to x(j end

# hw1 - 2%Construct Composite Trapezoidal Rule Quadrature...

• Homework Help
• 4
• 100% (5) 5 out of 5 people found this document helpful

This preview shows page 1 - 4 out of 4 pages.

2. %Construct 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))); %approximated the definite integral end 3. clear; clc; y=0.5*(exp(1)-1); %the actual vaule of definite integral 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 size 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 quadratic trend. 4. 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 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 Because it eliminates the leading order (in h) contribution to the error, which leads to 2 order of convergence. 5. %(a) For N being even, T 2/N =1 clear; clc; for i=1:20 N(i)=2*i+1; h1(i)=2/N(i); z(i)=Int(h1(i),-1,1,inline( 'abs(x)' )); e1(i)=abs(z(i)-1); % error for different step size end figure(1) plot(h1,e1); xlabel ( 'step size' ); ylabel ( 'error' ); disp( 'the order of convergence is 2' )
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.02 0.04 0.06 0.08 0.1 0.12 step size error Based on plot, the order of convergence is 2 for N being odd. %(b) for i=1:4 h2(i)=1/(16*2^i); z(i)=Int(h2(i),0,1,inline( 'x.^0.5' )); e2(i)=abs(z(i)-(2/3)); % error for different step size end figure(2) plot(h2,e2); xlabel ( 'step size' ); ylabel ( 'error' ); 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.2 0.4 0.6 0.8 1 1.2 x 10 -3 step size e rro r I see a second order convergence to the exact value of the Integral because the plot shows quadratic trend.