# Thread: Reinmann Integration m file error? :F

1. ## Reinmann Integration m file error? :F

Hey all,

Iv written a m file on Reimann Integration:

function [i] = in(a,b,n)
% integrates increasing function
h = (b-a)/n;
l = a+h*(0:n-1);
u = l+h;
U = h*sum(u^2)
L = h*sum(1^2)
U - L
end

It doesn't work... I want it to give a Lower Bound and an Upper bound as i use more strips, n.

Any help?

2. Originally Posted by ramdrop
Hey all,

Iv written a m file on Reimann Integration:

function [i] = in(a,b,n)
% integrates increasing function
h = (b-a)/n;
l = a+h*(0:n-1);
u = l+h;
U = h*sum(u^2)
L = h*sum(1^2) <-- one squared won't work
U - L
end

It doesn't work... I want it to give a Lower Bound and an Upper bound as i use more strips, n.

Any help?
CB

3. Doesn't work with any function. If i had Sin(u), and then Sin(1)

4. Try this:

Code:
function [II,Lo,Up] = in(a,b,n)
% integrates increasing function
h = (b-a)/n;
lo = a+h*(0:n-1);
up = lo+h;
Up = h*sum(up.^2);
Lo = h*sum(lo.^2);
II=(Up+Lo)/2;
end
CB

5. That works, for example I type:

in(1,5,10) and get 41.4400

but im still unsure of the actual function, because inside the m-file I don't see a function defined, or am i being stupid?

6. Originally Posted by ramdrop
That works, for example I type:

in(1,5,10) and get 41.4400

but im still unsure of the actual function, because inside the m-file I don't see a function defined, or am i being stupid?
These lines evaluate the Upper and Lower integrals respectivly:

Code:
  Up = h*sum(up.^2);
Lo = h*sum(lo.^2);
So here we are attempting to evaluste the integral of $f(x)=x^2$

CB

7. Im still not really understanding it, I find MATLAB so difficult although it is a very effective tool to use.

I see what you mean by the function x² , if I was say, to use exp(x) function, The way I see it is the "up / lo" is defined as "x", so I would put in, exp(lo) and exp(up) or something?

With the actual command 'in(1,5,10)' thats integrating the function between limits 5 and 1 , subtracting the 1st limit from the 5th limit as you normally would do.

The 'n' in this case is suppose to be the number of strips, but not seeing how, or where this affects it. I know its suppose to make it more accurate or something, so Il try with say, 100 strips and see, it should ultimately get closer to the Integral.

Im also trying to, (after iv typed the command) I want it to show "Up = ...." and Lo = "....", and mathematically I work out the integral to be between, then when I up the value of 'n' the "bounds" get closer together, how would i write this?

8. Originally Posted by ramdrop
Im still not really understanding it, I find MATLAB so difficult although it is a very effective tool to use.

I see what you mean by the function x² , if I was say, to use exp(x) function, The way I see it is the "up / lo" is defined as "x", so I would put in, exp(lo) and exp(up) or something?

With the actual command 'in(1,5,10)' thats integrating the function between limits 5 and 1 , subtracting the 1st limit from the 5th limit as you normally would do.

The 'n' in this case is suppose to be the number of strips, but not seeing how, or where this affects it. I know its suppose to make it more accurate or something, so Il try with say, 100 strips and see, it should ultimately get closer to the Integral.

Im also trying to, (after iv typed the command) I want it to show "Up = ...." and Lo = "....", and mathematically I work out the integral to be between, then when I up the value of 'n' the "bounds" get closer together, how would i write this?
Function definition for integrating exp(x) from a to b using n subintervals:

Code:
function [II,Lo,Up] = Riemann(a,b,n)
% integrates increasing function
h = (b-a)/n;
lo = a+h*(0:n-1);
up = lo+h;
Up = h*sum(exp(up));
Lo = h*sum(exp(lo));
II=(Up+Lo)/2;
end
Calling code and results:

Code:
>> [II,Lo,Up]=Riemann(1,5,2000)
II =
145.6949
Lo =
145.5492
Up =
145.8406
>>
Actual integral is $exp(5)-exp(1)=145.6949$

CB