473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Bisection method in C++

5
Hi,
I wrote befor about the bisection method in C++. This time I managed to make two source's and compile them. But both give me some error that i don't understand. Here are the codes:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <stdlib.h> 
  3. #include <math.h> 
  4.  
  5. using namespace std; 
  6.  
  7. double f1( double x) { 
  8. return x*x-pow(1.034554,2); 
  9. double f2( double x) { 
  10. return 2*x; 
  11.  
  12. double bisection(double (*f)(double), 
  13.  
  14. double xL=-100, double xR=100) { 
  15.  
  16. double xN; 
  17.  
  18. if( (*f)(xL)*(*f)(xR) >0) { 
  19. cout << "Error!" << endl; 
  20. return xL; 
  21.  
  22. while (fabs(xL-xR)>0.0001) { 
  23. xN = (xL+xR)/2; 
  24. if( (*f)(xL)*(*f)(xN) <0) 
  25. xR = xN; // i.e. xL and xN have different signs 
  26. // so xRand xN have the same signs 
  27. else 
  28. xL = xN; 
  29. return (xL+xR)/2; 
  30.  
  31. int main() { 
  32. double tmp; 
  33. cout << bisection(f1,-100,100) << endl; 
  34.  system("PAUSE"); 
When i run it here's what comes out on the screen:
Expand|Select|Wrap|Line Numbers
  1. Error!
  2. -100
  3. Press any key to continue...
This source(which maybe is not very good):
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdlib.h> 
  4. #include <math.h> 
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int xR, xL,xM, epsilon;
  10. cout <<"Enter a value for the left side: " "\n";
  11. cin >> xL;
  12. cout <<"Enter a value for the right side: " "\n";
  13. cin >> xR;
  14. cout <<"Enter a value for epsilon: " "\n";
  15. cin >> epsilon;
  16.  
  17.   while ((xR - xL) > epsilon)
  18.  
  19.  
  20.   xM = (xR + xL) / 2;
  21.  
  22.  
  23.   if (xL * xM > 0) 
  24.      xL = xM;
  25.   else
  26.  
  27.     xR = xM;
  28.  
  29. system ("pause");
  30. return 0;
  31. }
gives me exactly the same error:
Expand|Select|Wrap|Line Numbers
  1. Error!
  2. -100
  3. Press any key to continue...
Could anyone try the program and help me solve the problem.
I need this program working untill 12 o'clock tonight.
Please try to explain the things in a simple way because i am entirely new to programming.
Thank you!
Jan 22 '07 #1
1 7628
horace1
1,510 Expert 1GB
you appear to have your xL and xR start points wrong as there are two roots at -1.03459 and +1.03459 so you test fails
try
Expand|Select|Wrap|Line Numbers
  1. cout << bisection(f1,0,100) << endl; 
  2. cout <<  bisection(f1,-100,0) << endl; 
  3.  
Jan 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: aruna | last post by:
to write a c program to find the roots of the equation using bisection method that too using array or pointers
1
by: salman patni | last post by:
hi myself patni salman i am studied in amit collage aurangabad.i need solution of bisection method program using c language with algorithm
2
by: t4zone58 | last post by:
Hey guys! I am beginner at VB studying. I got confused with solve the functions by applying bisection method in VB. Basically I can understand the bisection method mathmatically. However i got...
6
by: Cappo | last post by:
Hello, I just started programming in C++ and i have a problem with a task.I need to write a program which illustrates the Bisection method in C++. I have some codes, which use the bisection code,...
1
by: beneyam | last post by:
i want to develop a program on bisection method using c++. the program should ask user to enter the function and the no of iterations at the run time. any one do this i really appreciate. 10qs 4...
2
by: Baba sina | last post by:
How can I solve f(x) with bisection method with the interval ?
1
by: zxipo | last post by:
the numerical prolem is is x*x*x*x-4*x-10=0 this to be solve by bisection method
0
by: jhen61315 | last post by:
Hello there, First time here in forum, I hope im welcome here.... And I hope you'll help me in my assignment in bisection method because i dont know how to start to make in visual basic.. If you...
1
by: lhbrown | last post by:
Hi. I'm looking for some help with the Bisection method in Visual Basic, which I'm using for the first time as part of a university project. I am using the bisection method to work out the water...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.