//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;}
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.
#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);
elsereturn"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