Clear Filters
Clear Filters

How to plot graph regarding the definite integral by using Midpoint rule for the function 𝑦 =x^1/3?

10 views (last 30 days)
This is what I have done and I'm not sure is it correct.
I don't know how to continue to plot the graph of it.
y = @(x) nthroot(x,3); %Function to integrate
a = 0; b = 2; %Interval where a is the lower boundary and b is the upper boundary.
n = 10; %Number of subintervals
dx = (b-a/n); %Width of each rectangle
mpr = 0;
for i = a:dx:b
mpr = mpr + f(i+dx/2);
end
I = dx*mpr;
  4 Comments

Sign in to comment.

Accepted Answer

Hassaan
Hassaan on 6 Mar 2024
Edited: Hassaan on 6 Mar 2024
% 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 the function
plot(x_vals, y_vals, 'b-', 'LineWidth', 1.5);
hold on; % Hold on to the current plot
% 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, 'r', 'EdgeColor', 'r', 'FaceAlpha', 0.1);
end
% Formatting the plot
xlabel('x');
ylabel('y');
title('Midpoint Rule Approximation');
legend('y = nthroot(x, 3)', 'Midpoint rectangles');
hold off; % Release the plot hold
MATLAB Learning Material:
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!