Using the program for the trapezoidal rule given in the text

Using the program for the trapezoidal rule given in the text, prepare a table of values of T_n(f) for n = 2, 4, 8, ..., 512 for the following integrals. Also find the errors and the ratios by which the errors decrease. integral_0^1 x^5/2 dx = 2/7

Solution

Following matlab code should give the integral values, errors for different n :

Use [a,b,c] = time() to get the required arrays

function [ integralValue, AbsoluteError, RelativeError ] = time()
    Ns = [2,4,8,16,32,64,128,256,512];
    integralValue = [];
    AbsoluteError = [];
    RelativeError = [];
    for index=1:size(Ns,2)
        N = Ns(1,index);
        a = 0;
        b = 1.0;
        h = (b - a)/N;
        answer = 0;
        xk = a;
        xkplus1 = a + h;
        for i=0:N-1
            answer = answer + ( xk^(2.5) + xkplus1^(2.5) );
            xk = xkplus1;
            xkplus1 = xkplus1 + h;
        end
        intgVal = answer*(h/2);
        integralValue = [ integralValue intgVal ];
        AbsoluteError = [ AbsoluteError abs( intgVal - 2/7 ) ];
        RelativeError = [ RelativeError abs( intgVal - 2/7 )*3.5 ];
    end
end

 Using the program for the trapezoidal rule given in the text, prepare a table of values of T_n(f) for n = 2, 4, 8, ..., 512 for the following integrals. Also f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site