| New Reply |
c++ simpson's method --loop doesn't stop |
Share Thread |
| Mar6-11, 11:11 AM | #1 |
|
|
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;
}
Thanks! |
| Mar6-11, 11:28 AM | #2 |
|
Mentor
|
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;
|
| Mar6-11, 11:32 AM | #3 |
|
Mentor
|
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
}
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. |
| Mar6-11, 11:33 AM | #4 |
|
|
c++ simpson's method --loop doesn't stop
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++;
}
|
| Mar6-11, 11:40 AM | #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.. |
| Mar6-11, 12:36 PM | #6 |
|
Mentor
|
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. |
| Mar6-11, 12:44 PM | #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)..
|
| Mar6-11, 01:54 PM | #8 |
|
Mentor
|
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. |
| Mar7-11, 03:59 AM | #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;
}
If i use n*=2 instead of n++ ,it gives : |
| New Reply |
Similar discussions for: c++ simpson's method --loop doesn't stop
|
||||
| Thread | Forum | Replies | ||
| [SOLVED] Riding a Loop the Loop (velocity at the bottom of the loop) | Introductory Physics Homework | 13 | ||
| Simpson approximation method | Calculus & Beyond Homework | 2 | ||
| simpson method question(numerical analysis) | Calculus & Beyond Homework | 8 | ||
| when to stop a simulate annealing when it doesn't stop by itself | Set Theory, Logic, Probability, Statistics | 3 | ||
| Why doesn't this method work? (Re: Simultaneous ODEs) | Calculus & Beyond Homework | 3 | ||