21.2)
function
[L R T] = myints(f,a,b,n)
% Math 344
% Homework Problem 21.2
% Written by Astrom and Elbert
%
% Calculates the area under a function curve (integration)
% using the Left and Right endpoint rules and the Trapezoid Rule
%
% inputs f, a, b and n
% f is the function
% a is the start point of integration
% b is the end point of integration
% n is number of intervals
% The intervals have a start and end point which will
% require n+1 points to make n intervals
L = 0;
% Set L to begin integration at 0
R = 0;
% Set R to begin integration at 0
T = 0;
% Set T to begin integration at 0
delta = (ba)/n;
% Width of interval
x = linspace(a,b,n+1);
% Vector of n+1 points
y = f(x);
% Set function to variable y
for
i = 1:n
% Loop to calculate area
L=L+y(i)*delta;
% Calculate using Left point
R=R+y(i+1)*delta;
% Calculate using Right point
T=T+(y(i)+y(i+1))/2*delta;
% Calculate using Trapezoid
end
% End calculation loop
>> f = inline('sqrt(x)','x');
>> a = 1;
>> b = 2;
>> n = 4;
>> [L R T] = myints(f,a,b,n)
L =
1.166413628918445
R =
1.269967019511719
T =
1.218190324215082
>> n = 100;
>> [L R T] = myints(f,a,b,n)
L =
1.216879128301471
R =
1.221021263925201
T =
1.218950196113336
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
The percent error was calculated by adding a few extra lines of code on to the program.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 Staff
 Math, 21.2 %, Right endpoint rules, End calculation loop, 1.218951416497460 L, 004 L

Click to edit the document details