assignment_609_4

pdf

School

Texas A&M University *

*We aren’t endorsed by this school

Course

609

Subject

Electrical Engineering

Date

Apr 3, 2024

Type

pdf

Pages

6

Uploaded by DrLyrebirdMaster564

Report
M609 Assignment4 Lu Sun Fall 2018 1
Problem 3 Set up a matlab function which implements the above quadrature. The input to the function should be α , the function g(x), l and N. (3.1) For α = 1/4, 1/2, 3/4, use function to compute the approximations for N = 16, 32, 64, 128 for the case when g = 1. Report the errors that you obtain by computing the absolute value of your result minus the analytic value of the integral. You should be able to see the convergence rate, i.e., reducing h by a factor of 2 you should (approximately) see a reduction factor of 16 in the error. solution: code The code for function which implements the above quadrature is as follow: 1 function [app,err,rel] = quadt(a,gx,N) 2 %'a'=alpha,'gx'=g(x),'N'=N 3 %'app'=approximation value,'err'=errors,'rel'=actual value 4 5 6 % ******************* compute for intergral ******************** 7 syms xnew 8 f=xnew.ˆ(-a). * gx(xnew); 9 rel=double(int(f,0,1)); %result of intergral 10 11 12 % ******************** compute for L *************************** 13 L=double(int8(4/(1-a) * log2(N)-1/(1-a) * log2(1-a)-0.5)+1); 14 15 16 % ****************** compute for approximation value ************ 17 fun=@(x)x.ˆ(-a). * gx(x); %function of f(x) 18 IJ=0; 19 for i=1:1:L 20 y=(1/2)ˆ(L+1-i); %'y'=y(j) 21 k=y/N; %'k'=k(j)=(y(j+1)-y(j))/N 22 I=0; %'I'=I(j) 23 for m=1:1:N 24 I=I+fun(y+k * (m-1))+4 * fun((2 * y+2 * k * m-k)/2)+fun(y+k * m); 25 end 26 IJ=IJ+I * k/6; %'IJ'=I(f) 27 end 28 err=abs(rel-IJ); 29 app=IJ; 30 end Here, according to problem2(a), we choose L by the following formula: L = [ 4 1 - α log 2 ( N ) - 1 1 - α log 2 (1 - α )] + 1 Because in matlab ’int8’ following the rule of rounding, we need to minus ’0.5’. Namely we have the following code: 2
1 L=double(int8(4/(1-a) * log2(N)-1/(1-a) * log2(1-a)-0.5)+1); results The code for getting results is as follow: 1 % ************** approximation and error **************** 2 gx1=@(x)1; %'gx1':= g(x)=1 3 for i=1:1:3 4 for j=1:1:4 5 [app1(j,i),err1(j,i),rel] = quadt(i/4,gx1,8 * 2ˆj); 6 end 7 app1(j+1,i)=rel; %compute the real value by integral 8 end 9 10 11 % ************ comparing the error by '2' and '16' ************ 12 errnew11=[err1(1,:)/16;err1(2,:)/16;err1(3,:)/16]; 13 % each line of error divided by 16 14 15 errnew12=[err1(2,:) * 16;err1(3,:) * 16;err1(4,:) * 16]; 16 % each line of error multiply 16 The results for approximation are in the following form: α =1/4 α =1/2 α =3/4 N=16 1.33331895209011 1.99998923222711 3.99998726229199 N=32 1.33333269785592 1.99999932701621 3.99999920390285 N=64 1.33333328609759 1.99999995793854 3.99999995024408 N=128 1.33333332982229 1.99999999737116 3.99999999689026 analytic value 1.33333333333333 2 4 Table 1: Approximations for problem3(1) The results for error values are in the following form: α =1/4 α =1/2 α =3/4 N=16 1.43812432245038e-05 1.07677728873323e-05 1.27377080132440e-05 N=32 6.35477409138829e-07 6.72983794469673e-07 7.96097153532571e-07 N=64 4.72357466474449e-08 4.20614552076870e-08 4.97559229373223e-08 N=128 3.51104345597264e-09 2.62884025659105e-09 3.10974446193768e-09 Table 2: Error for problem3(1) comparison According to the result of ’errnew11’ and ’errnew12’, we can find that the error of N multiplied(or divided) by 16 = 2 4 approximately equals to the error of N 2 (or 2 × N ). They have same order of magnitude and the data from ’errnew11’ and ’errnew12’ is as follow: 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
error/16 N=16 8.98827701531491e-07 6.72985805458271e-07 7.96106750827752e-07 N=32 3.97173380711768e-08 4.20614871543545e-08 4.97560720957857e-08 N=64 2.95223416546531e-09 2.62884095048044e-09 3.10974518358265e-09 error × 16 N=32 1.01676385462213e-05 1.07677407115148e-05 1.27375544565211e-05 N=64 7.55771946359118e-07 6.72983283322992e-07 7.96094766997157e-07 N=128 5.61766952955622e-08 4.20614441054568e-08 4.97559113910029e-08 Table 3: Comparison (3.2) For α = 1/4, 1/2, 3/4, use your function to compute the approximations for N = 16, 32, 64, 128 and 512 for the case when g ( x ) = e x . Report the absolute value of the approximate error defined by subtracting the value that you obtain (for the first 4 values of N) from that obtained using N = 512. Again, you should be able to see the convergence rate. solution: code The code for function which implements the above quadrature is as follow: 1 function [app,err,rel] = quadt1(a,gx,N) 2 %'a'=alpha,'gx'=g(x),'N'=N 3 %'app'=approximation value,'err'=errors,'rel'=actual value 4 5 6 % ******************* compute for N=512 ******************** 7 rel=quadt(a,gx,512); %result of N=512 8 9 10 % ******************** compute for L *************************** 11 L=double(int8(4/(1-a) * log2(N)-1/(1-a) * log2(1-a)-0.5)+1); 12 13 14 % ****************** compute for approximation value ************ 15 fun=@(x)x.ˆ(-a). * gx(x); %function of f(x) 16 IJ=0; 17 for i=1:1:L 18 y=(1/2)ˆ(L+1-i); %'y'=y(j) 19 k=y/N; %'k'=k(j)=(y(j+1)-y(j))/N 20 I=0; %'I'=I(j) 21 for m=1:1:N 22 I=I+fun(y+k * (m-1))+4 * fun((2 * y+2 * k * m-k)/2)+fun(y+k * m); 23 end 24 IJ=IJ+I * k/6; %'IJ'=I(f) 25 end 26 err=abs(rel-IJ); 27 app=IJ; 28 end 4
results The code for getting results is as follow: 1 % ************** approximation and error **************** 2 gx2=@(x)exp(x); %'gx2':= g(x)=eˆx 3 for i=1:1:3 4 for j=1:1:4 5 [app2(j,i),err2(j,i),rel] = quadt1(i/4,gx1,8 * 2ˆj); 6 end 7 app2(j+1,i)=rel; %compute the real value by N=512 8 end 9 10 11 % ************ comparing the error by '2' and '16' ************ 12 errnew21=[err1(1,:)/16;err1(2,:)/16;err1(3,:)/16]; 13 % each line of error divided by 16 14 15 errnew22=[err1(2,:) * 16;err1(3,:) * 16;err1(4,:) * 16]; 16 % each line of error multiply 16 The results for approximation are in the following form: α =1/4 α =1/2 α =3/4 N=16 2.14146586286037 2.92529272341151 5.08513568103220 N=32 2.14147960873177 2.92530281879115 5.08514762346460 N=64 2.14148019697996 2.92530344975044 5.08514836985724 N=128 2.14148024070507 2.92530348918537 5.08514841650663 N=512 2.14148024420460 2.92530349180409 5.08514841850913 Table 4: Approximations for problem3(2) The results for error values are in the following form: α =1/4 α =1/2 α =3/4 N=16 1.43813442372576e-05 1.07683925807400e-05 1.27374769371968e-05 N=32 6.35472836130191e-07 6.73012940932694e-07 7.95044537760248e-07 N=64 4.72246490801354e-08 4.20536463430210e-08 4.86518976217099e-08 N=128 3.49953754863463e-09 2.61872035167698e-09 2.00250482862430e-09 Table 5: Error for problem3(2) comparison According to the result of ’errnew21’ and ’errnew22’, we can find that the error of N multiplied(or divided) by 16 = 2 4 approximately equals to the error of N 2 (or 2 × N ). They have same order of magnitude and the data from ’errnew21’ and ’errnew22’ is as follow: 5
error/16 N=16 8.98827701531491e-07 6.72985805458271e-07 7.96106750827752e-07 N=32 3.97173380711768e-08 4.20614871543545e-08 4.97560720957857e-08 N=128 2.95223416546531e-09 2.62884095048044e-09 3.10974518358265e-09 error × 16 N=32 1.01675653780831e-05 1.07682070549231e-05 1.27207126041640e-05 N=64 7.55594385282166e-07 6.72858341488336e-07 7.78430361947358e-07 N=128 5.59926007781542e-08 4.18995256268317e-08 3.20400772579887e-08 Table 6: Comparison 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help