Make false position method as a function.

Hello there!

At this moment, I am writing a program that solves the real root of the function f(x) = .05x - sin(x) using false position method. I have my code below . It works fine, but i want to make this false position method a function so that my main program will appear short.

I still have 5 methods left and i think that if this method is fixed then all the other remaining methods will also be fixed by similar way.

This is what i want to do in my main program:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  int main()
  {
    int selection;

    cout << "What method do you want to use?" << endl;
    cout << "Press 4 for false position. "<< endl;
    cin >> selection;

    if(selection == 4)

   {
      flaseposition();
    
    }




Here is my 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
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <math.h>
#include <Windows.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 << endl;
    cout << "Enter first guess: ";
    cin >> xplus[0];
    cout << endl;
    cout << "Enter second guess: ";
    cin >> xminus[0];
    cout << endl;
    cout << "Set maximum number of iterations: ";
    cin >> imax;
    
    for(int i = 0; i < imax; i++)
    {
            xnew[i] = falsi(xplus[i], xminus[i]);
            
           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];
            }
    }
    cout << endl << endl;
    for(int i = 0; i < imax; i++)
    {
            cout.setf(ios::fixed);
            cout.setf(ios::showpoint);
            cout.precision(20);
            cout << xnew[i] << endl;
    }
    
    cout << endl << endl << endl;
    cout << "The root is approximately: ";
    
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(20);
    cout << endl << endl << 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));
}


Please help me implement it. Thanks!
Last edited on
First of all, it should be
if(selection == 4)
(two equals signs instead of one), since one equal sign is assignment.

Technically, you could take your current code above and pretty much rename main to falseposition (wait, did you want falseposition or flaseposition? You have the latter in your code).
Then stick your current work-in-progress main function in and you should be fine.

You might want to consider returning the calculated value from the function and move the code that prints it to the screen into main, though. (That way, you won't have to duplicate that part of the code in each of your different functions.)
Topic archived. No new replies allowed.