riemann

advertisement
Developing a MATLAB function: riemann.m
First you need to think about what you are trying to do – most fundamentally, what are
the inputs and what are the outputs, and what other products (such as a plot) need to be
generated.
In this case our inputs will be:
1) a general function handle or name, called f here,
2) the endpoints a,b of the interval [a,b] on which the Riemann sum is to be
calculated
3) the number of intervals n in the partition (assumed to be uniform)
4) the selection method to be employed, that we call meth (left, right, or middle)
We want to output:
1) the Riemann sum, that we call s.
2) We also want to produce a figure, roughly drawn below, showing the graph of
the function and the rectangles whose areas sum to the Riemann sum.
We make a rough sketch of this information, including the formula for the Riemann sum.
Since we’ll have to plot a lot of rectangle, we review how to plot a simple rectangle.
Below we make an outline of how our function is to be structured, what is to be done in
what order. In this case we wish to generate the selection (the points at which f will be
evaluated), then evaluate the Riemann sum, then plot the rectangles and finally draw the
graph of f on top of the rectangles. We define variable names to be used internally by the
function (xs – the array of selected points, xp – the array of endpoints in the partition,
dx=(b-a)/n. We begin making our MATLAB function. As tasks are finished, we try out
our function to see if it is working correctly. In this case, once we have generated the
Riemann sum, we test out what we have done so far – it seems to work!
The key calculation of the Riemann sum is
s=sum(feval(f,xs))*dx
where we have used the MATLAB sum function for expedience. In other languages
where the sum function is not included, or if we want to do it without the sum function,
we would use a loop to calculate the sum
s=0;
for i=1:n
s=s+feval(f,xs(i));
end
s=s*dx; %we save the dx multiplication for last
Plotting the rectangles will also require a loop as we plot them one-by-one. The ith
rectangle is located above the interval [xp(i) xp(i+1)] where i=1,2,..,n . The height of the
rectangle is feval(f,xs(i)). With this information we can plot each rectangle and we finish
with the graph of f and a point plot of the points (xs(i),f(xs(i)).
General considerations:
When making your function “plan” you can include key MATLAB steps, such as
defining the variables, or calculating the Riemann sum in this case. A plan is usually
described as being “top-down” meaning that the problem is broken up into large pieces,
then each piece is broken up into smaller subtasks and so on.
Frequently test to make sure that what you are doing is working correctly. This can be
done after each small task is completed. If something doesn’t look right, have MATLAB
output intermediate calculations by removing semicolons and try to find out where things
are going wrong. When you use/write MATLAB statements you need to have a clear idea
of what each statement means and MATLAB is doing when it executes it.
Translating “math” into “matlab” takes some getting used to and is a matter of thought
and practice. For instance a “sum” in mathematics usually corresponds to several
programming statements – an initialized sum variable, e.g. s=0, and a loop through the
terms in the sum, adding each to s in turn. Of course in MATLAB the sum function is
introduced to simplify this process but most basically a sum is associated with a loop
structure in a programming language.
Download