Question

In: Advanced Math

1. Approximate the integral, exp(x), from 0 to 1, using the composite midpoint rule, composite trapezoid...

1. Approximate the integral,
exp(x), from 0 to 1,
using the composite midpoint rule, composite trapezoid rule, and composite Simpson’s method. Each method
should involve exactly n =( 2^k) + 1 integrand evaluations, k = 1 : 20. On the same plot, graph the absolute error
as a function of n.

Solutions

Expert Solution


%Matlab code for finding integration using different method
clear all
close all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%function for which integration have to do
f1=@(x) exp(x);
fprintf('\tfunction for which integration have to do\n')
disp(f1)

%upper and lower limit
a=0; b=1;
fprintf('\tupper and lower limit of integration a=%f, b=%f.\n',a,b)

%exact integration value for this limit
ext_int=f1(1)-f1(0);
fprintf('\texact integration value for this limit is %f.\n',ext_int)

%all intervals
k=1:20;

%loop fpr all k
for i=1:length(k)
    n(i)=2^i;
    %finding integration using different method
    val_simp=Simpson_int(f1,a,b,n(i));
    val_trap=Trapizoidal_int(f1,a,b,n(i));
    val_mid=Midpoint_int(f1,a,b,n(i));
  
    %error in all method
    err_sim(i)=abs(ext_int-val_simp);
    err_tra(i)=abs(ext_int-val_trap);
    err_mid(i)=abs(ext_int-val_mid);

end

%loglog plot of error vs n
figure(1)
loglog(n,err_sim)
hold on
loglog(n,err_tra)
loglog(n,err_mid)
title('Loglog plot of n vs. error for y=exp(x)')
xlabel('Interval n')
ylabel('Error')
legend('Simpson','Trapizoidal','Mid point')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Matlab function for Simpson Method
function val=Simpson_int(f,a,b,n)
%f=function for which integration have to do
%a=upper limit of integration
%b=lower limit of integration
%n=number of subintervals

    dx=(b-a)/n;     %interval length
    zs=f(a)+f(b);   %simpson integration
    %all x values for given subinterval
    xx=a:dx:b;
    %Simpson Algorithm for n equally spaced interval
    for i=2:n
        if mod(i,2)==0
            zs=zs+4*f(xx(i));
        else
            zs=zs+2*f(xx(i));
        end
    end
    %result using Simpson rule
    val=double((dx/3)*zs);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Matlab function for Trapizoidal Method
function val=Trapizoidal_int(f,a,b,n)
    % f is the function for integration
    % a is the lower limit of integration
    % b is the upper limit of integration
    % n is the number of trapizoidal interval in [a,b]
    dx=(b-a)/n; %x interval
    val=0;
    %splits interval a to b into n+1 subintervals
    xx=linspace(a,b,n+1);
    %loop for trapizoidal integration
        for i=2:n
            val=val+2*double(f(xx(i)));
        end
        %Final integration value val for limit a to b
        val=(dx/2)*(val+f(a)+f(b));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Matlab function for midpoint Method
function val=Midpoint_int(f,a,b,n)
    % f is the function for integration
    % a is the lower limit of integration
    % b is the upper limit of integration
    % n is the number of trapizoidal interval in [a,b]
    dx=(b-a)/n; %x interval
    val=0;
    %splits interval a to b into n+1 subintervals
    x=linspace(a,b,n+1);
    %loop for trapizoidal integration
        for i=1:n
            x_mid(i)=(x(i+1)+x(i))/2;
            val=val+double(f(x_mid(i)));
        end
    %result using midpoint integration method
    val=dx*val;
end
  
%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%


Related Solutions

Use the trapezoid rule, midpoint rule, and Simpson’s rule to approximate the given integrals with the...
Use the trapezoid rule, midpoint rule, and Simpson’s rule to approximate the given integrals with the given values of n. ?) ∫ ? ? / 1+? 2 ?? (from 0 to 2)   ? = 10 ?) ∫ √??? ?? (from 1 to 4) ? = 6
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with...
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) 2 1 6 ln(x) 1 + x dx, n = 10
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with...
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) π/2 0 3 2 + cos(x) dx,    n = 4 (a) the Trapezoidal Rule (b) the Midpoint Rule (c) Simpson's Rule
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with...
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) 4 0 ln(3 + ex) dx,    n = 8 (a) the Trapezoidal Rule (b) the Midpoint Rule (c) Simpson's Rule
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with...
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) π/2 0 3 1 + cos(x) dx,    n = 4
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with...
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) 5 2 cos(7x) x dx, n = 8 1 (a) the Trapezoidal Rule (b) the Midpoint Rule (c) Simpson's Rule
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with...
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) 2 0 e^x/ 1 + x^2 dx, n = 10 (a) the Trapezoidal Rule (b) the Midpoint Rule (c) Simpson's Rule
Evaluate the following integral using the Trapezoid Rule with n = 4 where a = 2....
Evaluate the following integral using the Trapezoid Rule with n = 4 where a = 2. 2 ∫ rootsquare (x^3 + a) dx 0 Round your answer to 1 decimal place.
Evaluate the following integral using the Midpoint Rule​ M(n), the Trapezoidal Rule​ T(n), and​ Simpson's Rule​...
Evaluate the following integral using the Midpoint Rule​ M(n), the Trapezoidal Rule​ T(n), and​ Simpson's Rule​ S(n) using nequals4. Integral from 2 to 6 StartFraction dx Over x cubed plus x plus 1 EndFraction Using the Midpoint​ Rule, ​M(4)equals
use the a) midpoint rule, b) Trapezoidal rule, and c) the Simpsons rule to approximate the...
use the a) midpoint rule, b) Trapezoidal rule, and c) the Simpsons rule to approximate the given integral with the value of n and round to 4 decimal places integral (from 0 to 1) e^-x^2 dx, n = 10 show work please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT