Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,163 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,901 people online right now. Registration is fast and FREE... Join Now!




Help with Bisection method

 
Reply to this topicStart new topic

Help with Bisection method

MAX KORN
17 Dec, 2007 - 05:59 PM
Post #1

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 12


My Contributions
Hello everybody, I was hoping someone could guide me with this program I'm trying to construct which uses the bi-section method or the regular falsi method to find the roots of the function :

f(x) = a*sin( b* x ) + c*cos( d*x )

So far, I just have a little bit of code, and I have little to no idea how to proceed. Here's my work so far:

CODE


//Using the bisection and regular falsi method to find root of y= a*sin(b*x) + c*cos(d*x)
#include <iostream> //facilitate interaction with user, cout cin
#include <iomanip> //If you need to produce formatted output
#include <cmath>

using namespace std;
int main ()
{

        double y1 = a*sin(b*x1) + c*cos(d*x1);
        double y2= a*sin(b*x2) + c*cos(d*x2);
        double bisect (double a, double b, double c, double d, double x1, double x2, double epsilon);

        cout <<"Enter the values of a, b, c, and d. Separated by spaces.";
        cin >> a >> b >> c >> d;
        cout <<"Enter the left side value of the interval: ";
        cin >> x1;
        cout <<Enter the right side value of the interval: ";
        cin >> x2;

        while ((x2-x1) > epsilon)
        {
        double xmid = .5(x1 + x2); //mid-point
        double y=


Thank you very much for any help!

This post has been edited by MAX KORN: 17 Dec, 2007 - 06:01 PM
User is offlineProfile CardPM
+Quote Post

salindor
RE: Help With Bisection Method
17 Dec, 2007 - 07:45 PM
Post #2

D.I.C Head
Group Icon

Joined: 10 Nov, 2006
Posts: 156



Thanked: 4 times
Dream Kudos: 50
My Contributions
Here is an article that talks about the bisection method.

http://en.wikipedia.org/wiki/Bisection_method

Salindor
User is offlineProfile CardPM
+Quote Post

MAX KORN
RE: Help With Bisection Method
17 Dec, 2007 - 07:49 PM
Post #3

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 12


My Contributions
Thanks, I've actually looked at this before but was still wondering if I can use the Pseudo-code that they give for visual basic, as the premise for my code?

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Help With Bisection Method
17 Dec, 2007 - 08:11 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Well, in reality, pseudocode is pseudocode, and the language doesn't really matter. The example on Wikipedia is actually just straight VB code. But if you can translate that into pseudocode (a step-by-step verbal description of your algorithm), you're golden. It should probably help that VB is one of the easiest languages to translate into plain English (in my mind, at least).

One recommendation I have is to move your function prototype for the bisection function to a point prior to the main() routine. Put a call to the function in main(), and put the function definition after main is closed:
CODE
double bisect (double a, double b, double c, double d, double x1, double x2, double epsilon);

int main() {
    //...variable declaration, input of values, etc.
    double result = bisect (a, b, c, d, x1, x2, epsilon);
    //...output to screen, housekeeping
}

double bisect (double a, double b, double c, double d, double x1, double x2, double epsilon) {
    //...code for the bisection method goes here
}


This is the most standard way of organizing C++ code.


User is offlineProfile CardPM
+Quote Post

MAX KORN
RE: Help With Bisection Method
17 Dec, 2007 - 08:33 PM
Post #5

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 12


My Contributions
Is this more of the format you were looking for? Also, where do I put the regular-falsi method?

CODE

//Using the bisection and regular falsi method to find root of y= a*sin(b*x) + c*cos(d*x)
#include <iostream> //facilitate interaction with user, cout cin
#include <iomanip> //If you need to produce formatted output
#include <cmath>

using namespace std;

double bisect (double a, double b, double c, doubld d, doubld x1, double x2, double epsilon);

int main ()
{

        double y1 = a*sin(b*x1) + c*cos(d*x1);
        double y2= a*sin(b*x2) + c*cos(d*x2);
        

        cout <<"Enter the values of a, b, c, and d. Separated by spaces.";
        cin >> a >> b >> c >> d;
        cout <<"Enter the left side value of the interval: ";
        cin >> x1;
        cout <<Enter the right side value of the interval: ";
        cin >> x2;

double result = double bisect (a, b, c, d, x1, x2, epsilon);
//output to screen, housekeeping

      

}

double bisect (double a, double b, double c, double d, double x1, double x2, double epsilon) //code for bisection method
{
  while ((x2-x1) > epsilon)
        {
        double xmid = .5(x1 + x2); //mid-point
        double y=//code for bisection method goes here
}

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Help With Bisection Method
17 Dec, 2007 - 09:11 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
It's not about the format I'm looking for...it's a matter of getting the program running how it should smile.gif. Prototyping functions in main() is doable, but it's not a great programming practice.

As for the regula falsi method (no 'r' on the end of that), you'd write another function for that as well, with a prototype, one or more calls to the function from main(), and a function definition. Are you doing these to be able to compare their performance (i.e. speed), or their accuracy?
User is offlineProfile CardPM
+Quote Post

MAX KORN
RE: Help With Bisection Method
17 Dec, 2007 - 09:14 PM
Post #7

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 12


My Contributions
I think it's just to compare accuracy. How does the regula falsi method work though? That's another component I haven't figured out.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Help With Bisection Method
17 Dec, 2007 - 09:21 PM
Post #8

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
As always, Google is your friend. "regula falsi method" and "false position method" both produce a ton of hits. And Wikipedia has an entry on that one, as well. Complete with graphics and...(ta-dah!) full C code for the method.

In any algorithmic problem like this, starting out by writing the code is, IMO at least, the wrong way to go. You need to sit down and look at the algorithm as your first step. Then, once you've got that figured out, start writing the code that implements it.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month