帮助正确的根查找C ++ [英] help with secant root finding C++

查看:117
本文介绍了帮助正确的根查找C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释如何使用正割方法找到方程的根?
方程是:(v / b)^ 2sin(alpha)= kr * Ts ^ 4 + Uc * Ts -q
找到 Ts 。我有所有其他信息,但困惑我应该做什么与​​seccant方法。

Can someone explain to me how I would use the secant method to find the root of an equation? The equation is: ( v / b ) ^2sin(alpha)= kr * Ts^4 +Uc *Ts -q and I have to find Ts. I have all the other info but am confused on what I'm supposed to do with the seccant method. Any help would be greatly appreciated.

以下是我的代码:

#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

void secant(double, double, double, double, double, double, double);
int main()
{
    double kr, uc, q, b, radians;

    const double PI = 4.0 * atan(1.0);
    ifstream datain("shuttle.txt");
    ofstream dataout("results.txt");
    datain >> kr >> uc >> q >> b;
    int velocity = 16000;
    double angle = 10;

    for (int velocity = 16000; velocity <= 17500; velocity += 500) {
        for (int angle = 10; angle <= 70; angle += 15) {
            radians = angle * PI / 180;
            cout << velocity << endl;
            cout << radians << endl;
            cout << angle << endl;
            secant(angle, radians, velocity, kr, uc, q, b);
        }
    }
    getchar();
}

void secant(double angle, double radians, double velocity, double kr, double uc,
        double q, double b)
{

}


推荐答案

方法解决四次(这是公认的相当参与),但因为这是家庭作业练习,你可能想要正割数值方法。对于额外的分数,您可能想要检查分析结果是否一致!]

[ There is actually an analytic method for solving the quartic (which is admittedly fairly involved), but as this is homework exercise, you presumably want the secant numerical method. For extra marks, you might want to check that the analytic results agree!]

我假设您已经选中了维基百科。它表示数值迭代是:

I assume you have already checked Wikipedia. It shows that the numerical iteration is:

x[n] = x[n-1] - f(x[n-1]) * (x[n-1] - x[n-2])/(f(x[n-1] - f(x[n-2]));

以适当选择的 x [0],x [1]

要很好地做迭代,你可能想抽象 f(x)。您可以使用函数指针,函子或抽象类。

To do the iteration nicely, you probably want to abstract f(x). You can use a function pointer, a functor or just an abstract class.

class Function
{
public:
    virtual double evaluate(double x) const = 0;  
};


double findRootUsingSecant(const Function& function, double x0, double x1);

传递实现 evaluate()通过计算你的公式,并实现上面的迭代确保终止迭代在一些合适的条件。

Pass in an instance of a class which implements evaluate() by computing your formula, and implement the above iteration. Make sure to terminate the iteration on some suitable conditions.

这篇关于帮助正确的根查找C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆