Question

In: Computer Science

Create a C++ project to compute the following: 1. Numerical differentiation of x at a point...

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.

Solutions

Expert Solution

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; 
} 

Related Solutions

1 point) If xx is a binomial random variable, compute P(x)P(x) for each of the following...
1 point) If xx is a binomial random variable, compute P(x)P(x) for each of the following cases: (a)  P(x≤1),n=5,p=0.3P(x≤1),n=5,p=0.3 P(x)=P(x)= (b)  P(x>3),n=4,p=0.1P(x>3),n=4,p=0.1 P(x)=P(x)= (c)  P(x<3),n=7,p=0.7P(x<3),n=7,p=0.7 P(x)=P(x)=
Create a memory diagram for the following C program when the program reaches point 1. void...
Create a memory diagram for the following C program when the program reaches point 1. void foo (int *a); int bar(int *x); int search(int *c); int main(void) { int q, p=10; q= search(&p); return 0; } void foo(int *a) { *a += 2; //point 1 *f *= 10; } int bar(int *x) { return 2* *x; } int search(int *c) { int z= *c + bar(c); foo(c); return z; }
C++ Project Organization Create a project called Project1 and then name and organize the following programs...
C++ Project Organization Create a project called Project1 and then name and organize the following programs under it. Part 1 CircleArea You are going to write a program to compute and output the area of a circle with a radius of 2.5. Think through what you need to do: Create a variable for radius set it to 2.5 Create a variable for area set it to 3.14159 * radius * radius output the value of area Challenge (not required) Generalize...
f(x) = xln(x+1) Find the Taylor polynomal to n=4 at the point c =1
f(x) = xln(x+1) Find the Taylor polynomal to n=4 at the point c =1
1. If f(x) = ln(x/4) -(a) Compute Taylor series for f at c = 4 -(b)...
1. If f(x) = ln(x/4) -(a) Compute Taylor series for f at c = 4 -(b) Use Taylor series truncated after n-th term to compute f(8/3) for n = 1,.....5 -(c) Compare the values from above with the values of f(8/3) and plot the errors as a function of n -(d) Show that Taylor series for f(x) = ln(x/4) at c = 4 represents the function f for x element [4,5]
1. Compute the NPV of a project with the following information. The IRR is 9%. The...
1. Compute the NPV of a project with the following information. The IRR is 9%. The project life is 3 years. The initial cost is $19,000. In years 1 and 2 the cash inflows are $4,500 and $5,500, respectively. Cash flow in year 3 is missing. WACC is 12%.
1.      Compute the IRR for Project X and note whether the firm should accept or reject...
1.      Compute the IRR for Project X and note whether the firm should accept or reject the project with the cash flows shown as follows if the appropriate cost of capital is 9 percent. Time: 0 1 2 3 4 5 Cash flow: ?1,000 ?75 100 100 0 2,000 A.      9 PERCENT, ACCEPT B.       9 PERCENT, REJECT C.       16.61 PERCENT, ACCEPT D.      16.61 PERCENT, REJECT
f(x), find the derivative (f^-1)'(c) at the given point c, first finding a=f^-1(c) 1. f(x)=5x+9x^21. c=-14...
f(x), find the derivative (f^-1)'(c) at the given point c, first finding a=f^-1(c) 1. f(x)=5x+9x^21. c=-14 a=? (f^-1)'(c)= ?
In C++ 1.Create a class Point which has a template parameter of the type of internal...
In C++ 1.Create a class Point which has a template parameter of the type of internal data, T, and a template parameter for the dimension of the Point(2D, 3D etc.). Store a statically allocated, internal array of type T with dimension n. Ensure to include any constructer(s),destructors, getters or setters you might need. (10 points) 2.Create a template function which computes the Euclidean distance between 2 points. (6 points) 3.Instantiate two Point<double, 3> and compute their distance. Instantiate two Point<int,...
1. Create a numerical example of a permanent tax difference and how it is treated on...
1. Create a numerical example of a permanent tax difference and how it is treated on the Company’s books and the tax (IRS) books?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT