Matlab's polyval and polyder in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redsox595
    New Member
    • Aug 2014
    • 3

    Matlab's polyval and polyder in C++

    Hello, I am trying to write functions that behave exactly as the polyval and polymer functions do in Matlab. My code is outputting the wrong solution and I am not sure where my errors are. Thank you

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    float polyval(float*, int, float);
    void polyder(float*, int, float*);
    
    int main() {
        
        int n;
        float x, dp;
        
        cout << "Enter polynomial order: ";
        cin >> n;
        float* p = new float[n+1];
        
        cout << "Enter coefficients, starting with the highest power: ";
        for (int i=0; i<n+1; i++) cin >> p[i];
    
        cout << "Enter x = ";
        cin >> x;
        
        cout << "Value = " << polyval(p, n, x) << endl;
        
        cout << "Derivative = " << dp << endl;
        
        return 0;
    }
    
    
    float polyval(float* p, int n, float x) {
        float px=0;
        
        for (int j=n; j>0; j--)
        {
            for (int i=0; i<n+1; i++)
            {
                px = p[i]*pow(x,j);
            }
        }
        return px;
    }
    
    
    void polyder(float* p, int n, float* dp) {
        dp={0};
        for(int i=0; (i=n); i++) {
            //dp += (n - i) * pow(p[i], ((n - i) - 1));
        }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    float polyval(float* p, int n, float x) {
        float px=0;
        
        for (int j=n; j>0; j--)
        {
            for (int i=0; i<n+1; i++)
            {
               [B] px = p[i]*pow(x,j); <<<<<????
    [/B]        }
        }
        return[B] px; <<<<????
    [/B]}
    appears to calculate a float px over and over and overstores the previous calculated value. It looks like the last calculation is returned.

    There may be other stuff but this code looks odd.

    Comment

    • redsox595
      New Member
      • Aug 2014
      • 3

      #3
      How could I fix it? I am new to C++ and I want px to be the value of the polynomial when evaluated at x.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You means like a summation?

        If so, you can code:

        Code:
        px = px + p[i]*pow(x,j);
        or you can use a combination operator that does the same thing:

        Code:
        px += p[i]*pow(x,j);

        Comment

        • redsox595
          New Member
          • Aug 2014
          • 3

          #5
          I have figured out the polyval function. Do you have any ideas as to how I could write a function to differentiate a polynomial of the form
          p(x) = p(1) * x^(n) + p(2) * x^(n-1) + p(3)*x^(n-2) + ... + p(n)*x + p(n-1)

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I'm not a math major but how does this look? I may not have it correctly.

            Code:
            double polyval(double* p, int n, double x) {
            	double px = 0.0;
            
            	for (int i = 0; i < n - 1; ++i)
            	{
            	px += p[i] * pow(x, n - i);    //
            	}
            
            	
            	return px;
            }

            Comment

            Working...