【问题标题】:Newton method for computing an inverse计算逆的牛顿法
【发布时间】:2015-01-18 11:57:55
【问题描述】:

这是我在这个帖子中提出的问题:Link error missing vtable

我定义了一个类“函数”和另外两个类“多项式”和“仿射”,它们继承自“函数”。

class function {

        public:
            function(){};
            virtual function* clone()const=0;
            virtual float operator()(float x)const=0; //gives the image of a number by the function
            virtual function* derivative()const=0;
            virtual float inverse(float y)const=0; 
            virtual ~function(){}

        };

        class polynomial : public function {
        protected:
            int degree;
        private:
            float *coefficient;
        public:

            polynomial(int d);
            virtual~polynomial();
            virtual function* clone()const;
            int get_degree()const;
            float operator[](int i)const; //reads coefficient number i
            float& operator[](int i); //updates coefficient number i
            virtual float operator()(float x)const; 
            virtual function* derivative()const;
            virtual float inverse(float y)const; 

        };

        class affine : public polynomial {
            int a; 
            int b; 
            //ax+b
    public:
            affine(int d,float a_, float b_);
            function* clone()const;
            float operator()(float x)const;
            function* derivative()const;
            float inverse(float y)const;
            ~affine(){}
        };
polyomial 中的

方法 inverse 似乎无法正常工作。它基于牛顿法,应用于函数 x->f(x)-y 用于固定 y(我们正在计算逆的元素)和当前多项式 f。

float polynomial::inverse(float y)const
{
    int i=0;
    float x0=1;
    function* deriv=derivative();
    float x1=x0+(y-operator()(x0))/(deriv->operator()(x0));
    while(i<=100 && abs(x1-x0)>1e-5)
    {
        x0=x1;
        x1=x0+(y-operator()(x0))/(deriv->operator()(x0));
        i++;
    }

    if(abs(x1-x0)<=1e-5) 
    {
        //delete deriv; //I get memory problems when I uncomment this line
        return x1;   
    }

    else    
    {
        cout<<"Maximum iteration reached in polynomial method 'inverse'"<<endl;
        //delete deriv; //same here
        return -1;
    }
}

double polynomial::operator()(double x)const
{
    double value=0;
    for(int i=0;i<=degree;i++) value+=coefficient[i]*pow(x,i);
    return value;
}
polynomial* polynomial::derivative()const
{
    if(degree==0)
    {
        return new affine(0,0,0);
    }
    polynomial* deriv=new polynomial(degree-1);
    for(int i=0;i<degree;i++)
        deriv[i]=(i+1)*coefficient[i+1];   
    return deriv;
}

我用 p:x->x^3 测试这个方法:

#include "function.h"

int main(int argc, const char * argv[])
{

        polynomial p(3);
        for(int i=0;i<=2;i++) p[i]=0;
        p[3]=1;
        cout<<"27^(1/3)="<<p.inverse(27);



        return 0;
}

即使我输入 10,000 而不是 100,此脚本也会输出 27^(1/3)=Maximum iteration reached in polynomial method 'inverse' -1。我在 Internet 上阅读了一些文章,似乎这是一种计算逆的常用方法。

【问题讨论】:

  • 可能离题,但肯定与让您的代码正常工作有关;为什么要明确调用运营商?像运算符()(x0);这不是 C++ 中运算符重载的工作方式。您应该只调用 (x0) 来调用 () 运算符。
  • 我用箭头 (->) 显式调用运算符,因为'deriv' 是一个指针,而不是'多项式'的实例
  • 如果我不这样做,我会得到错误:调用的对象类型'function*'不是函数或函数指针
  • 好吧,我用(*deriv)(x0) 改变了对operator() 的显式调用。这一次,该方法返回值,奇怪的值。我已经在 polynomial::inverse 方法中声明 x1 之前放置了cout&lt;&lt;"Before while, (*deriv)(x0)="&lt;&lt;(*deriv)(x0)&lt;&lt;endl;,并且每次运行代码时都会得到 x0=1 不同的值(-2.31584e+77、1.49167e-154,...)。以防万一,我在我的问题中添加了 polynomial::derivative() 和 polynomial::operator()(double) 的定义。顺便说一句,我把我所有的变量都改成了双倍。它似乎更高效。

标签: c++ xcode4


【解决方案1】:

abs函数原型为:int abs(int) 所以像abs(x1-x0)&lt;=1e-5 这样的测试不会像你期望的那样运行;您将 int 与 float 进行比较。在这种情况下,float 将被转换为 int,因此它与 abs(x1-x0)&lt;=0 相同

这可能是您没有得到预期结果的原因 - 我建议添加更多打印输出以了解事情的真相。

【讨论】:

  • 可能值得一提的修复:使用std::abs,它为浮点类型重载,确保包含&lt;cmath&gt;。
  • 确实,我正在使用 并且通过阅读文档确定它考虑了浮点数。
  • @dada:但你叫它abs 而不是std::abs。除非您遇到了一些令人讨厌的命名空间污染,否则不合格的 abs 可能只引用 C 库版本,它只是整数。
  • 我要添加using namespace std;
【解决方案2】:

嗯,问题出在“导数”方法上。我没有使用我重新定义的 'operator[]',而是使用了 '->coefficient[]' 并且主脚本在 p.inverse(27) 上运行良好(仅 14 次迭代)。我刚刚用deriv-&gt;coefficient[i]=(i+1)*coefficient[i+1];替换了deriv[i]=(i+1)*coefficient[i+1];

【讨论】:

  • 但是我应该提到,当我以 x0=8 开始时,使用p.inverse(729) 运行主脚本会输出15.5884。它通常应该返回 9 ...
【解决方案3】:

检查此代码:

#include<iostream>
#include<cmath>
#include<math.h>

using namespace std;

void c_equation(int choose, double x);
void Processes(double x, double fx1, double fdx1, int choose);

void main()
{
    int choose,choose2;
    double x;
    system("color A");
    cout << "                                                             " << endl;
    cout << "=============================================================" << endl;
    cout << "Choose Equation : " << endl;
    cout << "_____________________________________" << endl;
    cout << "1- x-2sin(x)" << endl;
    cout << "2- x^2 + 10 cos(x)" << endl;
    cout << "3- e^x - 3x^2" << endl;
    cout << "                                                    " << endl;
    cin >> choose;
    cout << "If you have values  press 1/ random press 2 :" << endl;
    cin >> choose2;
    if (choose2 == 1)
    {
        cout << "                                         " << endl;
        cout << "Enter Xo : " << endl;
        cin >> x;
        c_equation(choose, x);
    }
    else if (choose2 == 2)
    {
        x = rand() % 20;
        cout << "Xo = " << x << endl;
        c_equation(choose, x);
        choose2 = NULL;
    }
    else
    {
        cout << "Worng Choice !! " << endl;
        choose = NULL;
        choose2 = NULL;
        main();
    }
}

void c_equation(int choose, double x)
{
    double fx;
    double fdx;
    double fddx;
    double result;
    if (choose == 1)
    {
        fx = x - 2 * sin(x);
        fdx = 1 - 2 * cos(x);
        fddx = 2 * sin(x);
        result = abs((fx * fddx) / pow(fdx, 2));
    }

    else if (choose == 2)
    {

        fx = pow(x, 2) + 10 * cos(x);
        fdx = 2 * x - 10 * sin(x);
        fddx = 2 - 10 * cos(x);
        result = abs((fx * fddx) / pow(fdx, 2));
    }

    else if (choose == 3)
    {
        fx = exp(x) - 3 * pow(x, 2);
        fdx = exp(x) - 6 * x;
        fddx = exp(x) - 6;
        result = abs((fx * fddx) / pow(fdx, 2));
    }
    else
    {
        cout << " " << endl;
    }
    //------------------------------------------------------------
    if (result < 1)
    {
        cout << "True Equation :) " << endl;
        Processes(x, fx, fdx , choose);
    }
    else
    {
        system("cls");
        cout << "False Equation !!" << endl;
        choose = NULL;
        x = NULL;
        main();
    }

}
void Processes(double x, double fx, double fdx , int choose)
{
    double xic;
    for (int i = 0; i < 3; i++)
    {
        xic = x - (fx / fdx);
        cout << "                                          " << endl;
        cout << "Xi = " << x << "   " << "F(Xi) = " << fx   << "   " << "  F'(Xi) = " << fdx   << "  " << "  Xi+1 = " << xic << endl;
        x = xic;
        if (choose == 1)
        {
            fx = xic - 2 * sin(xic);
            fdx = 1 - 2 * cos(xic);
        }
        else if (choose == 2)
        {
            fx = pow(xic, 2) + 10 * cos(xic);
            fdx = 2 * xic - 10 * sin(xic);
        }
        else if (choose == 3)
        {
            fx = exp(xic) - 3 * pow(xic, 2);
            fdx = exp(xic) - 6 * xic;
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-11
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2018-07-29
    • 2015-12-19
    相关资源
    最近更新 更多