TASK: Estimate the area under the curve y=humps(x), for x between 0 and 1, using a FOR loop.
COMMENT: MATLAB has a built-in function called "humps()". You can evaluate it at a point x by the command:
y = humps ( x );
To estimate the area under this curve, for x between 0 and 1, we can simply imagine dividing the x range into n equal parts of width 1/n. This slices the graph up. Now we can try to approximate each slice by a rectangle.
In the interval [x,x+width], we could use a rectangle whose height is height=humps(x). The area of this rectangle is height*width. Adding up the area of each rectangle gives us an estimate of the total area.
As the number n of equal parts is increased, we can hope to get better approximations to the area.
INSTRUCTIONS:
Use the input() statement to get a value of n from the user. Initialze the variable "area_total" to zero. Initialize the variable "width" to 1/n. Initialize the variable x to 0. Start a for loop that carries out steps i = 1 to n. Set the height of the i-th rectangle by evaluating humps(x). height = ? The area of the i-th rectangle is width * height. Add this to "area_total". area_total = ? Get ready for the next interval by adding "width" to x. x = ? end your FOR loop fprintf ( ' Using N=%d subintervals, area estimate: %g\n', n, area_total );
CHECK: If you are doing things correctly, then using N=5 subintervals should give you an area estimate of about 25.
SUBMIT: Your work should be stored in a script file called "hw023.m". Your script file should begin with at least three comment lines:
% hw023.m % YOUR NAME % This script (describe what it does) % Add any comments here that you care to make.If this problem is part of an assignment, then submit it to Canvas.