These codes are tested in Dev C++ and are error free. If any error occurs, comment below. Other programs will be uploaded as soon as possible.
Thank you! :)

Friday, April 15, 2016

Secant Method

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x - 4*x - 10
#define e 0.001
int main()
{
    float x0, x1, x2, f0, f1;
    double E;
    static int count;
    cout << "Enter x0:" ;
    cin >> x0;
    cout << "Enter x1:" ;
    cin >> x1;
    Top:
    f0 = f(x0);
    f1 = f(x1);
    x2 = x1 - ((f1 * (x1 - x0)) / (f1 - f0));
    E = (fabs((x2 - x1) / x2) * 100);
    if(E < e)
    {
         cout << "The root is " << x2 << endl;
         cout << "Number of iterations = " << count << endl;
         }
         else{
              count++;
              x0 = x1;
              x1 = x2;
              goto Top;
           
              }
              getch();
              return 0;
              }






No comments:

Post a Comment

If you have any problem, please leave your message here.