Double derivative by c++

//How to find the value of 2nd derivative.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int po,p,mul;
int x1,x2,x3,diff=0,x,n=1;
cout<<("enter the value of x at which you want to calculate derivative = ")<<endl;
cin>>x;
for(;;)
{
cout<<("enter power of ") <<n<< (" term = ")<<endl;
cin>>p;
cout<<("enter multiplier of ")<<n<<(" term = ")<<endl;
cin>>mul;
n=n+1;
if(p==0 )
{
break;
}
else
{
po=p-2;
x1=p*mul;
x2=pow(x,po);
x3=x2*x1;
diff=diff+x3;}}
cout<<"the diffrential of polinomial is = "<<diff;
return 0;}
Last edited on
The 2nd derivative of polynomial axn is n(n-1)xn-2 ( for n<2 is zero ).
You should apply this calculation formula to each parts of the following equation:
a1xn + a2xn-1 + ... + an-1x + an

C++ speaking, you should collect both degree of polynomial and coefficients an,..,a1. Then calculate the 2nd derivative form of polynomial which reduces its degree by two. Finally calculate the value.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

using Poly = vector<double>;

Poly deriv( Poly p )
{
   if ( p.size() <= 1 ) return Poly{ 0 };
   for ( int i = 1; i < p.size(); i++ ) p[i-1] = i * p[i];
   p.resize( p.size() - 1 );
   return p;
}

double evaluate( const Poly &p, double x )
{
   double result = 0.0;
   for ( int i = p.size() - 1; i >= 0; i-- ) result = p[i] + x * result;
   return result;
}

void write( const Poly &p )
{
   cout << p[0];
   for ( int i = 1; i < p.size(); i++ ) if ( p[i] ) cout << ( p[i] > 0 ? " + " : " - " ) << abs( p[i] ) << "x^" << i;
}

int main()
{
   Poly y = { 2, -4, 1, 3 };    // 2 - 4x + x^2 + 3x^3
   Poly Dy = deriv( y );
   Poly D2y = deriv( Dy );

   double x = 3;
   cout << "x = " << x << '\n';
   cout << "Original poly:     ";   write( y   );   cout << " = " << evaluate( y  , x ) << '\n';
   cout << "First derivative:  ";   write( Dy  );   cout << " = " << evaluate( Dy , x ) << '\n';
   cout << "Second derivative: ";   write( D2y );   cout << " = " << evaluate( D2y, x ) << '\n';
}

x = 3
Original poly:     2 - 4x^1 + 1x^2 + 3x^3 = 80
First derivative:  -4 + 2x^1 + 9x^2 = 83
Second derivative: 2 + 18x^1 = 56
struct's come in handy too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

struct Term // ax^n
{
    int a{0};
    int n{0};
    
    Term n_th_derivative(int level = 1)
    {
        Term result{a,n};
        int N = n;
        
        for(int i = 0; i < level; i++)
        {
            result.a *= N;
            
            N--;
            result.n = N;
        }
        return result;
    }
    
    std::string display()
    {
        if(a != 0)
            return std::to_string(a) + " x^ " + std::to_string(n);
        else
            return "Zero";
    }
};

int main()
{
    int a{0}, n{0};
    
    std::cout
    << "A single term has the form aX^n\n"
    << "Enter a: ";
    std::cin >> a;
    
    std::cout
    << "Enter n: ";
    std::cin >> n;
    
    int d{2};
    Term term{a,n};
    std::cout
    << "The second derivative of " << term.display() << " is "
    << term.n_th_derivative(d).display() << '\n';
    
    return 0;
}


A single term has the form aX^n
Enter a: 9
Enter n: 5
The second derivative of 9 x^ 5 is 180 x^ 3
Program ended with exit code: 0
And as an alternative to <vector>'s and subject to tidy-up terms can be arrayed to become equations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

struct Term // ax^n
{
    int a{0};
    int n{0};
    
    Term n_th_derivative(int level = 1)
    {
    Term result{a,n};
    int N = n;
    
    for(int i = 0; i < level; i++)
        {
        result.a *= N;
        
        N--;
        result.n = N;
        }
    return result;
    }
    
    std::string display()
    {
    if(a != 0 and n !=0)
        return std::to_string(a) + " x^ " + std::to_string(n);
    else
        return std::to_string(a);
    
    return "Zero";
    }
};

int main()
{
    Term equation[]{ Term{2,0}, Term{-4,1}, Term{1,2}, Term{3,3} };
    int no_terms = sizeof(equation)/sizeof(Term);
    
    for (int i = 0; i < no_terms - 1; i++)
    std::cout << equation[i].n_th_derivative(2).display() << " + ";
    
    std::cout << equation[no_terms - 1].n_th_derivative(2).display() << " = ";
    
    return 0;
}
Topic archived. No new replies allowed.