function rsums(f)
%RSUMS  Interactive evaluation of Riemann sums.
%   RSUMS(f) approximates the integral of f by Riemann sums.
%   f is a string representing a function of exactly one variable.
%   RSUMS is usually called with the command line form, eg.
%      rsums exp(-5*x^2)

%   Copyright (c) 1993-1999 The Mathworks, Inc. All Rights Reserved.
%   $Revision: 1.11 $  $Date: 1999/01/04 21:54:59 $

clf reset
plotedit on;
axes('pos',[.18 .16 .77 .70])
ind_var = findsym(sym(f));
eflag = find(ind_var==',');
if ~isempty(eflag) & any(find(ind_var==',')) 
   error('Function must contain exactly one variable.'),
end;
f = strrep(f,ind_var,'x');
n = 10;
rsummer(f,n)
uicontrol('units','normal','style','slider','pos',[.18 .03 .70 .03],...
   'min',2,'max',256,'value',n,'userdata',f,...
   'callback',['s = get(gcf,''child''); s=s(1); f=get(s,''userdata''); '...
   'n = round(get(s,''value'')); feval(''rsums/rsummer'',f,n)']);


function rsummer(f,n)
%RSUMMER Evaluate and display Riemann sum.
%   RSUMMER('expr',n) displays a graph of the Riemann sum
%   for 'expr' using n points on [0,1].

x = (1:n)/n;
y = eval(vectorize(f));
r = sum(y)/n;
bar(x,y)
title([f '  :  ' sprintf('%9.6f',r)])
xlabel(int2str(n))
axis([0 1 min(0,min(y)) max(0,max(y))])


function F = vectorize(F)
%VECTORIZE Vectorize a Maple string expression.
%   VECTORIZE(F) inserts a '.' before any '^', '*' and '/' in F.
%   Called by EZPLOT.

l = length(F);
for k = fliplr(find((F=='^') | (F=='*') | (F=='/')))
   F = [F(1:k-1) '.' F(k:l)];
   l = l+1;
end
