Question
Write recursive adaptive gauss using MATLAB (2)adaptive_gauss(f,a,b, level, N, eps) Gauss.m function sum = gauss(f,a,b) % This function evaluates the Gaussian quadrature of order N.
Write recursive adaptive gauss using MATLAB
(2)adaptive_gauss(f,a,b, level, N, eps)
Gauss.m
function sum = gauss(f,a,b)
% This function evaluates the Gaussian quadrature of order N. N = 2; xw = [-0.57735026918963 1.00000000000000; 0.57735026918963 1.00000000000000]; % get quadrature points and weights y = 0.0; for j = 1:N x = a + 0.5*(b-a)*(1+xw(j,1)); y = y + f(x)*xw(j,2); end
sum = 0.5*(b-a)*y;
function sum = adaptive_gauss(f,a,b,level,N,delta)
one_gauss = gauss(f,a,b); c = 0.5 * (a + b); two_gauss = gauss(f,a,c) + gauss(f,c,b);
if level > N fprintf('Error: max level reached '); sum = two_gauss; return else if abs(one_gauss - two_gauss) The aim of this lab is to develop an adaptive two-point Gaussian quadrature. We will use recursion rather than iteration in Algorithm 4.3. Write 3 functions for this recursive method. 1) A basic two-point Gauss procedure gauss (f, a, b) to approximate the integralo f(x) dx. Refer to Eqs. 4.40 and 4.41 on the textbook. 2) A recursive function adaptive_gauss (f,a,b, level,N,eps) to approximate af(r) dx. The function divides the interval [a, b] in half, and calls the gauss procedure on the left sub-interval, the right subinterval, and the whole interval. Then, it checks if the maximum level of refinement (N) is exceeded. If so, the function prints an error message and returns. Otherwise, the error is approximated and checked against the tolerance (eps). If the error is smaller than eps, the function returns the approximated integral over the whole interval. Otherwise, it calls itself on the left and right subinterval, with increased level. See next page for a pseudocode of this function. For informational purposes, also print out each subinterval, its contribution to o f (x)dx, and the refinement level. Hint:consider where you should print this information, then use the fprintf command and the format below. A driving program (main.m) that initializes the variable level to zero, and calls adaptive_gauss. 3) Use your functions to approximate 5tanh 5(x 3) 0 with a tolerance of 10-5 and maximum 20 refinements. The integral is 2.0000, and your function should generate the outputs below [0.000000,1.250000] contribution-0.000000 level-2 [1.250000,1.875000] contribution-0.000001 level-3 [1.875000, 2.187500] contribution 0.000028 level=4 [2.187500,2.500000] contribution-0.000641 level-4 [2.500000,2.656250] contribution 0.002492 level-5 [2.656250,2.812500] contribution-0.011103 level-5 [2.812500,2.890625] contribution-0.014623 level-6 [2.890625, 2.968750] contribution-0.026016 level=6 2.968750,3.046875] contribution-0.040569 level-6 [3.046875, 3.125000] contribution=0.054719 level=6 [3.125000,3.281250] contribution-0.136889 level-5 [3.281250,3.437500] contribution-0.151669 level-5 [3.437500, 3.593750] contribution-0.155263 level-5 [3.593750,3.750000] contribution-0.156042 level- 5 [3.750000,4.375000] contribution-0.624946 level-3 [4.375000, 5.000000] contribution=0.625000 level=3 Adaptive Gaussian Quadrature (Recursive Implementation) adaptive gauss(f,a,b,level,N,s) INPUT OUTPUT endpoints a, b; the tolerance ; current level of refinement, level; max level N the approximation to the integral, sum STEP 1 STEP 2 s(f,a,b) one gauss gaus c = (a+b)/2 two gauss-gauss(f.a,c) + gauss(f.c,b) If level >N, then STEP 3 STEP 4 Print "Error: max level reached" sumtwo gauss Else If lone-gauss-two-gauss|N, then STEP 3 STEP 4 Print "Error: max level reached" sumtwo gauss Else If lone-gauss-two-gauss|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started