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.