Implement regula falsi using array

Right now i am writing a program that will solve for the real root of the equation .05x - sin(x) using regula-falsi method. i am thinking to implement it in an array for the purpose of a good output.. but i encountered a problem and i think asking for your help would be the best solution for this...

the algorithm for regula-falsi (method of false position) is as follows:

•Start with two guesses x+ and x– that
bracket a root with f(x+) > 0 and f(x–) < 0..

• Repeat the following steps for the two
current guesses x+ and x–
– Use a linear interpolation (like the secant
method) to get xnew and fnew = f(xnew)

xnew = xplus - f(xminus) * (xplus - xminus)/(f(xplus) - f(xminus))
fnew = f(xnew)

– If fnew > 0 replace x+ by xnew
– If fnew < 0 replace x– by xnew

• Continue until iterations give desired
accuracy for x or f

i want the user to see this output:


________________________________________________________________________________
xplus    xminus      f(xplus)      f(xminus)            xnew           f(xnew)
________________________________________________________________________________
4        2           9.568E-01    -8.0930E-01    2.91647977182172    -7.7392E-02
4.0000   2.916479    9.568E-01    -7.7392E-02    2.99756336632598     6.3463E-03
2.9976   2.916479    6.346E-03    -7.7392E-02    2.99141826018393    -3.9652E-05
2.9976   2.991418    6.346E-03    -3.9652E-05    2.99145641684829    -1.7194E-08
2.9976   2.991456    6.346E-03    -1.7194E-08    2.99145643339340    -7.4547E-12



Please help me implement it. Thanks!

Here is my wrong code





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include <iostream>
#include <math.h>
using namespace std;

double f(double);
double falsi(double, double);

const int MAX = 1000;


int main()
{
    
    double guess1, guess2;
    double xnew[MAX], xplus[MAX], xminus[MAX];
    int imax;
    
    cout << "Enter first guess: ";
    cin >> xplus[0];
    cout << "Enter second guess: ";
    cin >> xminus[0];
    cout << "Set maximum iteration: ";
    cin >> imax;
    
    for(int i = 0; i < imax; i++)
    {
            xnew[i] = falsi(xplus[i], xminus[i]);
            
            if(f(xnew[i])> 0)
            {
                         xplus[i] = xnew[i];
            }
            else
            {
                         xminus[i] = xnew[i];
            }
    }
    
    for(int i = 0; i < imax; i++)
    {
            cout.setf(ios::fixed);
            cout.setf(ios::showpoint);
            cout.precision(20);
            cout << xnew[i] << endl;
    }
    
    cout << endl << "The root is approximately: ";
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(20);
    cout << xnew[imax - 1];
    
    cin.get();
    cin.get();
    
    return 0;
}
double f(double x)
{
       return .05 * x - sin(x);
}
double falsi(double xp, double xm)
{
       return xp - f(xp) *(xp - xm)/(f(xp) -f(xm));
}
Last edited on
Your problem is that you don't update your next x. Replace 29 to 36 with:
1
2
3
4
5
6
7
8
9
10
11
 
           if(f(xnew[i])> 0)
            {
                         xplus[i+1] = xnew[i];
                         xminus[i+1]=xminus[i];
            }
            else
            {
                         xminus[i+1] = xnew[i];
                         xplus[i+1]=xplus[i];
            }
thank you so much.. it's a big help..
Topic archived. No new replies allowed.