0

Lets say I have created a function Sn=LeftRiemannSum(f,left,right,N) ,that computes the left riemann sum over the interval left to right with N subdomains.i.e.:

Sn=sum(f(xi)h) for all subdomains i=0 to N-1. f is my function and xi=left+ih , so the input arguments left=x0 and right=xN.

Let f be my anonymous function (ex f=@(x)(x.*log(1+x)) .

I also estimated the Sn for varying N, from N=10 to 100000.

Now, I simply want to compute the value of the series Sn when N -> infinity. Inside the function I have a for loop [ for i=0:(N-1)] so I will have endless loop ..

Can I pass the function somehow to the 'limit' command? Any clues?

Thanx!

PS: The main part of the code of my function LeftRiemann Sum is the following:

for i=0:(N-1)
    x=x0+i.*h;
    y=f(x);
    A=y.*h;
    S=S+A
  end
   Sn=S

1 Answers1

0

If N -> inf the value of the Riemann sum will be the integral of the function in the interval [left, right].

One can introduce a check like this:

function Sn = LeftRiemannSum(f, left, right, N)

if isinf(N)
    Sn = exact integral of the function f in the interval [left, right]
    return
end

% // Rest of the code

the challenge here is to calculate the analytic integral of f. Unless you use the symbolic math toolbox, all the results from Matlab algorithms will be approximations.

Since you cannot really run your loop til infinity, I see two options:

  1. I would declare a limit for N and throw an error if the input is greater than the limit.
  2. Internaly define infinity as a high number, whenever you receive inf as parameter, you replace it by, let's say, N=1e200. Take into account that with this strategy your algorithm could take a long time to finish executing.
gire
  • 1,105
  • 1
  • 6
  • 16
  • The asker asks about how to use the `limit` command which is part of the symbolic toolbox. As such I think a (somewhat vague) answer of how to deal with this if you don't have that toolbox is a bit out of place. – Dennis Jaheruddin Nov 03 '14 at 13:43
  • @DennisJaheruddin I do not think the OP is referring to the symbolic math toolbox, since it is not mentioned in the question. Only the OP can clarify that point. For a symbolic system, passing `N=inf` would pose no problem. – gire Nov 03 '14 at 14:06