need to convert definit integral to trapezoidal rule for computer program

lleb

New member
Joined
Aug 1, 2013
Messages
3
Hello all, I am attempting to write a C Program that will estimate the results of a definite integral using the Trapezoidal Rule. My hang up is the sum portion of the rule of i=1 to n-1 f(xi)

the formula i have for the trapezoidal rule is:

integral from a to b f(x)dx approx: h/2(f(a) + f(b) + 2 SUM i=1 -> n-1 f(xi))

with an integral of: 1 to 5 x^2sin(x) dx assuming n = 150.

What I have that is NOT working is the following:

SUM = n/2[a1 + an] = 150/2[sin(1) + 1492sin(149)]

and when i put it all together i have:

h = (b-a)/n = 4/150 = 2/75
h/2 = 1/75
1/75[ sin(1) + 25sin(5) + 2 { 50/2[sin(1) + 1492sin(149)] } ]

using my Ti93 to solve the definite integral i get an approx answer of 2.7, but when i plug in the above equation i get an approx answer of 350. that is a significant error.

Some assistance would be greatly appreciated.

Thank you in advance.
 
Hello all, I am attempting to write a C Program that will estimate the results of a definite integral using the Trapezoidal Rule. My hang up is the sum portion of the rule of i=1 to n-1 f(xi)

the formula i have for the trapezoidal rule is:

integral from a to b f(x)dx approx: h/2(f(a) + f(b) + 2 SUM i=1 -> n-1 f(xi) )

with an integral of: 1 to 5 x^2sin(x) dx assuming n = 150.

What I have that is NOT working is the following:

SUM = n/2[a1 + an] = 150/2[sin(1) + 1492sin(149)] .... where did 2 SUM i=1 -> n-1 f(xi) go?!!

and when i put it all together i have:

h = (b-a)/n = 4/150 = 2/75
h/2 = 1/75
1/75[ sin(1) + 25sin(5) + 2 { 50/2[sin(1) + 1492sin(149)] } ]

using my Ti93 to solve the definite integral i get an approx answer of 2.7, but when i plug in the above equation i get an approx answer of 350. that is a significant error.

Some assistance would be greatly appreciated.

Thank you in advance.
.
 

SUM = n/2[a1 + an] = 150/2[sin(1) + 1492sin(149)] .... where did 2 SUM i=1 -> n-1 f(xi) go?!!.

the equation for sum is n/2[a1 + an] a1 and an are the plugged in values such that f(1) = sin(1) and f(n-1) = 149^2 sin(149) were n = 150. no?
 
Hello all, I am attempting to write a C Program that will estimate the results of a definite integral using the Trapezoidal Rule. My hang up is the sum portion of the rule of i=1 to n-1 f(xi)

the formula i have for the trapezoidal rule is:

integral from a to b f(x)dx approx: h/2(f(a) + f(b) + 2 SUM i=1 -> n-1 f(xi))

with an integral of: 1 to 5 x^2sin(x) dx assuming n = 150.

What I have that is NOT working is the following:

SUM = n/2[a1 + an] = 150/2[sin(1) + 1492sin(149)]

and when i put it all together i have:

h = (b-a)/n = 4/150 = 2/75
h/2 = 1/75
1/75[ sin(1) + 25sin(5) + 2 { 50/2[sin(1) + 1492sin(149)] } ]

using my Ti93 to solve the definite integral i get an approx answer of 2.7, but when i plug in the above equation i get an approx answer of 350. that is a significant error.

Some assistance would be greatly appreciated.

Thank you in advance.

\(\displaystyle \displaystyle \int_1^5x^2sin(x) dx\)

your integration should be:

= sin(1) + 25*Sin(5) + \(\displaystyle \dfrac{1}{75}\)*[ (1\(\displaystyle \dfrac{2}{75}\))2 * sin(1\(\displaystyle \dfrac{2}{75}\)) + (1\(\displaystyle \dfrac{4}{75}\))2*sin (1\(\displaystyle \dfrac{4}{75}\)) + (1\(\displaystyle \dfrac{6}{75}\))2*sin (1\(\displaystyle \dfrac{6}{75}\)).......]

= sin(1) + 25*Sin(5) + \(\displaystyle \dfrac{1}{75}\)*[ (\(\displaystyle \dfrac{77}{75}\))2 * sin(\(\displaystyle \dfrac{77}{75}\)) + (\(\displaystyle \dfrac{79}{75}\))2*sin (\(\displaystyle \dfrac{79}{75}\)) + (\(\displaystyle \dfrac{81}{75}\))2*sin (\(\displaystyle \dfrac{81}{75}\)).......]
 
Last edited by a moderator:
\(\displaystyle \displaystyle \int_1^5x^2sin(x) dx\)

your integration should be:

= sin(1) + 25*Sin(5) + \(\displaystyle (\dfrac{1}{75}\)*[ (1\(\displaystyle \dfrac{2}{75}\))2 * sin(1\(\displaystyle \dfrac{2}{75}\)) + (1\(\displaystyle \dfrac{4}{75}\))2*sin (1\(\displaystyle \dfrac{4}{75}\)) + (1\(\displaystyle \dfrac{6}{75}\))2*sin (1\(\displaystyle \dfrac{6}{75}\)).......]

= sin(1) + 25*Sin(5) + \(\displaystyle (\dfrac{1}{75}\)*[ (\(\displaystyle \dfrac{77}{75}\))2 * sin(\(\displaystyle \dfrac{77}{75}\)) + (\(\displaystyle \dfrac{79}{75}\))2*sin (\(\displaystyle \dfrac{79}{75}\)) + (\(\displaystyle \dfrac{81}{75}\))2*sin (\(\displaystyle \dfrac{81}{75}\)).......]


i see, thank you that is a bit more complicated and i like it.
 
Hello all, I am attempting to write a C Program that will estimate the results of a definite integral using the Trapezoidal Rule.
Excellent reference:

Press et al., "Numerical Recipes", Cambridge University Press, any edition, section 4.1

See also subroutine Trapzd in section 4.2.
 
Top