Docsity

Adaptive Quadrature Algorithm-Numerical Analysis-MATLAB Code, Exercises of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
% ADAPTIVE QUADRATURE ALGORITM 4.3 %
% To approximate 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: approximation 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 Adaptive 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 maximum 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;
docsity.com
pf3

Partial preview of the text

Download Adaptive Quadrature Algorithm-Numerical Analysis-MATLAB Code and more Exercises Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

% ADAPTIVE QUADRATURE ALGORITM 4.3 %

% To approximate 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: approximation 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 Adaptive 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 maximum 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,7); TOL(I) = 10EPS; 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; % Approximation from Simpson's method for entire interval. S(I) = H(I)(FA(I)+4FC(I)+FB(I))/3; L(I) = 1; % STEP 2 while I > 0 & OK == TRUE % STEP 3 FD = F((A(I)+0.5H(I))); CNT = CNT+1; FE = F((A(I)+1.5H(I))); CNT = CNT+1; % Approximations from Simpson's method for halves of intervals S1 = H(I)(FA(I)+4FD+FC(I))/6; S2 = H(I)(FC(I)+4FE+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