I am trying to write a program to find the integrals of a function, where the computer determines the number of slices that it should use, going through a loop until it finds the change small enought to ignore. I want it to have a tolerance of .01%

Can anyone whip up a bit of code?

This isn't for a class....personal useage. Im just blank on how to start it for the loop.

Edit: This is what I have for code thus far...but I think I don't have it set up right to include the loop that I need. Also, for N, I want to start at 25 and bump it up by 25 through each loop....

double myfunction(double) ;



double integrate(double lower, double upper, int N)
{
	
    double width = (upper - lower) / N ;  
	double sum = 0.0 ;  
	double x ; 
	int i ;  
	for(i = 0 ; i < N ; i++ )
	{
		x = lower + width * (i + 0.5) ;
		
		sum += width * myfunction(x) ;

	}

Dani AI

Generated

A clear approach and a few safety checks will make the loop you want robust.

Start N at 25, call your existing integrate(lower, upper, N) to get an estimate, then increase N by 25 and call integrate again. Compare the two estimates with a relative test (|Inew - Iold| / |Iold|) and stop when that relative change is ≤ 0.01% (0.0001). was right to flag the missing return in your integrate function — ensure it returns the computed sum. ’s point about indexing is relevant: every time N changes you must recompute the sampling points to match that N (do not try to reuse old indices unless you carefully remap them).

Watch these practical details:

  • If the previous integral is (near) zero, use an absolute tolerance fallback instead of dividing by zero.
  • Limit total work: set a sensible maxN or maxIterations so the loop can bail out if the integrand is slow to converge. Return a status or final N so the caller knows whether the tolerance was reached.
  • For better numeric accuracy with many samples, consider using Kahan summation when accumulating the sum to reduce rounding error.

If performance matters, increasing N by a fixed small amount is often inefficient. Doubling N each iteration or switching to a composite Simpson / Romberg or an adaptive-quadrature routine will converge far faster; see Adaptive Simpson’s method for a standard alternative (Adaptive Simpson's method). Also, as hinted, standard numerical-integration references discuss composite rules and error estimates.

Example wrapper (call your midpoint/composite integrate from here):

double adaptive_integrate(double a, double b, double tol_percent, int maxN)
{
    const int STEP = 25;
    int n = STEP;
    double prev = integrate(a, b, n);
    while (n < maxN) {
        n = n + STEP;
        double curr = integrate(a, b, n);
        double denom = fabs(prev) > 1e-300 ? fabs(prev) : 1.0;
        double rel = fabs(curr - prev) / denom;
        if (rel <= tol_percent * 1e-2) return curr;
        prev = curr;
    }
    return prev; /* maxN reached; consider signalling failure */
}

This gives a safe, easy-to-read adaptive loop you can drop into your program and extend to return the final n or a success flag.

Recommended Answers

All 5 Replies

maybe this site will help you out :

>Also, for N, I want to start at 25 and bump it up by 25 through each loop...
If I understand correctly.

for (i = 25 ; i < N ; i += 25 )

You are not returning the final result of sum.

or did you mean to use x = lower + width * (i*25 + 0.5) ;

how to write the C program of pyramid:
1
2 3
4 5 6

7 8 8 10

how to write the C program of pyramid:
1
2 3
4 5 6

7 8 8 10

Start a new thread for that. Sheesh!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.