Sei sulla pagina 1di 16

ANALISIS NUMERICO

CURSO DOCENTE ALUMNO IVAN TRABAJO


GAUSSIANA

: : :

ANALISIS NUMERICO DR. WILSON MACO V. GOICOCHEA PRINCIPE, EVER

PROGRAMA

DE CUADRATURA

CICLO

IV

TRUJILLO-PER 2010

%ADAPTATIVE QUADRATE ALGORITH 4.3 % %To aproximate I=integral((f(x)dx)) from a to b to within %a given tolerance TOL: % % INPUT: endpoints a,b; tolerance TOL; limit N to number of levels % % OUTPUT: aproximation APP or message that N is exceeded. syms('OK','AA','BB','EPS','N','CNT','APP','I','TOL','A','H','FA'); syms('FC','FB','S','L','FD','FE','S1','S2','V','LEV','s','x'); TRUE = 1; FALSE = 0; fprintf(1,'This is adaptative quadrature with simpsons method.\n\n'); fprintf(1,'Input the function F(x)in terms of x\n'); fprintf(1,'For example: cos(x)\n'); s = input(' ','s'); F = inline(s,'x'); OK = FALSE; while OK == FALSE fprintf(1,'Input lower limit of integration and '); fprintf(1,'upper limit of integration\n'); fprintf(1,'on separate lines\n'); AA = input(' '); BB = input(' '); if AA > BB fprintf(1,'Lower limit must be less than upper limit\n'); else OK = TRUE; end end OK = FALSE; while OK == FALSE fprintf(1,'Input tolerance.\n'); EPS = input(' '); if EPS > 0 OK = TRUE; else fprintf(1,'Tolerance must be positive.\n'); end end OK = FALSE; while OK == FALSE fprintf(1,'Input the maximun number of levels.\n'); N = input(' ');

if N > 0 OK = TRUE; else fprintf(1,'Number must be positive\n'); end end if OK == TRUE CNT = 0; OK = TRUE; %step 1 APP = 0; I = 1; TOL = zeros(1,N); A = zeros(1,N); H = zeros(1,N); FA = zeros(1,N); FC = zeros(1,N); FB = zeros(1,N); S = zeros(1,N); L = zeros(1,N); FD = zeros(1,N); FE = zeros(1,N); V = zeros(1,N); TOL(I) = 10*EPS; A(I) = AA; H(I) = 0.5*(BB-AA); FA(I) = F(AA); CNT = CNT+1; FC(I)= F((AA+H(I))); CNT = CNT+1; FB(I) = F(BB); CNT = CNT+1; %Aproximate from simpson's method for entire interval S(I) = H(I)*(FA(I)+4*FC(I)+FB(I))/3; L(I) = 1; %STEP 2 while I > 0 && OK == TRUE %STEP 3 FD = F((A(I)+0.5*H(I))); CNT = CNT+1; FE = F((A(I)+1.5*H(I))); CNT = CNT+1; %Aproximate from simpson's method for halves of intervals S1 = H(I)*(FA(I)+4*FD+FC(I))/6; S2 = H(I)*(FC(I)+4*FE+FB(I))/6; %save data at this level V(1) = A(I); V(2) = FA(I); V(3) = FC(I); V(4) = FB(I);

V(5) = H(I); V(6) = TOL(I); V(7) = S(I); LEV = L(I); %STEP 4 %DELETE THE LEVEL I = I-1; %STEP 5 if abs(S1+S2-V(7)) < V(6) APP = APP+(S1+S2); else if LEV >= N OK = FALSE; %procedure fails else % Add one level % Datafor right half subinterval I = I+1; A(I) = V(1)+V(5); FA(I) = V(3); FC(I) = FE; FB(I) = V(4); H(I) = 0.5*V(5); TOL(I) =0.5*V(6); S(I)= S2; L(I) = LEV+1; %Data for left half subinterval I = I+1; A(I) = V(1); FA(I)= V(2); FC(I)=FD; FB(I)= V(3); H(I)= H(I-1); TOL(I) = TOL(I-1); S(I)= S1; L(I) = L(I-1); end end end if OK == FALSE fprintf(1,'Level exceedded.method did not produce an\n'); fprintf(1,'accurate approximation.\n'); else fprintf(1,'\nThe integral of F from %12.8f to %12.8f is\n',AA,BB); fprintf(1,'%12.8f to within %14.8e\n',APP,EPS); fprintf(1,'The number of function evaluations is: %d\n',CNT); end end

EJEMPLO N1 >> programa1 This is adaptative quadrature with simpsons method. Input the function F(x)in terms of x For example: cos(x) cos(x) Input lower limit of integration and upper limit of integration on separate lines 1 7 Input tolerance. 0.0001 Input the maximun number of levels. 4 The integral of F from 1.00000000 to 7.00000000 is -0.18448028 to within 1.00000000e-004 The number of function evaluations is: 29 EJEMPLO N2 >> programa1 This is adaptative quadrature with simpsons method. Input the function F(x)in terms of x For example: cos(x) sin(x) Input lower limit of integration and upper limit of integration on separate lines -1 1 Input tolerance. 0.0001 Input the maximun number of levels. 5 The integral of F from -1.00000000 to 1.00000000 is 0.00000000 to within 1.00000000e-004 The number of function evaluations is: 5 EJEMPLO N3 >> programa1 This is adaptative quadrature with simpsons method. Input the function F(x)in terms of x For example: cos(x)

x Input lower limit of integration and upper limit of integration on separate lines 1 5 Input tolerance. 0.0001 Input the maximun number of levels. 3 The integral of F from 1.00000000 to 5.00000000 is 12.00000000 to within 1.00000000e-004 The number of function evaluations is: 5 EJEMPLO N4 >> programa1 This is adaptative quadrature with simpsons method. Input the function F(x)in terms of x For example: cos(x) x^2 + 3*x -2 Input lower limit of integration and upper limit of integration on separate lines 1 8 Input tolerance. 0.0001 Input the maximun number of levels. 5 The integral of F from 1.00000000 to 8.00000000 is 250.83333333 to within 1.00000000e-004 The number of function evaluations is: 5 EJEMPLO N5 > programa1 This is adaptative quadrature with simpsons method. Input the function F(x)in terms of x For example: cos(x) cos(x) + x^2 Input lower limit of integration and upper limit of integration on separate lines 1 9 Input tolerance. 5 Input the maximun number of levels.

5 The integral of F from 1.00000000 to 9.00000000 is 242.16809030 to within 5.00000000e+000 The number of function evaluations is: 5 >> EJEMPLO N6 >> programa1 This is adaptative quadrature with simpsons method. Input the function F(x)in terms of x For example: cos(x) 5*x^3 + 2*x^2 - x +20 Input lower limit of integration and upper limit of integration on separate lines 1 5 Input tolerance. 0.000000001 Input the maximun number of levels. 7 The integral of F from 1.00000000 to 5.00000000 is 930.66666667 to within 1.00000000e-009 The number of function evaluations is: 5 EJEMPLO N7 > programa1 This is adaptative quadrature with simpsons method. Input the function F(x)in terms of x For example: cos(x) 5*x^3 + 2*x^2 - x + 10 Input lower limit of integration and upper limit of integration on separate lines 0 9 Input tolerance. 0.0001 Input the maximun number of levels. 4 The integral of F from 0.00000000 to 9.00000000 is 8736.75000000 to within 1.00000000e-004 The number of function evaluations is: 5

% GAUSSIAN DOUBLE INTEGRAL ALGORITHM % % To approximate I = double integral ((f(x , y)dy dx)) with limits % of integration from a to b for x and from c(x) to d(x) for y: % % INPUT: endpoints a, b; positive integers m, n. (Assume thet the % roots r(i,j) and coefficients c(i,j) are available for % i equals m and n for 1<= j <=i. % %OUTPUT: approximation J to I. syms('OK', 'A', 'B', 'M', 'N', 'r', 'co', 'H1', 'H2', 'AJ', 'I', 'X'); syms('JX', 'C1', 'D1', 'K1', 'K2', 'J', 'Y', 'Q', 'x', 's', 'Y'); TRUE = 1; FALSE = 0; fprintf(1,'This is Gaussian Quadrature for double integrals.\n'); fprintf(1,'Input the function F(x,y) in ternas of x and y\n'); fprintf(1,'For example: sqrt(x^2+y^2)\n'); s = input(' ','s'); F = inline(s,'x','y'); fprintf(1,'Input the function C(x), and D(x) in terms of x '); fprintf(1,'on separate lines\n'); fprintf(1,'For example: cos(x) \n'); fprintf(1,' sin(x) \n'); s = input(' ','s'); C = inline(s,'x'); s = input(' ','s'); D = inline(s,'x'); OK = FALSE; while OK == FALSE fprintf(1,'Input lower limit of integration and upper limit of\n '); fprintf(1,'integration on separate lines\n'); A = input(' '); B = input(' '); if A > B fprintf(1,'Lower limit must be less than upper limit\n'); else OK = TRUE; end end OK = FALSE; while OK == FALSE fprintf(1,'Input two integers M > 1 and N > 1 on separate lines.\n'); fprintf(1,'This implementation of Gaussian quadrature requires\n');

fprintf(1,'both to be less than or equal to 5.\n'); fprintf(1,'M is used for the outer integral and N for the inner\n'); fprintf(1,'integral.\n'); M = input(' '); N = input(' '); if M <=1| N <= 1 fprintf(1,'Integers must be greater than 1.\n'); else if M > 5 | N > 5 fprintf(1,'Integers must be less than or equal to 5.\n'); else OK = TRUE; end end end r = zeros(4,5); co = zeros (4,5); if OK == TRUE r(1,1) = 0.5773502692; r(1,2) = -r(1,1); co(1,1) = 1.0; co(1,2) = 1.0; r(2,1) = 0.7745966692; r(2,2) = 0.0; r(2,3) = -r(2,1); co(2,1) = 0.5555555556; co(2,2) = 0.8888888889; co(2,3) = co(2,1); r(3,1) = 0.8611363116; r(3,2) = 0.3399810436; r(3,3) = -r(3,2); r(3,4) = -r(3,1); co(3,1) = 0.3478548451; co(3,2) = 0.6521451549; co(3,3) = co(3,2); co(3,4) = co(3,1); r(4,1) = 0.9061798459; r(4,2) = 0.5384693101; r(4,3) = 0.0; r(4,4) = -r(4,2); r(4,5) = -r(4,1); r(4,1) = 0.2369268850; r(4,2) = 0.4786286705; r(4,3) = 0.5688888889; co(4,4) = co(4,2); co(4,5) = co(4,1); %STEP1 H1 = (B-A)/2; H2 = (B+A)/2; % use AJ in place of J

AJ = 0; %STEP 2 for I = 1:M %STEP 3 X = H1*r(M-1,I)+H2; JX = 0; C1 = C(X); D1 = D(X); K1 = (D1-C1)/2; K2 = (D1+C1)/2; %STEP 4 for J = 1:N Y = K1 * r(N-1,J)+K2; Q = F(X, Y); JX = JX + co(N-1,J)*Q; end %STEP 5 AJ = AJ+co(M-1,I)*K1*JX; end %STEP 6 AJ = AJ*H1; %STEP 7 fprintf(1,'\nThe double integral of F from %12.8f to %12.8f is\n', A, B); fprintf(1,' %.10e', AJ); fprintf(1,' obtained with M = %3d and N =%3d\n',M,N); end EJEMPLO N1 >> programa2 This is Gaussian Quadrature for double integrals. Input the function F(x,y) in ternas of x and y For example: sqrt(x^2+y^2) x^2 + y^2 Input the function C(x), and D(x) in terms of x on separate lines For example: cos(x) sin(x) cosx sinx Input lower limit of integration and upper limit of integration on separate lines 1 3 Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral.

2 7 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 3 8 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 3 9 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 4 9 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 5 6 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 7 8 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 4 7

Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 6 9 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 9 10 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 10 12 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 13 14 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 15 16 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 17 18 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines.

This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 19 20 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral.

The double integral of F from 1.00000000 to 3.00000000 is 0.0000000000e+000 obtained with M =3 EJEMPLO N2 >> programa2 This is Gaussian Quadrature for double integrals. Input the function F(x,y) in ternas of x and y For example: sqrt(x^2+y^2) cos(x) Input the function C(x), and D(x) in terms of x on separate lines For example: cos(x) sin(x) x Input lower limit of integration and upper limit of integration on separate lines 4 5 Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 2 6 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 38 39

Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral.

The double integral of F from 4.00000000 to 5.00000000 is 0.0000000000e+000 obtained with M = 5 >> EJEMPLO N3 >> programa2 This is Gaussian Quadrature for double integrals. Input the function F(x,y) in ternas of x and y For example: sqrt(x^2+y^2) x^3 Input the function C(x), and D(x) in terms of x on separate lines For example: cos(x) sin(x) x y Input lower limit of integration and upper limit of integration on separate lines 1 9 Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 2 6 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 3 9 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral.

The double integral of F from 1.00000000 to 9.00000000 is 0.0000000000e+000 obtained with M =3 EJEMPLO N4 >> programa2 This is Gaussian Quadrature for double integrals. Input the function F(x,y) in ternas of x and y For example: sqrt(x^2+y^2) x^2+y^2 Input the function C(x), and D(x) in terms of x on separate lines For example: cos(x) sin(x) cos(x) sin(x) Input lower limit of integration and upper limit of integration on separate lines 2 10 Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 3 8 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 4 12 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. The double integral of F from 2.00000000 to 10.00000000 is 0.0000000000e+000 obtained with M = EJEMPLO N5

>> programa2 This is Gaussian Quadrature for double integrals. Input the function F(x,y) in ternas of x and y For example: sqrt(x^2+y^2) 2*x Input the function C(x), and D(x) in terms of x on separate lines For example: cos(x) sin(x) 3 Input lower limit of integration and upper limit of integration on separate lines 2 7 Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 3 9 Integers must be less than or equal to 5. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral. 0.22 0.89 Integers must be greater than 1. Input two integers M > 1 and N > 1 on separate lines. This implementation of Gaussian quadrature requires both to be less than or equal to 5. M is used for the outer integral and N for the inner integral.

The double integral of F from 2.00000000 to 7.00000000 is 0.0000000000e+000 obtained with M = 3 >>

Potrebbero piacerti anche