C++ simpson's method -loop doesn't stop

  • C/C++
  • Thread starter ggeo1
  • Start date
  • Tags
    C++ Method
In summary, the conversation discusses the use of Simpson's method in C++ and the issue of the loop not stopping when the desired result is achieved. The main function is shown, along with the simpson function and the different methods used in the for loops. Suggestions are made to fix the issue, such as incrementing n by doubling it instead of adding 1, and the final output is provided showing the iterations and results of the program.
  • #1
ggeo1
63
0
c++ simpson's method --loop doesn't stop

Hello, i have done the simpson's method and it works fine.My problem is that it doesn't stop when it gives me the desired result but the loop continues for ever..

Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;

double f(double x){
double y;
y=sqrt(x-0.1);
return y;

}

double simpson (double down,double up,int n){

    double h,sumeven,sumodd,all,result;

    h=(up-down)/n;
     sumeven=0.0;
    for (int i=1;i<=n/2.0-1.0;i++){

        sumeven+=f(down+2.0*i*h);//coefficients

    }
    sumodd=0.0;
    for (int i=1;i<=n/2.0;i++){
     sumodd+=f(down+h*(2.0*i-1.0));//coefficients

    }


    all=4*sumodd+2*sumeven;

    result=(h/3.0)*(f(down)+f(up)+all);
    return result;

}


int main()
{
    double eps=1e-6;//accuracy
    double exact=1.1767695;//exact solution for the integral
    double error=1.0;
    double result;

    int n=1;//initial point
    result=simpson(1.0,2.0,n);

    while (fabs(error-exact)>eps) {
        result=simpson(1.0,2.0,n);
        cout <<"\nFor n = "<<n<<",error = "<<fabs(error-exact)<<",value = "<<result;

    n++;
    }

    return 0;
}

I have done other methods too like that and they stop,but this one not.

Thanks!
 
Technology news on Phys.org
  • #2


In your main function, you define

Code:
    double eps=1e-6;//accuracy
    double exact=1.1767695;//exact solution for the integral
    double error=1.0;

but then in your while-loop you don't change any of those variables, so the condition 'fabs(error-exact)>eps' is always true.
 
Last edited:
  • #3


This loop looks flaky to me.
Code:
for (int i=1;i<=n/2.0-1.0;i++){
        sumeven+=f(down+2.0*i*h);//coefficients
}
In the first iteration of the while loop in main, n is 1, so the test expression in the for loop above is false, so the for loop doesn't execute.

When n == 1, n/2.0 - 1.0 is 0.5 - 1.0 == -.5, and i <= -.5 is false. You should not have to compare the counter in the for loop (i) with a double.
 
  • #4


Hello,you are right but

if i do :

Code:
 int n=1;//initial point
    result=simpson(1.0,2.0,n);

    while (fabs(result-exact)>eps) {
        result=simpson(1.0,2.0,n);
        cout <<"\nFor n = "<<n<<",error = "<<fabs(result-exact)<<",value = "<<result;

    n++;
    }

then the loop stops at step n=8 ,but the problem is that at step n=2 it gives me the right solution and then goes over again.
 
  • #5


Ok, i changed the for loops : for (int i=1;i<=n/2-1;i++)
for (int i=1;i<=n/2;i++)
(it was a mistake ,i didn't want to insert double values in the loops!)

But now again , it gives me the exit as a say above..
 
  • #6


I think that you are not using Simpson's Rule correctly. Each time you call simpson(), the 3rd argument should be an even integer.

In each iteration of Simpson's Rule the interval [a, b] should be broken up into an even number of subintervals.
 
  • #7


I used the same code in mathematica and it works fine.It gives me the right answer after 2 iterations.The same is happening here but it continues to step 8.(it gives me the right result every "even" steps)..
 
  • #8


Instead of incrementing n by 1 in each while loop iteration, try doubling n each time.

So instead of doing this: n++;
Do this: n *= 2;

See if that makes a difference.

You've changed your code since the first post. Can you show us what you have now?
Also, it would help to see the exact output.
 
  • #9


Here is my code :

Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;

double f(double x){
double y;
y=sqrt(x-0.1);
return y;

}

double simpson (double down,double up,int n){

    double h,sumeven,sumodd,all,result;

    h=(up-down)/n;
     sumeven=0.0;
    for (int i=1;i<=n/2-1;i++){

        sumeven+=f(down+2.0*i*h);//coefficients

    }
    sumodd=0.0;
    for (int i=1;i<=n/2;i++){
     sumodd+=f(down+h*(2.0*i-1.0));//coefficients

    }


    all=4*sumodd+2*sumeven;

    result=(h/3.0)*(f(down)+f(up)+all);
    return result;

}


int main()
{
    double eps=1e-6;//accuracy
    double exact=1.1767695;//exact solution for the integral
    double error=1.0;
    double result;

    int n=1;//initial point
   

    while (fabs(result-exact)>eps) {
        result=simpson(1.0,2.0,n);
        cout <<"\nFor n = "<<n<<",error = "<<fabs(result-exact)<<",value = "<<result;

    n++;
    }

    return 0;
}

and the exit is :

For n = 1,error = 0.401073,value = 0.775696
For n = 2,error = 0.000110833,value = 1.17666
For n = 3,error = 0.424624,value = 0.752146
For n = 4,error = 8.23034e-06,value = 1.17676
For n = 5,error = 0.263326,value = 0.913444
For n = 6,error = 1.67268e-06,value = 1.17677
For n = 7,error = 0.190651,value = 0.986119
For n = 8,error = 5.17684e-07,value = 1.17677


If i use n*=2 instead of n++ ,it gives :

For n = 1,error = 0.401073,value = 0.775696
For n = 2,error = 0.000110833,value = 1.17666
For n = 4,error = 8.23034e-06,value = 1.17676
For n = 8,error = 5.17684e-07,value = 1.17677

which is the same but with step 2 of course.
 

1. What is C++ Simpson's method and how does it work?

C++ Simpson's method is a numerical integration technique used to calculate the area under a curve by dividing it into smaller, simpler shapes. It uses a loop to iterate through the smaller shapes and add their areas together to approximate the total area under the curve.

2. Why is my C++ Simpson's method loop not stopping?

There could be several reasons for a C++ Simpson's method loop not stopping. One possibility is that the loop condition is not properly defined, causing it to continuously execute. Another possibility is that there is an error in the logic of the code, causing it to get stuck in an infinite loop. It is important to carefully check the loop condition and logic to identify and fix the issue.

3. How can I debug my C++ Simpson's method loop?

Debugging a C++ Simpson's method loop can be done by using breakpoints to pause the code at specific points and inspecting the values of variables. This can help identify any errors or issues with the loop condition or logic. Additionally, using the print or cout statements to display the values of variables at different points in the loop can also be helpful in understanding the code's execution.

4. Is there a more efficient way to implement C++ Simpson's method?

Yes, there are alternative methods for implementing C++ Simpson's method that may be more efficient. One such method is the adaptive Simpson's method, which dynamically adjusts the size of the smaller shapes based on the curve's curvature. This can result in a more accurate approximation of the area under the curve with fewer iterations.

5. Can C++ Simpson's method be used for any type of curve?

Yes, C++ Simpson's method can be used for any type of curve as long as it can be broken down into smaller, simpler shapes. This method is commonly used for polynomial functions, but it can also be applied to other types of curves such as trigonometric functions or exponential functions.

Similar threads

  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
6
Views
916
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
Back
Top