Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimized Polynomial
#1
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).
hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Reply
#2
Like this, you mean? http://www.mecca.org/~halfacre/MATH/binomialtheorem.htm

UBOUND gives the size of an array dimension.

Derivative function? Explain?
am an asshole. Get used to it.
Reply
#3
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.
hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Reply
#4
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???
B 4 EVER
Reply
#5
I think he is asking to find the roots, ie the x values which make the polynomial 0.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#6
Oh...ok...then I'll sit down and think about it...
B 4 EVER
Reply
#7
I should be good at this; I had to take the class twice! :-?

*peace*

Meg.
Reply
#8
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.
hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Reply
#9
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
Reply
#10
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())
This made your output double precision also.

Your times were 25.7 sec w/o FFIX and 3.4 sec w/ FFIX.
Very good Big Grin

Are you going to try the derivative?
hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)