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));
}
}
Comment