function [TRsum,Error,M1]=RiemannSums
%Computes and graphs the upper and lower Riemann Sums
%f: function passed with @ (e.g. "@(x) x.^2") or inline
%a,b are interval bounds
%N: number of subintervals

%Input section
g=input('Enter a function as a function of x (no equals sign or quotes):  ','s');
f=inline(g,'x');
a=input('Enter left bound:  ');
b=input('Enter Right bound:  ');
display=input('Do you want to display graphs (y/n)?  ','s');
movie=input('Do you want to record a movie of graphs (y/n)?  ','s');
if display~='y'|'Y'
    display='y';
end

repeat='y';

while repeat=='y'
    N=input('Enter number of subdivisions:  ');
    K=1;
    cla; %clear the plot 
    %compute the step size.
    h=abs(b-a)/N;
    %compute partition of [a,b] and f(x(i))
    x=linspace(a,b,N+1);
    for i=0:N
        y(i+1)=f(x(i+1));
    end

    %compute upper and lower Riemann values 
    for i=1:N
        if y(i)>=y(i+1)
            UR(i)=y(i);
            LR(i)=y(i+1);
        else
            UR(i)=y(i+1);
            LR(i)=y(i);
        end
    end
    %Create vectors for graph f(x)
    X=linspace(a,b,100);
    for i=1:100
        Y(i)=f(X(i));
    end
    
    %plot Lower Riemann Rectangles
    hold on
    if display=='y'|'Y'
        title('Lower Riemann Sum')
        plot(X,Y)
        axis auto;
        if movie=='y'
            for j=1:10
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
    end
    Area=0;
    for i=1:N
        if display=='y'|'Y'
            rectX=[x(i) x(i) x(i+1) x(i+1) x(i)];
            rectY=[0 LR(i) LR(i) 0 0];
            plot(rectX,rectY)
            if movie=='y'
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
        Area=Area+h*LR(i);
    end;
    LRsum=Area;
    disp('Lower Riemann sum =')
    disp(LRsum)
        if movie=='y'
            for j=1:10
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
    disp('Press any key to continue')
    pause;
    %K=1;

    %plot and calulate Upper Riemann Sum
    cla;
    hold on
    if display=='y'|'Y'
        title('Upper Riemann Sum')
        plot(X,Y)
        if movie=='y'
            for j=1:3
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
    end
    Area=0;
    for i=1:N
        if display=='y'|'Y'
            rectX=[x(i) x(i) x(i+1) x(i+1) x(i)];
            rectY=[0 UR(i) UR(i) 0 0];
            plot(rectX,rectY) 
            if movie=='y'
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
        Area=Area+h*UR(i);
    end;
    URsum=Area;
    disp('Upper Riemann sum =')
    disp(URsum)
        if movie=='y'
            for j=1:10
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
    disp('Press any key to continue')
    pause;
    
    %plot and calculate integral with the trapezoid method
    cla;
    hold on
    if display=='y'|'Y'
        title('Trapezoidal Method')
        plot(X,Y)
        if movie=='y'
            for j=1:3
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
    end
    Area=0;
    for i=1:N
        if display=='y'|'Y'
            Xt=[x(i) x(i) x(i+1) x(i+1) x(i)];
            Yt=[0 y(i) y(i+1) 0 0];
            plot(Xt,Yt)
            if movie=='y'
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
        Area=Area + (1/2)*h*(y(i)+y(i+1));
    end;
    TRsum=Area;
    disp('Trapezoidal area approximate =')
    disp(TRsum)
    syms x;
    Actual=eval(int(f(x),a,b));
    Error=abs(TRsum-Actual);
    disp('Error = ')
    disp(Error)
        if movie=='y'
            for j=1:10
                M1(K)=getframe(gcf);
                K=K+1;
            end
        end
    repeat=input('Repeat with more subintervals(y/n)?  ','s');
end



