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

10 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

Hassaan
Hassaan 2024-3-6
编辑:Hassaan 2024-3-6
% 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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by