C++ Use secant method to solve function

user69514 picture user69514 · Sep 24, 2010 · Viewed 7.1k times · Source

I have a school problem but I do not understand what it actually asks. Any of you have an idea what it's really asking for? I don't need code, I just need to understand it.

This is the problem: Construct a computer program that uses the Secant method to solve the problem: f(x)  =  (1+x) cos( sin(x)3 ) -  1.4   =  0 Starting with the initial guesses of x=2.0 and x=2.1, obtain an approximation to x such that  |f(x)| < 0.0000001.

This is my code from what I understand, but I think I'm not understanding the question correctly.

#include <iostream>
#include <cmath>

double secant(double x);

using namespace std;

int main()
{
    double x = 2.0;
    double r = 0.0;
    int counter = 0;

    while( r < 0 && counter <= 40)
    {
        r =secant(x);
        cout << "x: " << x << ", f(x): " << r << endl;
        counter++;
        x += 0.1;
    }



    return 0;
}

double secant(double x)
{
    double r;
    r = (1+x) * cos(pow(sin(x), 3.0)) - 1.4;
    return r;
}

Answer

JoshD picture JoshD · Sep 24, 2010

You are supposed to use the Secant Method: http://en.wikipedia.org/wiki/Secant_method

Follow the method as described in the article. It is an iterative method much like Netwon's method. You'll need to make a function to evaluate x(n+1) given x(n) and iterate it until your margin of error is less than specified.

The coding side of this may prove fairly straightforward as long as you know what the secant method is. Also, that page has a code example. That should prove pretty useful. :)