# Qbasicnews.com

Full Version: Optimized Polynomial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Write a function that evaluates an nth order polynomial as efficiently as possible.
Input:
C(): an n dimensional coefficient array (SINGLE)
x : SINGLE

rules: Pure QB

Optional:
include a derivative function

(I know what I want to do but have not written it yet. There is a function in QB (atleast 7.1) that gives the dimension of an array. I don't remember where I read it and I don't remember the name. Does anyone else know it? If it is only 7.1, I won't use it).
Like this, you mean? http://www.mecca.org/~halfacre/MATH/binomialtheorem.htm

UBOUND gives the size of an array dimension.

Derivative function? Explain?
Ninkazu wrote:
Quote:UBOUND gives the size of an array dimension.
Thank you. I have the manual, but It is most helpful when you already know what you are looking for.

Ninkazu wrote:
Quote:Like this, you mean? http://www.mecca.org/~halfacre/MATH/binomialtheorem.htm
The binomial theorem is a special case. I was thinking more general. The function would do something like this
Code:
`P(x, C()) = C(n)*x^4 + C(n-1)*x^3 + . . . + C(0)`
Part of the problem is the function must determine the order (highest power) of the polynomial based on the size of the coefficient array, C(). That's where UBOUND comes in.

Ninkazu wrote:
Quote:Derivative function? Explain?
It's from calculus: the slope of a line tangent to a function at a given point. If the function is f(x), we could call the derivative dfdx. For the terms of a polynomial, the rules are simple:
Code:
```If f(x) = a*x^b,        dfdx = a*b*x^(b-1) if f(x) = k             dfdx = 0              (where k is a constant that does not depend on x)```
The derivative of the whole polynomial can be found through differentiating each term.
Code:
```Example: f(x)= 3x^2 - 5x + 4           dfdx = 3*2*x^(2-1) - 5x^(1-1) + 0               dfdx = 6x - 5```
I hope this isn't too much of a math lesson.
Something like that:

Code:
```DIM C(10) AS SINGLE DIM Value AS SINGLE DIM x     AS SINGLE CLS INPUT "Please Input X: ", x FOR n% = LBOUND(C) TO UBOUND(C)         C(n%) = INT(RND * 10) NEXT n% FOR n% = UBOUND(C) TO LBOUND(C) STEP -1         Value = Value + C(n%) * x ^ n% NEXT n% PRINT Value```

Or did I missunderstand the question???
I think he is asking to find the roots, ie the x values which make the polynomial 0.
Oh...ok...then I'll sit down and think about it...
I should be good at this; I had to take the class twice! :-?

*peace*

Meg.
Ak00ma was on the right track, but what I am looking for is a function. The obvious solution is
Code:
```DECLARE FUNCTION Pof(x!,C!()) DEFSNG A-Z FUNCTION Pof (x, C())   P = 0   for n =  0 to UBOUND(C)     P = P + C(n) * x^n       NEXT   Pof = P END FUNCTION```
But there are better ways to do it. The challenge is optimizing the performance of the function. Running this in my test program for 1000000 iterations with n = 4, it took 46.7 sec. w/o FFIX and 10 .1 Sec. w/ FFIX. I am looking for functions that will beat these times.
Let's me try

Code:
```FUNCTION Pof (x AS DOUBLE, c() AS DOUBLE)   DIM i AS INTEGER   DIM k AS INTEGER   DIM s AS DOUBLE   DIM xx AS DOUBLE   s = c(0)   k = UBOUND(c)   xx = 1#   FOR i = 1 TO k     xx = xx * x     s = s + c(i) * xx   NEXT i   Pof = s END FUNCTION```
I made one small change to your function. Since you were calculating at double precision, I changed
Code:
`FUNCTION Pof (x AS DOUBLE, C() AS DOUBLE)`
to
Code:
```DEFDBL A-Z FUNCTION Pof (x, C())```