Question

Implement a function double polynom(double x, const vector<double> & a); that computes the value of the...

Implement a function

           double polynom(double x, const vector<double> & a);

that computes the value of the polynomial functionfn(x)=a0 +a1x+···+anxn.

Here the input vector<double> a contains the coefficients of the polynomial function and the value of n is determined by the size of a.

can you solve this for c++? thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
double polynom(double x, const vector<double> & a){
   double sum = 0;
   if(a.size() > 0){
      sum += a[0];
   }
   for(int i = 1;i<a.size();i++){
      sum = sum + (a[i]*x);
      x = x * x;
   }
   return sum;
}

Add a comment
Know the answer?
Add Answer to:
Implement a function double polynom(double x, const vector<double> & a); that computes the value of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Newton's method in Python The polynomial function Implement the polynomial function. The function computes where ....

    Newton's method in Python The polynomial function Implement the polynomial function. The function computes where . def poly(x, a, b, c, d): # # complete # pass The derivative of the polynomial Implement the derivative function given by . def poly_der(x, a, b, c, d): # # complete # pass Newton's method Implement Newton's method by updating the solution guess iteratively. The function returns a tuple of . def newtons_method(a, b, c, d, n, x0): # # complete # pass...

  • Problem1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to...

    Problem1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to its Tavlor series expansion: (-1)"r2n (2n)! x2 x4x6x8x10 cos(x)-Σ 1 This series produces the exact value of cos(x) for any real number x, but contains an infinite number of terms. Obviously, a computer program can compute only a finite number of terms. Thus, you will have to truncate the infinite series in (1). Your program should be able to do so in two different...

  • Use only C++ for all function Introduce a call-by-value function that computes the volume of a...

    Use only C++ for all function Introduce a call-by-value function that computes the volume of a box. Hint: Length, width, and height of a box is needed. Introduce a call-by-reference function with void output. that computes the square of an integer. Please double check the value of the function input before and after function call. Introduce a call-by-pointer function with void output. that computes the square of an integer. Please double check the value of the function input before and...

  • C++ implement the function: vector<int> level_traversal(const BinaryNode* root) Add each node's data to a vecotr and...

    C++ implement the function: vector<int> level_traversal(const BinaryNode* root) Add each node's data to a vecotr and return that vector Given BinaryNode.h: typedef T; struct BinaryNode{ T elem; BinaryNode* left; BinaryNode* right; BinaryNode( T d, BinaryNode* l =NULL, BinaryNode *r = NULL):elem(d) ,left(1), right(r){}; }; need to implement that function in level_traversal.h file thank u !

  • implement a C++ class name Vector. The Vector class contains a private double array of length...

    implement a C++ class name Vector. The Vector class contains a private double array of length 2 named elements that represents a two-dimensional vector. We will also implement an overloaded multiplication operator (*) that accepts a single Vector variable by reference and returns the Dot Product between the current Vector object and the one pointed to by the function argument in form of a double. Please recall that the dot product of two vectors a =(21,92) and 5 = (b1,b2)...

  • C++ Question Polynomial Calculator (Will Rate All Answers) Hello, this question will be divided into many...

    C++ Question Polynomial Calculator (Will Rate All Answers) Hello, this question will be divided into many parts, this is the second part. Will quickly rate your answer! Thank you. First part is here : https://www.chegg.com/homework-help/questions-and-answers/c-question-polynomial-calculator-rate-answers-hello-question-divided-many-parts-s-first-pa-q39525382 This question has to do with operators and polynomials. - Scalar Polynomial Multiplication: const Polynomial operator* (const Polynomial& P, double z) The scalar multiplication P * z simply multiplies each coefficient in p with z as depicted in the following example: 0.5 (3.2x5 + 1.6x2...

  • Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main...

    Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main function..so can take it from text file.... or 2) make main function with user input for all methods mentioned in the program. import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class Polynomial { //array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. double coefficients[]; static int size; //fube the following...

  • (a) Write a recursive function int find(const int A[], int n, int x); which returns the...

    (a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....

  • I'm not allowed to use polyfit or polyval functions. The hint the homework gives is setting...

    I'm not allowed to use polyfit or polyval functions. The hint the homework gives is setting up a linear system for the polynomial coefficients and solving it. The test case x = [1,2,3,4] y = [5,-2,3,0] x_dot = [1.5,2.5] should result in y_dot = -1.2500 0.2500 p = -3.333 26.0000 -61.6667 44.0000 Thanks to anyone who can answer this within the next day or so! 1 Interpolation methods 1.1 The Lagrange polynomial function (y dot,p] = my Lagrange (x, y,x_dot)...

  • Use a double for-loop to implement an OR function in python that can combine two Boolean...

    Use a double for-loop to implement an OR function in python that can combine two Boolean NumPy matrices. (Do not use built-in operators such as | or +). The function should work for one-dimensional and two-dimensional matrices and it should error check to verify input matrix compatibility. Only matrices of the same size can be OR-ed together, if the input to the function is two incompatible matrices, the function should return an error message (using return, not print).

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT