0

I already wrote the code to calculate the derivative of a single variable function at a point by making a class called "Der". In class Der, I defined two private variables double f and double df and a print() function to print the values of f and df. Inside the class, I overloaded the operators +, -, *, /, ^ to calculate the derivative of the sum, difference, multiplication etc of functions. I can't show the whole code because it's very long but I will show some snippets to give an idea.

class Der{
private:
    double f;  // function value at x
    double df; // derivative of function at x
public:
    Der();
    Der(double);
    Der operator+(Der); // f + g
    Der operator-(Der); // f - g
    Der operator*(Der); // f * g
    Der operator/(Der); // f / g
       
    friend Der operator+(double, Der); //c+f
    friend Der operator-(double, Der); //c-f
    friend Der operator*(double, Der); //c*f
    friend Der operator/(double, Der); //c/f
    Der operator^(double); // f^c,  Where c is constant
   
    friend Der sin(Der);
    friend Der cos(Der);
    friend Der tan(Der);
    friend Der log(Der);
    friend Der exp(Der);
    
    void print();
};

Der :: Der(){}

Der :: Der(double x){
    this->f = x;
    this->df = 1;
}

Der Der :: operator+(Der g){
    Der h;
    h.f = this->f + g.f;
    h.df = this->df + g.df;
    return h;
}

Der sin(Der g){
    Der h;
    h.f = sin(g.f);
    h.df = cos(g.f)*g.df;
    return h;
}
void Der :: print(){
    cout<<"Derivative of function at a point : "<<df<<endl;
}

int main()
    {
        Der x(10), f;
        f = x^2+x^3;
        f.print();
    }

Now I want to use this derivative calculator to calculate the partial derivative of a several variable function and ultimately to calculate the gradient of that function. I have some vague ideas but I am not able to implement it in code. I am a beginner in C++ programming, so it would be helpful if you don't use too many advanced concepts.

Any kind of help would be highly appreciated. Thanks!

Edit: I have added how Der is used. The program should take inputs of independent variables like x(2), y(4), z(5) and function like f(x,y,z)=x^2*y*z+log(x*y*z). Then, it will give the partial derivative of f w.r.t x, y, z at point (2, 4, 5) in the form of an array. But, I just need some idea on how to code the partial derivative calculator.

uuuuuuuuuu
  • 121
  • 5
  • 1
    At least show how `Der` is used, and what the usage of the partial derivative stuff would look like. Also, you need to show some attempt at code, or at least describe what you want more precisely than "I have some vague ideas...". – cigien Oct 19 '20 at 13:50
  • @cigien I have added how `Der` is used. The program should take inputs of independent variables like `x(2), y(4), z(5)` and function like `f(x,y,z)=x^2*y*z+log(x*y*z)`. Then, it will give the partial derivative of `f` w.r.t `x, y, z` at point (2, 4, 5) in the form of an array. But, I just need some idea on how to code the partial derivative calculator. – uuuuuuuuuu Oct 19 '20 at 14:05
  • This is better, but add all the info in your last comment to the question as well. – cigien Oct 19 '20 at 14:30
  • Also, it would be nice to see a `main` program that actually does this stuff (it won't compile obviously), but seeing the code usage is more helpful than a descritption. – cigien Oct 19 '20 at 14:34
  • @cigien I have added the main program. Do you want to see how the output looks like? – uuuuuuuuuu Oct 19 '20 at 14:35
  • No, I mean the `main` that you would write if the partial derivative code existed. – cigien Oct 19 '20 at 14:36
  • @cigien Ok, let me see. I haven't thought about that yet, because I till now i have no idea how to define all the classes. But I know I have to define one class as `class partial_derivative` and another one `class gradient`. I will surely provide the main function if I get some idea. – uuuuuuuuuu Oct 19 '20 at 14:44
  • @cigien Actually I have to make a gradient calculator of a function at some given point. The number of variables depends on the user. The input will be the no. of variables (say 2), the variables (say x, y) and the point (say (3, 6)) where the gradient has to be calculated. The output will be a single row matrix. – uuuuuuuuuu Oct 19 '20 at 15:00

1 Answers1

3

You seem to be very close to the solution, but struggling with the step to functions defined over multiple dimensions.

Instead of having one member variable df, you need several of them, one for each partial derivative. You could hard-code them:

double dfx, dfy, dfz;

or use a container:

double df[3];

I'll use hard-coding for now. (Containers are a vitally important topic, and a std::vector is better than an array in almost all respects, but one thing at a time.)

It would also be wise to rename the other variable from x to v, since it represents the value of the function at the point of interest, not the location of the point. (This is worth thinking about.)

The old constructor took one value and one derivative:

Der :: Der(double x){
    this->f = x;
    this->df = 1;
}

This can be better written using initializers:

Der :: Der(double nx): x(nx), df(1)
{}

which makes it easy to rewrite the constructor to take three partial derivatives:

Der :: Der(double nv, double dx, double dy, double dz): v(nv), dfx(dx), dfy(dy), dfz(dz)
{}

Now we can declare functions:

Der x(2, 1, 0, 0), y(4, 0, 1, 0), z(5, 0, 0, 1);

And the logic of the arithmetical operations is straightforward. For instance, the first partial of the product of two functions:

dfx = A.v * B.dfx + A.dfx * B.v;

(In fact, you could abstract the arithmetic out of your current class and use it for both old and new classes, but one thing at a time.)

Beta
  • 96,650
  • 16
  • 149
  • 150