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 c++ program to compute the product of two integers. call create the following functions:...
Create a c++ program to compute the product of two integers. call create the following functions: 1. getNum - to accept only positive numbers && will call computeProd. 2.computeProd - to compute the product of the numbers & will call the function displayProduct. 3. displayProduct - to display the product. Call the function getNum in the main function. Compute the product w/o using the multiplication operator(*). using #include <iostream> only
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
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; }
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
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...
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]
C++ Project 1 For this project, we are going to create our own classes and use...
C++ Project 1 For this project, we are going to create our own classes and use them to build objects. For each class you will create an *.h and *.cpp file that contains the class definition and the member functions. There will also be one file called 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. You will create 2 new classes for this project, one will be from the list below and one...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT