473,388 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. float polyval(float*, int, float);
  6. void polyder(float*, int, float*);
  7.  
  8. int main() {
  9.  
  10.     int n;
  11.     float x, dp;
  12.  
  13.     cout << "Enter polynomial order: ";
  14.     cin >> n;
  15.     float* p = new float[n+1];
  16.  
  17.     cout << "Enter coefficients, starting with the highest power: ";
  18.     for (int i=0; i<n+1; i++) cin >> p[i];
  19.  
  20.     cout << "Enter x = ";
  21.     cin >> x;
  22.  
  23.     cout << "Value = " << polyval(p, n, x) << endl;
  24.  
  25.     cout << "Derivative = " << dp << endl;
  26.  
  27.     return 0;
  28. }
  29.  
  30.  
  31. float polyval(float* p, int n, float x) {
  32.     float px=0;
  33.  
  34.     for (int j=n; j>0; j--)
  35.     {
  36.         for (int i=0; i<n+1; i++)
  37.         {
  38.             px = p[i]*pow(x,j);
  39.         }
  40.     }
  41.     return px;
  42. }
  43.  
  44.  
  45. void polyder(float* p, int n, float* dp) {
  46.     dp={0};
  47.     for(int i=0; (i=n); i++) {
  48.         //dp += (n - i) * pow(p[i], ((n - i) - 1));
  49.     }
  50. }
  51.  
Aug 10 '14 #1
5 5551
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. float polyval(float* p, int n, float x) {
  2.     float px=0;
  3.  
  4.     for (int j=n; j>0; j--)
  5.     {
  6.         for (int i=0; i<n+1; i++)
  7.         {
  8.             px = p[i]*pow(x,j); <<<<<????
  9.         }
  10.     }
  11.     return px; <<<<????
  12. }
  13.  
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.
Aug 10 '14 #2
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.
Aug 11 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
You means like a summation?

If so, you can code:

Expand|Select|Wrap|Line Numbers
  1. px = px + p[i]*pow(x,j); 
or you can use a combination operator that does the same thing:

Expand|Select|Wrap|Line Numbers
  1. px += p[i]*pow(x,j); 
Aug 11 '14 #4
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)
Aug 11 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
I'm not a math major but how does this look? I may not have it correctly.

Expand|Select|Wrap|Line Numbers
  1. double polyval(double* p, int n, double x) {
  2.     double px = 0.0;
  3.  
  4.     for (int i = 0; i < n - 1; ++i)
  5.     {
  6.     px += p[i] * pow(x, n - i);    //
  7.     }
  8.  
  9.  
  10.     return px;
  11. }
Aug 12 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: not telling | last post by:
There are two functions in matlab. Polyfit and polyval. I am looking for C/C++ code to duplicate their functionality. TIA Brian.
1
by: Hiddo | last post by:
Hello, I'm looking for the C++ code of the MatLab functions polyfit and polyval. Does anyone know where to find it? Hiddo hiddo@mailcity.comNOSPAMPLEASE
15
by: nikie | last post by:
I'm a little bit stuck with NumPy here, and neither the docs nor trial&error seems to lead me anywhere: I've got a set of data points (x/y-coordinates) and want to fit a straight line through...
2
by: Binod Pant | last post by:
Does any one know of a way to call MATLAB from PHP/Perl? I found the Perl module Math:MATLAB , but it seems not to work on Windows, and it's not easy to install and use. This would enable one to...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.