Question

In: Computer Science

In c++ This question is relevant if you implemented a polynomial that included some calculus-based operations...

In c++

This question is relevant if you implemented a polynomial that included some calculus-based operations such as derivative and integral. Write a new function that meets the following specification:

double slope(const polynomial& p, double x)

// POSTCONDITION: The return value is equal to the slope of the

// polynomial p, evaluated at the point x.

Solutions

Expert Solution

Solution:

In this question, we have to write the function which returns the slope of a given polynomial. so, here we calculate the differential calculus.
The formula for differential calculus is as follows:

If f(x) = axn then, f'(x) = f(x) dx = a*nxn-1 where n>=0 and a is coefficient (a>=1) .

Suppose f(x) = 2x2+4x. and x =2.

Then the slope will be f'(x) = 2x2 dx + 4x dx
f'(x) = 2*2 x2-1 + 4x1-1
f'(x) = 4x + 4
put x = 2,
f'(2) = 4 * 2 + 4 = 8 + 4 = 12. (Slope of the polynomial) .  

C++ Function:

// the term_calulate function calculate the derivative of each term of polynomial.

double term_calulate(string pTerm, double val)
{

   // Get coefficient
   string coeffStr = "";
   int i;
   for (i = 0; pTerm[i] != 'x'; i++)
       coeffStr.push_back(pTerm[i]);
   double coeff = atol(coeffStr.c_str());

   // Get Power after skipping 2 characters for x and ^
   string powStr = "";
   for (i = i + 2; i != pTerm.size(); i++)
       powStr.push_back(pTerm[i]);
   double power = atol(powStr.c_str());

   // For ax^n, we return a(n-1)x^(n-1)
return coeff * power * pow(val, power - 1);
}

// The slope function takes the polynomial and value of point x as input and return the slope value as output.

double slope(string& poly, double x)
{
   double ans = 0.0;

   // Use istringstream to get input in tokens
istringstream is(poly);

   string pTerm;
   while (is >> pTerm) {

       // If the token is '+' then continue with the string
       if (pTerm == "+")
           continue;

       // Else find the derivative of particular term
       else
           ans = (ans + term_calulate(pTerm, x));
   }
   return ans;
}


Here we use the 2 functions the first function calculates the derivative of each term and the second one calculate the slope of the polynomial.


Related Solutions

Show that any polynomial over C (the complex numbers) is the characteristic polynomial of some matrix...
Show that any polynomial over C (the complex numbers) is the characteristic polynomial of some matrix with complex entries. Please use detail and note any theorems utilized.
c++ please Your task is to write the implementation for a class of polynomial operations. Your...
c++ please Your task is to write the implementation for a class of polynomial operations. Your will write the code for: 2 constructors, a destructor, print, addition, multiplication , differentiation and integration of polynomials. The polynomials will be comprised of linked TermNodes. struct TermNode { int exp; // exponent double coef; // coefficient TermNode * next; }; class Polynomial { public: Polynomial (); // default constructor Polynomial (int r, int c); // constructor makes a 1 node polynomial Polynomial(const Polynomial...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with a fixed-front approach as opposed to a floating-front approach.
** Please answer the question without using Calculus. This is an algebra based Physics course. Thank...
** Please answer the question without using Calculus. This is an algebra based Physics course. Thank you! ** a) Over 2 millenniums ago, intellectuals in the ancient world concluded the earth's radius. This determined value is about RE = 6400 km. More than 70 years after Isaac Newton formulated his law of universal gravity, Cavendish made a direct measurement of the gravitational constant. This value is about G = 6.7 x 10-11 Nm2/kg2. Show how to calculate the mass of...
From calculus, you know that the derivative and integral are inverse operations; one undoes the effect...
From calculus, you know that the derivative and integral are inverse operations; one undoes the effect of the other. Prove that the first difference and the running sum are also inverse operations. That is, show that the cascade of these two systems is identical to the delta function. Explain in detail?
Part (c): An implementation of division You are familiar with integer division operations. In some processors...
Part (c): An implementation of division You are familiar with integer division operations. In some processors there is no division instruction. One way to implement division is via repeated subtractions. NB: This is integer division, NOT floating point division. For example, consider the expression M / N where M = 370 and N = 120 . If we repeatedly subtract until we have a value less than N , then the number of times we do the subtraction is the...
The C++ question: This part of the assignment will give you a chance to perform some...
The C++ question: This part of the assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related to each other. Start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one....
Question 2 With relevant example of your choice, explain the important framework for managing operations used...
Question 2 With relevant example of your choice, explain the important framework for managing operations used by production firm
This is an assignment for a C++ introduction class. This module included if statements. If you...
This is an assignment for a C++ introduction class. This module included if statements. If you have any questions please feel free to let me know! { In this module you learned about making decisions in C++ and how to combine decision making with the material from the first few modules to solve problems. For this assignment, write a program that calculates a discount for buying certain quantities of coffee. Consider the following scenario: A coffee company sells a pound...
Please give some specific explanation why "Activity Based Costing is not relevant and too difficult to...
Please give some specific explanation why "Activity Based Costing is not relevant and too difficult to implement in the service industry"?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT