Modified Secant Method

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<iomanip>
#include<math.h>
#include<stdlib.h>

/* Defining equation to be solved.
   Change this equation to solve another problem. */
#define    f(x)    x*x*x - 2*x - 5

using namespace std;

int main()
{
	 float x0, x1, x2, f0, f1, f2, e;
	 int step = 1, N;
	 
	 /* Setting precision and writing floating point values in fixed-point notation. */
   cout<< setprecision(6)<< fixed;

	 /* Inputs */
	 cout<<"Enter first guess: ";
	 cin>>x0;
	 cout<<"Enter second guess: ";
	 cin>>x1;
	 cout<<"Enter tolerable error: ";
	 cin>>e;
	 cout<<"Enter maximum iteration: ";
	 cin>>N;

	 /* Implementing Secant Method */
     cout<< endl<<"**************"<< endl;
	 cout<<"Secant Method"<< endl;
	 cout<<"**************"<< endl;
	 do
	 {
		  f0 = f(x0);
		  f1 = f(x1);
		  if(f0 == f1)
		  {
			   cout<<"Mathematical Error.";
			   exit(0);
		  }

		  x2 = x1 - (x1 - x0) * f1/(f1-f0);
		  f2 = f(x2);

		  cout<<"Iteration-"<< step<<":\t x2 = "<< setw(10)<< x2<<" and f(x2) = "<< setw(10)<< f(x2)<< endl;

		  x0 = x1;
		  f0 = f1;
		  x1 = x2;
		  f1 = f2;

		  step = step + 1;

		  if(step > N)
		  {
			   cout<<"Not Convergent.";
			   exit(0);
		  }
	 }while(fabs(f2)>e);

	 cout<< endl<<"Root is: "<< x2;

	 return 0;
}

I have my code above using the Secant Method. If I want to do the Modified Secant Method. How can I change my algorithm? Can anyone give me some ideas, please?
Last edited on
Which particular "modified" secant method were you thinking of?
I guess you probably meant this one. It's more like a modified Newton-Raphson method than a secant method, but hey-ho.
It's not guaranteed to converge (as I'm sure that you are aware).
x0 and f0 aren't actually needed in the modified method, but are in the original.

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;


/* Defining equation to be solved.
   Change this equation to solve another problem. */
double f( double x ) { return x * x * x - 2 * x - 5; }


int main()
{
   double x0, x1, f0, f1, eps, delta;
   int step = 1, N;
	 
   cout << setprecision(6) << fixed;

   cout << "Enter first guess: ";   cin >> x0;
   cout << "Enter second guess: ";   cin >> x1;
   cout << "Enter tolerable error: ";   cin >> eps;
   cout << "Enter delta for modified method: ";   cin >> delta;    // modified secant only
   cout << "Enter maximum iteration: ";   cin >> N;

   /* Implementing Secant Method */
   cout << "\n*************\n";
   cout <<   "Secant Method";
   cout << "\n*************\n";
   
   f0 = f(x0);
   f1 = f(x1);
   do
   {
      if ( step > N )
      {
         cout << "Not Convergent";
         exit(0);
      }

//    double dfdx = ( f1 - f0 ) / ( x1 - x0 );                       // Original secant method
      double dfdx = ( f(x1+0.5*delta) - f(x1-0.5*delta) ) / delta;   // (ONE POSSIBLE) modified secant method
      if ( dfdx == 0 )
      {
         cout << "Mathematical error";
         exit(0);
      }
      x0 = x1;
      f0 = f1;
      x1 -= f1 / dfdx;
      f1 = f(x1);
      cout << "Iteration-" << step << ":\t x = " << setw(10) << x1 << " and f(x) = " << setw(10) << f1 << '\n';
      step++;
   } while ( abs( f1 ) > eps );

   cout << "\nRoot is: "<< x1;
}


Enter first guess: 0
Enter second guess: 1
Enter tolerable error: 1e-6
Enter delta for modified method: 0.01
Enter maximum iteration: 20

*************
Secant Method
*************
Iteration-1:	 x =   6.999850 and f(x) = 323.978251
Iteration-2:	 x =   4.765421 and f(x) =  93.688204
Iteration-3:	 x =   3.348644 and f(x) =  25.852454
Iteration-4:	 x =   2.531570 and f(x) =   6.161301
Iteration-5:	 x =   2.173907 and f(x) =   0.925791
Iteration-6:	 x =   2.097883 and f(x) =   0.037256
Iteration-7:	 x =   2.094558 and f(x) =   0.000070
Iteration-8:	 x =   2.094551 and f(x) =   0.000000

Root is: 2.094551 
Last edited on
Topic archived. No new replies allowed.