In: Computer Science
Create a C++ project to compute the following:
1. Numerical differentiation of x at a point
2. Numerical integration of 2 points of x
3. Finding a root between 2 points of x.
Let the function is given as 4x5+ 2x3+x-9
In your project, the user will choose what he or she wants to do. Maybe the user wants to do differentiation (choice 1)or integration (choice 2) or finding root (choice 3). In case of choice 1, the user will be asked to provide a value of x. In the case of choice 2 and 3, the user will be asked to provide 2 values.
As output, it will show the user the value of differentiation, or integration, or root respectively.
The solution has been written in C++ with the required choices. The root search has been done using bisection method. You may replace the method and the code would still work.
#include <bits/stdc++.h>
using namespace std;
//Fuction calculates derivtive of each term of the polynomial and returns it
long long derivativeTerm(int power,double coeff, long long val)
{
// For ax^n, we return a(n-1)x^(n-1)
if(power == 0)
{
return 0;
}
else
{
return coeff * power * pow(val, power - 1);
}
}
//Fucntion handles the differentiation of the function term by term
long long derivativeVal(double coeff[], int val,int size)
{
long long ans = 0;
for(int i = 0; i < size;++i)
{
ans += derivativeTerm(i,coeff[i],val);
}
return ans;
}
//Fuction calculates integral of each term of the polynomial and returns it
double integralTerm(int power,double coeff,int val1,int val2)
{
// For ax^n, we return a(n-1)x^(n-1)
return (double)(coeff/ (power+1)) * (pow(val2, power + 1)-pow(val1,power+1));
}
//Function handles the integration of the function term by term
double integralVal(double coeff[],int val1,int val2,int size)
{
double ans = 0;
for(int i = 0; i < size;++i)
{
ans += integralTerm(i,coeff[i],val1,val2);
}
return ans;
}
//function evaluater which returns the substituted function value
double func(double coeff[],double a,int size)
{
double ans = 0;
for(int i = 0; i < size;++i)
{
ans += (double)coeff[i]*pow(a,i);
}
return ans;
}
//function uses the bisection method to find the root between the numbers
void rootSearch(double coeff[],double a, double b,int size)
{
double EPSILON = 0.01;
if (func(coeff,a,size) * func(coeff,b,size) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}
double c = a;
while ((b-a) >= EPSILON)
{
// Find middle point
c = (a+b)/2;
// Check if middle point is root
if (func(coeff,c,size) == 0.0)
break;
// Decide the side to repeat the steps
else if (func(coeff,c,size)*func(coeff,a,size) < 0)
b = c;
else
a = c;
}
cout << "The value of root is : " << c;
}
// Driver code
int main()
{
//inputst the degree of the polynomial
int n;
cout<<"Enter the degree of the polynomial";
cin>>n;
n = n+1;
//inputs the coefficient of the polynomial
double coeff[n];
cout<<"Enter the coefficients of the polynomial starting from constant term to highest power:";
for(int i =0 ;i<n;++i)
{
cin>>coeff[i];
}
//Option prints and askes for the choice
while(true)
{
int choice;
cout<< "Enter your Choice\n\t1.Differentiate\n\t2.Integrate\n\t3.Find Root\n";
cin>>choice;
cout<<"This is your choice"<<choice<<endl;
switch(choice)
{
case 1: {
//Inputs the value of the point to calculate the derivative and passes to the function
int val;
cout<<"Enter the value of the point ";cin>>val;
cout <<"The value of the derivative is :"<<derivativeVal(coeff,val,n);
break;
}
case 2: {
//Inputs the value of the points to calculate the integral and passes to the function
int val1,val2;
cout<<"Enter the values of the point ";cin>>val1>>val2;
cout <<"The value of the derivative is :"<<integralVal(coeff,val1,val2,n);
break;
}
case 3: {
//Inputs the value of the points to find the roots and passes to the function
double val1,val2;
cout<<"Enter the values of the point ";cin>>val1>>val2;
rootSearch(coeff,val1,val2,n);
break;
}
case 4: return 0;
}
}
return 0;
}