Algorithm for standard derivative?

Status
Not open for further replies.

MrCatbert

Ars Tribunus Angusticlavius
9,107
Subscriptor
It rather depends on what you are trying to do. If you just want the value of the derivative at a point, the above method will work fine. Or you could do something more involved like Richardson extrapolation to get more accurate results. If you want to see what the derivative function looks like, you will have perform calculations over a range of values for x.

If you want more than that, you probably aren't talking about a "small" program anymore. :)
 

Tristram

Ars Scholae Palatinae
1,428
Subscriptor
IIRC, there is no best numerical derivative algorithm. Each of them is unstable for some inputs. Oddly, integration algorithms are usually stable, though often not fast.

Symbolic differention is a different matter, but that is best left to Maple / Mathematica.

Polynomial derivatives are pretty simple, though, given the right data structure. CAVEAT: I'm doing this off the top of my head, may be inaccuracies, forgotten boundry conditions. Also some grunt work methods need to be added (like copy constructors, etc.)

code:<HR>
Code:
class Term {
  Term(float c, int e) : coeff(c), exp(e) {};
  Term& derivative(){
     return Term(coeff * exp, e - 1);
  }
  public:
     float coeff;
     int exp;
};<P>class Polynomial {
  public:
    float evaluate(float x){
       // use horner's rule to evaluate
       return ...;
    }
    Polynomial(const vector<Term>&  p) : poly(p) {}
    Polynomial derivative(){
       vector<Term> v;
       for (vector<Term>::const_iterator i = poly.begin(); 
           i != poly.end(); ++i){
          v.push_back(i->derivative());
       }
       return Polynomial(v);
    }
  private:
    vector<Term> poly;
};
 
Status
Not open for further replies.