Estimate area under a curve using finite approximations (lower sum with 4 rectangles of equal length)

54 views (last 30 days)
Hi, I'm having trouble calculating area under a curve using lower sum with 4 rectangles, specifically the for loop. This is my code:
f= @ (x) x.^2; x1=0; x2=1; n=4;
value=0;
dx=(x2-x1)/n; % width of rectangle
for k=0:n
c=x1+k*dx;
value=value+f(c);
end
value=dx*value
Gives answer as 0.4688, but this is how the calculations should be:
A = 0*0.25 + 0.0625*0.25 + 0.25*0.25 + 0.5625*0.25 = 0.21875 % approximately
Where 1st term: dx * f(0), 2nd term: dx * f(0.25), 3rd term: dx * f(0.5), 4th term: dx * f(0.75). Thanks

Accepted Answer

Umar
Umar on 12 Jun 2025 at 5:31

Hi @rezheen, To accurately calculate the area under the curve using the lower sum method with 4 rectangles, you need to ensure that you are evaluating the function at the left endpoints of each subinterval. The provided code incorrectly sums the function values at the right endpoints, which leads to an overestimation of the area.

Key Corrections

Loop Indexing: The loop should iterate from 0 to ( n-1 ) instead of 0 to ( n ) to ensure that we are using the left endpoints.

Function Evaluation: you need to evaluate the function at the left endpoints of each rectangle, which are ( x1 + k \cdot dx ) for ( k = 0, 1, 2, 3 ).

Area Calculation: The area of each rectangle is calculated as height times width where the height is the function value at the left endpoint.

Here is the corrected MATLAB code that implements the above logic and includes a plot of the rectangles:

% Define the function
f = @(x) x.^2; 
% Define the interval and number of rectangles
x1 = 0; 
x2 = 1; 
n = 4; 
% Initialize the area value
value = 0; 
% Calculate the width of each rectangle
dx = (x2 - x1) / n; 
% Loop to calculate the lower sum
for k = 0:n-1
  c = x1 + k * dx;  % Left endpoint
  value = value + f(c);  % Sum the function values at left endpoints
end
% Calculate the total area
value = dx * value;
% Display the result
disp(['Calculated area under the curve: ', num2str(value)]);
% Plotting the function and rectangles
x = linspace(x1, x2, 100); % Points for plotting the function
y = f(x); % Function values
% Create the figure
figure;
hold on;
plot(x, y, 'b', 'LineWidth', 2); % Plot the function
title('Area Under the Curve using Lower Sum');
xlabel('x');
ylabel('f(x) = x^2');
% Plot rectangles
for k = 0:n-1
  x_left = x1 + k * dx; % Left x-coordinate
  height = f(x_left); % Height of the rectangle
  rectangle('Position', [x_left, 0, dx, height], 'FaceColor', 'r', 'EdgeColor', 'k', 
 'LineWidth', 1.5);
end
hold off;
grid on;
legend('f(x) = x^2', 'Rectangles');

Function Definition: The function ( f(x) = x^2 ) is defined using an anonymous function.

Interval and Rectangles: The interval from 0 to 1 is divided into 4 rectangles.

Loop for Lower Sum: The loop iterates from 0 to ( n-1 ), calculating the left endpoint for each rectangle and summing the function values.

Area Calculation: The total area is computed by multiplying the sum of the function values by the width of the rectangles.

Plotting: The function is plotted along with the rectangles to visually represent the area under the curve.

The expected output for the area should now align closely with the calculated value of approximately 0.21875.

Please see attached.

I do agree with @ImageAnalyst comments.

More Answers (2)

Matt J
Matt J on 11 Jun 2025 at 17:01
f= @ (x) x.^2; x1=0; x2=1; n=4;
value=0;
dx=(x2-x1)/n; % width of rectangle
for k=0:n-1
c=x1+k*dx;
value=value+f(c);
end
value=dx*value
value = 0.2188

Image Analyst
Image Analyst on 12 Jun 2025 at 1:00
Try this:
f= @ (x) x.^2;
x1=0;
x2=1;
n=4;
totalArea=0;
dx=(x2-x1)/n; % width of rectangle
x = linspace(x1, x2, 500);
y = f(x);
plot(x, y, 'b-', 'LineWidth', 2)
grid on;
hold on;
xline(x1, 'Color', 'r');
xk = x1 + dx * [0 : n]
xk = 1×5
0 0.2500 0.5000 0.7500 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for k = 2 : length(xk)
fprintf('Processing area between %f and %f.\n', xk(k-1), xk(k))
minimumY = min(f(xk(k)) , f(xk(k-1)))
% Draw vertical lines.
line([xk(k), xk(k)], [0, f(xk(k))], 'Color', 'r', 'LineWidth', 2);
thisRectangleArea = minimumY * dx;
totalArea = totalArea + thisRectangleArea;
fprintf('Total area of boxes so far = %.5f', totalArea)
% Draw top of box.
line([xk(k-1), xk(k)], [minimumY, minimumY], 'Color', 'r', 'LineWidth', 2);
end
Processing area between 0.000000 and 0.250000.
minimumY = 0
Total area of boxes so far = 0.00000
Processing area between 0.250000 and 0.500000.
minimumY = 0.0625
Total area of boxes so far = 0.01562
Processing area between 0.500000 and 0.750000.
minimumY = 0.2500
Total area of boxes so far = 0.07812
Processing area between 0.750000 and 1.000000.
minimumY = 0.5625
Total area of boxes so far = 0.21875
  2 Comments
Image Analyst
Image Analyst on 12 Jun 2025 at 15:56
Edited: Image Analyst on 12 Jun 2025 at 16:02
Two things I always tell people are to put in lots of comments and use descriptive variable names. Normally I would have put in lots more comments that I did in this answer. And I couldn't follow what you did immediately, though I could see something was wrong, in part because you used short cryptic variable names. I encourage everyone to use longer, more descriptive variable names so that others can understand what is being done.
Another thing to note is that the other solutions rely on the function being monotonitcally increasing (which it happens to be in this case), while mine is a more robust and general solution that looks for the minimum y value at either end of the region rather than assuming it will always be at the leftmost x value. It could be made even more general by inspecting everywhere in the region for the lowest y value rather than assuming it will always be at one of the endpoints.

Sign in to comment.

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!