Home / Expert Answers / Calculus / writing-an-adaptive-integration-routine-adaptivesum-m-matlab-the-second-m-file-you-will-turn-in-w-pa588

(Solved): Writing an Adaptive Integration Routine adaptivesum.m (MATLAB) The second m-file you will turn in w ...



Writing an Adaptive Integration Routine adaptivesum.m (MATLAB) The second m-file you will turn in will be a recursive function. For this function, you will begin by evaluating the definite integral on , ?then splitting this interval in two and checking the area for each half interval. Call riemannsum.m to determine the areas, and if the difference between the area of the entire interval and the sum of the areas for the two half intervals are within tol, you stop and return the first area. If not, then each half interval is itself split in half and the evaluation occurs independently in each half interval. The process repeats until the condition is satisfied or a maximum number of recursions have been completed. Write adaptivesum.m with the declaration function area = ?adaptivesum(f,a,b,n,method,tol,c,varargin) where all of the input variables have the same meaning as in riemannsum.m except for n. ?The variable n in this program will be the maximum recursion depth. This is the total number of times the intervals are allowed to be halved. Keep in mind that this number allows for a maximum of iterations, ?so n should stay pretty small (7 ?or 8 ?at most). Be sure to check your function against carefully chosen examples which should require minimal or no recursion on particular subintervals. For example, a function like should only require two partitions for Simpson's rule if integrated over . Existing riemannsum.m function: function area = ?riemannsum(f,a,b,n,method) %RIEMANNSUM Numerical integration of a single variable function % ?function RIEMANNSUM integrates a function f on the interval from a to b % ?over n subintervals using one of several methods: % ?Left Riemann Sum {'l','left'} % ?Right Riemann Sum {'r','right'} % ?Midpoint Riemann Sum {'m','mid','middle','midpoint'} % ?Pseudorandom Riemann Sum {'p','pseudo','pseudorandom','random'} % ?Trapezoid Rule {'t','trapezoid'} % ?Simpson's Rule {'s','simpson','simpsons'} % % ?Syntax: % ?area = ?riemannsum(f,a,b,n,'p') % ?area = ?riemannsum(f,a,b,n,'pseudo') % ?area = ?riemannsum(f,a,b,n) % % ?Input: % ?f - ?function to be integrated % ?a - ?left endpoint % ?b - ?right endpoint % ?n - ?number of subintervals % --Optional-- % ?method - ?choice of integration scheme (see above), ?default 'p' % % ?Output: % ?area - ?result of definite integral % % ?Other m-files required: graphriemann.m % ?Subfunctions: none % ?MAT-files required: none % ?set default method if nargin < 5 method = 'p'; end % ?check n value if ~isnumeric(n) || ?mod(n,1)~=0 || ?n < 1 error('n must be a positive integer') end % ?define x (partition points) ?and deltax (interval width) x = ?linspace(a,b,n+1); % ?needs to be n+1 ?for n intervals deltax = (b-a)/n; % ?adjust x based on scheme (select sample points) switch lower(method) case {'l','left'} x = ?x(1:end-1); case {'r','right'} x = ?x(2:end); case {'m','mid','middle','midpoint'} x = ?x(1:end-1) + 0.5*deltax; %x = (x(1:end-1) + ?x(2:end))/2; case {'p','pseudo','pseudorandom','random'} x = ?x(1:end-1) + ?rand(1,n)*deltax; case {'t', ?'trapezoid'} % ?x modfication is not needed for trapezoid case case {'s','simpson','simpsons'} % ?x modification is not needed for Simpson's case end % ?compute area as a weighted sum switch lower(method) case {'l', ?'left', 'r', ?'right', 'm', ?'mid', 'middle', 'midpoint',... 'p', ?'pseudo', 'pseudorandom', 'random'} area = ?sum(f(x))*deltax; case {'t','trapezoid'} % ?Trapezoid Rule fx = ?f(x); % ?evaluate function at all points area = (deltax/2)*(fx(1) + 2*sum(fx(2:end-1)) + ?fx(end)); case {'s','simpson','simpsons'} % ?Simpson's Rule (n must be an even ineteger) if mod(n,2) ?~= 0 error('Simpson''s Rule requires an even number of intervals (n must be even).') end fx = ?f(x); % ?evaluate function at all points area = (deltax/3)*(fx(1) + 4*sum(fx(2:2:end-1)) + 2*sum(fx(3:2:end-2))... + ?fx(end)); otherwise error('Unkown method'); end



We have an Answer from Expert

View Expert Answer

Expert Answer


We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe