Home c++ Solution of the equation by the method of chord and the method...

# Solution of the equation by the method of chord and the method of half division of the segment (method of dichotomy)

Author

Date

Category

Hello! Posted by two functions to solve the equation by the chord method and the method of dichotomy, but the problem is that the results do not coincide.
Here are my written functions:

``````double chord_method (double a, double b, double e) {// Double Method
Double Next = 0;
Double TMP;
do {
TMP = NEXT;
next = b - f (b) * (a - b) / (F (a) - f (b));
a = b;
B = TMP;
}
While (ABS (Next - B) & GT; E);
RETURN NEXT;
``````

}

``````double dichotomy_method (double a, double b, double eps) {// Dichotomy Method
Double C;
While (B - A) / 2 & GT; EPS) {
C = (A + B) / 2;
if ((F (a) * f (c)) & gt; 0) {
a = c;
}
ELSE {
B = C;
}
}
Return C;
``````

}

and given, for example, the following equation:

``````double f (double x) {
Return x * x * x + 4 * x - 3;
``````

}

Cut on which it is necessary to calculate: from a = 0 to b = 1, accuracy E = 0.001.
The answers with such arguments are as follows: the chord method turns out: 0.6, method of dichotomy: 0.673828.

but with with such an equation, with the same accuracy, but on the segment [2, 10]:

``````double f (double x) {
Return x * x * x - 18 * x - 83;
``````

}

Results are as follows: Horde: 5.70511, method of dichotomy: 5.70508.

This question: what’s wrong with these functions? Formulas, it seems, found suitable, but the results do not coincide exactly at exactly. Or in terms of mathematics, everything is fine?

Correct the code slightly, and everything will turn out. And it is better to write not a global function, but to transmit it to the method as a parameter. See https://ideone.com/8h2fqq

``````double chord (double a, double b, double e, double (* f) (Double))
{// Horde Method
double Fa = F (a), fb = f (b);
if (FA * FB & GT; 0) Throw Runtime_error ("Wrong Data");
for (; ABS (B-a) & gt; e;)
{
double x = a - (b-a) * Fa / (FB-FA);
b = a;
FB = FA;
a = x;
Fa = F (x);
}
RETURN A;
}
Double Dichotomy (Double A, Double B, Double E, Double (* F) (Double))
{// Dichotomy Method
double Fa = F (a), fb = f (b);
if (FA * FB & GT; 0) Throw Runtime_error ("Wrong Data");
For (; ABS (B - a) & gt; e;)
{
Double X = (A + B) / 2;
if (f (x) * Fa & gt; 0) a = x;
ELSE B = X;
}
RETURN (A + B) / 2;
}
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.