From calculus class, we know that the integral of a function is associated with the area under the graph of the function. The area under the curve can be approximated by summing the area of rectangular \"strips\" whose heights are determined by the function values at equally spaced points along the abscissa (x-axis). The rectangles can be constructed in a number of ways. One way is to ensure that the top-left corner of each rectangle lies on the graph of the function (assuming the function has a positive value). Alternatively, the top-right corner can be used, as can the midpoint of the top line segment of the rectangle. Figure 1 below shows the cases where we have top-left (solid red lines) and top-right (dashed blue lines) alignment, using 5 rectangles to approximate the definite integral of the function y = x2 over the interval from 0 to 1 We also learned that one method generally produces an over-estimate of the integral value, and the other produces an under-estimate. As the rectangular strips are made progressively thinner, the number of rectangular strips increases, and each sum approaches the true value of the area under the curve. In the limit, the strips achieve a width of zero, and the discrete summation transforms to the definite integral Assignment You must write a MATLAB® function that accepts as an input the number of rectangular strips (N) that should be used to approximate the definite integral x2dx
clc;
close all;
clear all;
n=input(\'input number of rectangles \');
if n<=0;
n=100;
end
Area_top_left = 0;
Area_top_right = 0;
for i=1:n;
x(i)=(i/n);
y(i)=(x(i))^2;
end
Area_true = 1/3;
for i=2:1:n;
Area_top_left = Area_top_left + y(i).*(x(i)-x(i-1));
end
for i=1:1:n-1;
Area_top_right =Area_top_right + y(i).*(x(i+1)-x(i));
end
display(\'error b/w top left and true integral\');
display( (Area_top_left-Area_true) );
display(\'percentage error b/w top left and true integral\');
display( (Area_top_left-Area_true)*100/Area_true );
display(\'error b/w top right and true integral\');
display( (Area_top_right-Area_true) );
display(\'error b/w top right and true integral\');
display( (Area_top_right-Area_true)*100/Area_true );
display(\'number of n used\');
display( n );
plot(x,y);