This is not giving me the right answer! I have checked for errors like integer division and rounding, but can't find any. I am aware the comments are slightly inaccurate and they will be improved. But what's wrong with the actual code?(adsbygoogle = window.adsbygoogle || []).push({});

It's probably something in the actual maths... should i run from 2 to N-2? I clearly don't know how to use sum notation properly, since I can't convert ##\sum_{i=2}^{N-1}## into a for statement correctly.Code (Java):

//This method uses the trapezium rule to calculate the integral

//a is the lower limit, b the upper limit, c is the power of x in the function, N the number of trapezia

//Returns the result of the integral

public static double trapeziumRule(double a, double b, double c, int N){

double h = (b-a)/(N-1); //h is the width of a single trapezium

double x = 0.5*(f(a,c)+f(b,c)); //x is the average height of the trapezium

for(int i=2; i<N-1; i++){

x += f(a+h*i,c);

}

double integral = h*x;

return integral;

Actually, is that the problem? Should it be i<=N-1? And i starts from 1? That would also affect my formula for h.

Except that doesn't work either.

**Physics Forums - The Fusion of Science and Community**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Trapezium rule

Loading...

Similar Threads - Trapezium rule | Date |
---|---|

What should be the rules to write excellent code? | Monday at 1:02 PM |

Fixed Format Fortran Rules | Nov 24, 2015 |

Java rule on Methods with multiple parameters | Oct 2, 2013 |

50 percent rule | Mar 26, 2011 |

**Physics Forums - The Fusion of Science and Community**