2

我有一个学校问题,但我不明白它实际上问的是什么。你们中有人知道它真正要求的是什么吗?我不需要代码,我只需要理解它。

这就是问题: 构造一个使用正割法解决问题的计算机程序: f(x) = (1+x) cos( sin(x)3 ) - 1.4 = 0 从 x=2.0 的初始猜测开始和 x=2.1,获得 x 的近似值,使得 |f(x)| < 0.0000001。

这是我理解的代码,但我认为我没有正确理解这个问题。

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

1 回答 1

5

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

于 2010-09-24T04:27:48.510 回答