开发者

C++ Use secant method to solve function

开发者 https://www.devze.com 2023-01-17 15:16 出处:网络
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.

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;
}


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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消