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

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

Compare the exact & approximated ones using Midpoint rule
f = @(x) nthroot(x,3); %Function to integrate ...assign to correct variable
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
mpr = 0;
x = linspace(a,b,100);
y = f(x);
plot(x,y,'linewidth',2)
hold on
L = a:dx:b;
for i = 2:numel(L)-1
mpr = mpr + f((L(i)+L(i-1))/2);
plot((L(1) + (i-0.5)*dx), f((L(i)+L(i-1))/2),'r+','linewidth',2)
end
I = dx*mpr
I = 1.6485
legend('Exact: y = x^{1/3}','Approx (Midpoint)','location','best')
grid
Should be
plot((L(1) + (i-1-0.5)*dx), f((L(i)+L(i-1))/2),'r+','linewidth',2)
instead of
plot((L(1) + (i-0.5)*dx), f((L(i)+L(i-1))/2),'r+','linewidth',2)
These are the evaluation points for the Midpoint rule. I don't know what you mean by Approx(Midpoint) in the legend. These are not approximations for an integral.
How to code for the straight line for the midpoint?

Sign in to comment.

 Accepted Answer

% 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

Asked:

L
L
on 6 Mar 2024

Commented:

L
L
on 7 Mar 2024

Community Treasure Hunt

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

Start Hunting!