Math 112: MATLAB Assignment 1

NUMERICAL INTEGRATION * Due Date: October 9, 2008

 

Contents

Sample Program

The sample program below uses the left-endpoint rule, the right-endpoint rule and the trapezoid rule to approximate the definite integral of the function.

Matlab comments follow the percent sign (%)

a= 0;
b= 1;
N = 10;
h=(b-a)/N;
x=[a:h:b];  %creates a vector of n+1 evenly spaced points
f=x.^2;
IL=0;
IR=0;
IT=0;
for k=1:N;  %Note that the vector f has (N+1) elements
    IL=IL+f(k);
    IR=IR+f(k+1);
    IT=IT+(f(k)+f(k+1))/2;
end;
IL=IL*h;
IR=IR*h;
IT=IT*h;

fprintf(' When N = %i, we find:\n',N);
fprintf(' Left-endpoint approximation = %f.\n',IL);
fprintf('Right-endpoint approximation = %f.\n',IR);
fprintf('   Trapezoidal approximation = %f.\n',IT);
% Output from this program:
 When N = 10, we find:
 Left-endpoint approximation = 0.285000.
Right-endpoint approximation = 0.385000.
   Trapezoidal approximation = 0.335000.

New Matab ideas

Parameters used in this program:

Your assignment

After running the above example, do the following to hand in: (Consult the Matlab TA if you have any questions.)

Approximate the integral of

Answer the following questions