Question

In: Advanced Math

These instructions are written with the assumption that code will be done in matlab. You might...

These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max.

2. Numerical Integration (Quadrature). Write FOUR of your own numerical integration routines. One should use left-end, right-end, or mid-points, another should use the trapezoid method, another should use Simpson’s method, and the fourth should use either Guassian Quadrature or Romberg’s method. Use your four routines to approximate two integrals numerically so that they are accurate to 1 part in 1 billion (if possible, if this is not possible explain why). First use your routines to approximate f(x) = x 3 − x cos(x) on the interval [0, 2π], you can and should check that your answer is correct. Then use your routines to approximate f(x) = 4e −2x 2 on the interval [−1, 1]. Use one of matlab’s built in integration functions to approximate both integrals also. Each integral should be: numerically approximated FOUR ways, checked vs one of matlab’s built-in numerical integration methods, and the polynomial/trig function should be checked exactly.

Solutions

Expert Solution


%%Matlab code for finding integration using 4 different method
clear all
close all
%functions for which integration have to do
fun1=@(x) x.^3-x.*cos(x) ;
fun2=@(x) 4.*exp(-2.*x.^2) ;
%Number of steps in each methods
N=100;
%Limit of integration for function 1.
a1=0; b1=2*pi;
%Limit of integration for function 2.
a2=-1; b2=1;

fprintf('\n\nFor 1st function f(x)= ')
disp(fun1)
%Exact integration for function 1
I_ext = integral(fun1,a1,b1);
fprintf('\nExact integration value for a=%f to b=%f is %f.\n',a1,b1,I_ext)

%Left Riemann integration for function 1
I_rmn=left_riemann(fun1,a1,b1,N);
fprintf('\n\tLeft Riemann integration for function 1 for N=%d is %f.\n',N,I_rmn)
fprintf('Error in Left Riemann integration is %e.\n',abs(I_ext-I_rmn))

%Trapizoidal integration for function 1
I_trp=trapizoidal(fun1,a1,b1,N);
fprintf('\n\tTrapizoidal integration for function 1 for N=%d is %f.\n',N,I_trp)
fprintf('Error in Trapizoidal integration is %e.\n',abs(I_ext-I_trp))

%Simpson integration for function 1
I_smp=simpson(fun1,a1,b1,N);
fprintf('\n\tSimpson integration for function 1 for N=%d is %f.\n',N,I_smp)
fprintf('Error in Simpson integration is %e.\n',abs(I_ext-I_smp))

%Romberg integration for function 1
I_rom=romberg(fun1,a1,b1,10);
fprintf('\n\tRomberg integration for function 1 for N=%d is %f.\n',10,I_rom)
fprintf('Error in Romberg integration is %e.\n',abs(I_ext-I_rom))

fprintf('\n\nFor 2nd function f(x)= ')
disp(fun2)
%Exact integration for function 2
I_ext = integral(fun2,a2,b2);
fprintf('\nExact integration value for a=%f to b=%f is %f.\n',a2,b2,I_ext)

%Left Riemann integration for function 2
I_rmn=left_riemann(fun2,a2,b2,N);
fprintf('\n\tLeft Riemann integration for function 2 for N=%d is %f.\n',N,I_rmn)
fprintf('Error in Left Riemann integration is %e.\n',abs(I_ext-I_rmn))

%Trapizoidal integration for function 2
I_trp=trapizoidal(fun2,a2,b2,N);
fprintf('\n\tTrapizoidal integration for function 2 for N=%d is %f.\n',N,I_trp)
fprintf('Error in Trapizoidal integration is %e.\n',abs(I_ext-I_trp))

%Simpson integration for function 2
I_smp=simpson(fun2,a2,b2,N);
fprintf('\n\tSimpson integration for function 2 for N=%d is %f.\n',N,I_smp)
fprintf('Error in Simpson integration is %e.\n',abs(I_ext-I_smp))

%Romberg integration for function 2
I_rom=romberg(fun2,a2,b2,10);
fprintf('\n\tRomberg integration for function 2 for N=%d is %f.\n',10,I_rom)
fprintf('Error in Romberg integration is %e.\n',abs(I_ext-I_rom))

%%Matlab function for Left Riemann integration
function val=left_riemann(func,a,b,N)
    % func is the function for integration
    % a is the lower limit of integration
    % b is the upper limit of integration
    % N number of rectangles to be used
    val=0;
    %splits interval a to b into N+1 subintervals
    xx=linspace(a,b,N+1);
    dx=xx(2)-xx(1); %x interval
    %loop for Riemann integration
        for i=1:length(xx)-1
            xx1=xx(i);
            val=val+dx*double(func(xx1));
        end
end

%%Matlab function for Trapizoidal integration
function val=trapizoidal(func,a,b,N)
    % func is the function for integration
    % a is the lower limit of integration
    % b is the upper limit of integration
    % N number of rectangles to be used
    val=0;
    %splits interval a to b into N+1 subintervals
    xx=linspace(a,b,N+1);
    dx=xx(2)-xx(1); %x interval
    %loop for Riemann integration
        for i=2:length(xx)-1
            xx1=xx(i);
            val=val+dx*double(func(xx1));
        end
       val=val+dx*(0.5*double(func(xx(1)))+0.5*double(func(xx(end))));
end

%%Matlab function for Simpson integration
function val=simpson(func,a,b,N)
    % func is the function for integration
    % a is the lower limit of integration
    % b is the upper limit of integration
    % N number of rectangles to be used
    %splits interval a to b into N+1 subintervals
    xx=linspace(a,b,N+1);
    dx=xx(2)-xx(1); %x interval
    val=(dx/3)*(double(func(xx(1)))+double(func(xx(end))));
    %loop for Riemann integration
        for i=2:length(xx)-1
            xx1=xx(i);
            if mod(i,2)==0
                val=val+(dx/3)*4*double(func(xx1));
            else
                val=val+(dx/3)*2*double(func(xx1));
              
            end
        end    
end

%%Matlab function for Romberg integration
function val = romberg(func,a,b,N)
% f - Matlab inline function
% a,b - integration interval
% n - number of rows in Romberg tableau
% toll- tollerence of 2 succesive romberg value
% Output:
% r - Romberg tableau containing the computed values of the integral
% h - step length
% n - size of Romberg table
        h = (b - a) ./ (2.^(0:N-1));
        r(1,1) = (b - a) * (func(a) + func(b)) / 2;
        for j = 2:N
            subtotal = 0;
            for i = 1:2^(j-2)
                subtotal = subtotal + func(a + (2 * i - 1) * h(j));
            end
            r(j,1) = r(j-1,1) / 2 + h(j) * subtotal;
            for k = 2:j
                r(j,k) = (4^(k-1) * r(j,k-1) - r(j-1,k-1)) / (4^(k-1) - 1);
            end
        end
        val=r(end,end);
end


%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%


Related Solutions

These instructions are written with the assumption that code will be done in matlab. You might...
These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max. 3. Root finding; Bisection, Secant, Newton. a) Write your own version of the bisection method to solve e 2x − 10x = 0. Assume the root is in the interval [0, 1]. Try to use the matlab command ”fzero” to accomplish the same thing....
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
The following code must be written using matlab How to get the elements that are different...
The following code must be written using matlab How to get the elements that are different in two set without using the setdiff function in matlbab?
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
Written Analysis Instructions As part your course requirements you are required to complete a Written Analysis...
Written Analysis Instructions As part your course requirements you are required to complete a Written Analysis of Data. The Analysis, 1 ½ to 2 pages in length (typed and double spaced), is worth 300 points. The maximum number of points you can obtain from the written assignment is 300. For this assignment you will be using be using both your creative writing and your analytical skills. Below you will find two sets of data set forth in two different tables....
Matlab code for Gauss Siedel with solved example in Matlab
Matlab code for Gauss Siedel with solved example in Matlab
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT