Jump to content

Determine Polynom's value given X

- - - - -

  • Please log in to reply
No replies to this topic

#1
gdjs

gdjs

    Newbie

  • Members
  • PipPip
  • 10 posts
I was rummaging through some old code of mine when I found this routine for determining a polynom's value given its degree, coeficients and a value for x (the variable). I hope it's useful to anyone:



// http://guipn.com/blog


#include <stdlib.h>

#include <stdio.h>

#include <math.h>


double evalpolynom(double x, unsigned degree, double *coefs)

{

	double result = 0;

	unsigned index;


	for (index = 0; index <= degree; index++)

		result += coefs[index] * pow(x, index);


	return result;

}


int main(void)

{

	double *pol = (double []) {50, -1, 2}; // 50 -x + 2x^2

	printf("f(8) = 180: %f\n", evalpolynom(8, 2, pol));

	return 0;

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users