definite integral by using Midpoint rule

What i have done is as below and how to code it as the diagram shown?
% Define the function to integrate using a function handle
f = @(x) nthroot(x,3);
% Define the interval and the number of subintervals
a = 0; b = 2; % Interval [a, b]
n = 10; % Number of subintervals
% Calculate the width of each subinterval
dx = (b - a) / n;
% Initialize the midpoint sum
mpr = 0;
% Calculate the sum for the Midpoint Rule
for i = 1:n
xi = a + (i-0.5)*dx; % Midpoint of the i-th subinterval
mpr = mpr + f(xi); % Add the value at the midpoint
end
% Calculate the integral approximation
I = dx * mpr;
% Display the result
fprintf('The approximate value of the integral is: %f\n', I);
The approximate value of the integral is: 1.896224
% Plotting the function and the rectangles
x_vals = linspace(a, b, 1000); % Generate 1000 points between a and b
y_vals = arrayfun(f, x_vals); % Evaluate the function at each x value
% Plot each rectangle
for i = 1:n
xi = a + (i-0.5)*dx; % Midpoint of the i-th subinterval
% Plot the rectangle
rect_x = [xi - dx/2, xi - dx/2, xi + dx/2, xi + dx/2];
rect_y = [0, f(xi), f(xi), 0];
patch(rect_x, rect_y, 'm', 'EdgeColor', 'k', 'LineWidth', 1);
end
hold on; % Hold on to the current plot
% Plot the function
plot(x_vals, y_vals, 'r-', 'LineWidth', 3);
% Formatting the plot
xlabel('x-axis');
ylabel('y-axis');
box off;
hold off; % Release the plot hold

5 件のコメント

Manikanta Aditya
Manikanta Aditya 2024 年 3 月 6 日
Hi,
Check this:
% Define the function to integrate using a function handle
f = @(x) nthroot(x, 3);
% Define the interval and the number of subintervals
a = 0; b = 2; % Interval [a, b]
n = 10; % Number of subintervals
% Calculate the width of each subinterval
dx = (b - a) / n;
% Initialize the midpoint sum
mpr = 0;
% Calculate the sum for the Midpoint Rule
for i = 1:n
xi = a + (i-0.5)*dx; % Midpoint of the i-th subinterval
mpr = mpr + f(xi); % Add the value at the midpoint
end
% Calculate the integral approximation
I = dx * mpr;
% Display the result
fprintf('The approximate value of the integral is: %f\n', I);
The approximate value of the integral is: 1.896224
% Plotting the function and the rectangles
figure; % Create a new figure
x_vals = linspace(a, b, 1000); % Generate 1000 points between a and b
y_vals = arrayfun(f, x_vals); % Evaluate the function at each x value
% Plot each rectangle
hold on; % Hold on to the current plot
for i = 1:n
xi = a + (i-0.5)*dx; % Midpoint of the i-th subinterval
% Plot the rectangle
rect_x = [xi - dx/2, xi - dx/2, xi + dx/2, xi + dx/2];
rect_y = [0, f(xi), f(xi), 0];
patch(rect_x, rect_y, 'm', 'EdgeColor', 'k', 'LineWidth', 1); % 'm' for magenta color
end
% Plot the function
plot(x_vals, y_vals, 'r-', 'LineWidth', 3); % 'r-' for a red solid line
% Formatting the plot
xlabel('x-axis');
ylabel('y-axis');
title('Midpoint Rule Approximation');
legend('Rectangles', 'Function', 'Location', 'NorthWest');
box on; % Show the box around the plot
grid on; % Show grid
hold off; % Release the plot hold
Thanks!
John D'Errico
John D'Errico 2024 年 3 月 6 日
編集済み: John D'Errico 2024 年 3 月 6 日
What are you asking here? There is no question.
So is your question asking if the code is correct? It seems reasonable at a glance. But you are the one who should test that. Does it yield the correct answer? I.e., does your answer approach the known value as the number of intervals grows large, so the interval length goes to zero? Surely you have verified that already? If not, why not?
Is your question one asking if the code is good? I'd give it a B at best. Lots of good, readable comments. An A at least for that.
Heavily looped where no loops are needed. You already understand how to evaluate your function at a list of points. But then you have a loop where you need to form a sum. Sigh.
You use arrayfun, which allows you to be sloppy in your definition of f, since you will not need to vectorize the function itself. That makes your code less efficient. Why write efficient code, when you can be sloppy?
Not a function, but a script. Learn to use functions. Small, modular functions. They will vastly improve the quality of your code.
In the end though, IF your code does what it needs to do, in a reasonable amount of time to solve the problem at hand, the code can be deemed correct. So you should verify it does what you need, then go on to the next problem and not worry about whether we approve of it.
Overall, not actively bad code at all. A decent effort. If it produces the result you need, then good. I'd not worry about how you can make it better, since your code will gradually improve anyway as you gain skill. Hand it in and go on to the next assignment.
L
L 2024 年 3 月 7 日
how to code for the vertical dotted line at the midpoint?
Sam Chak
Sam Chak 2024 年 3 月 7 日
Hi @JX, I believe that plotting the vertical dotted line isn't part of solving the mathematical integral problem.
Do you wish to patch the 'green' areas as well?
L
L 2024 年 3 月 8 日
yes

サインインしてコメントする。

回答 (1 件)

Sandeep Mishra
Sandeep Mishra 2024 年 9 月 17 日

0 投票

Hi L,
From the code snippet, I realized that you are working on plotting a curve with multiple rectangular plots and aim to fill the area between the curve and the rectangles with green colour, along with drawing vertical dotted lines at each midpoint.
Here’s a detailed approach for achieving both tasks
1. To achieve the patch, you can use the ‘fill’ function in MATLAB to create filled 2-D patches. Refer to the following code snippet that demonstrates how to apply the 'fill' function before adding the rectangular patches:
% Fill the area below the curve
fill([x_vals, fliplr(x_vals)], [y_vals, zeros(size(y_vals))], 'g');
% Plot each rectangle
2. The ‘plot’ function of MATLAB can be used to draw vertical dotted lines at each midpoint. Refer to the following code snippet that demonstrates how to use the 'plot' function to draw vertical dotted lines at each midpoint.
patch(rect_x, rect_y, 'm', 'EdgeColor', 'k', 'LineWidth', 1);
hold on
% Plotting vertical red dotted line at each midpoint
plot([xi, xi], [0, f(xi)], 'r--');
Please refer to the following MathWorks documentation for more information.
  1. ‘fill’ function: https://www.mathworks.com/help/releases/R2024a/matlab/ref/fill.html
  2. fliplr’ function: https://www.mathworks.com/help/releases/R2024a/matlab/ref/fliplr.html
I hope this helps.

カテゴリ

質問済み:

L
L
2024 年 3 月 6 日

回答済み:

2024 年 9 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by