DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

polynomial evalutaion

I was just wondering if there were any built-in functions that will evaluate a polynomial for a given equation eg. (a*x^3 + b*x^2 + c*x + d) with a data set for x.  I can write one manually, but I just wanted to check to see if there was one already in Diadem that I was missing. 
 
Thanks
 
Wayne
0 Kudos
Message 1 of 8
(5,469 Views)
Hello Wayne,
 
Check out the command ChnApprXYCalc which you can run from the parameterization ANALYSIS > Curve fitting > Approximate.
Hit the button "Set up..." to specify the polynomial functions.
 
Christian
0 Kudos
Message 2 of 8
(5,458 Views)
I currently use the ChnApprXYCalc to get the polynomial function.  What I want to do is create the Y channel that corresponds to an existing X data set using the polynomial coefficients from an approximation.  I am trying to replicate the "Polyval" function in MATLAB that evaluates a polynomial defined by its coefficients for a specified X data set.
 
0 Kudos
Message 3 of 8
(5,453 Views)
Hi Wayne,

You might use the polyval function you mentioned in a Mathscript which you call directly in DIAdem. But to do this you need LabVIEW on your machine with the Mathscript option.

Ralf
0 Kudos
Message 4 of 8
(5,449 Views)
Hi Wayne,
 
Having already calculated the coefficients you can use the ChnCalcuate command to evaluate the polynomial:
 
Example:
 

Call

ApprAnsatzOff ' Deactivates all function terms

ApprAnsatzFct

(1) = "Yes" ' Activates function term constant

ApprAnsatzFct

(2) = "Yes" ' Activates function term x

ApprAnsatzFct

(3) = "Yes" ' Activates function term x^2

ApprAnsatzFct

(4) = "Yes" ' Activates function term x^3

Call

ChnApprXYCalc("[1]/Time","[1]/Speed","/ApproximatedX","/ApproximatedY","Partition complete area",100,1) '... XW,Y,E,E,XChnStyle,XNo,XDiv

Call

ChnCopy("[1]/Time", "x")

Call

GlobalDim("a0") : a0 = ApprAnsatzCoef(1)

Call

GlobalDim("a1") : a1 = ApprAnsatzCoef(2)

Call

GlobalDim("a2") : a2 = ApprAnsatzCoef(3)

Call

GlobalDim("a3") : a3 = ApprAnsatzCoef(4)

Call

ChnCalculate("Ch(""y"") = a0 + a1*Ch(""x"") + a2*Ch(""x"")^2 + a3*Ch(""x"")^3")

 
Christian
0 Kudos
Message 5 of 8
(5,436 Views)

Here is the subroutine that I came up with.  It is a generic routine that determines the order of the polynomial based on the number of coefficients sent to the routine. "x" is the channel name with the x values, and "yf" is the result of evaluating the polynomial.  The routine works correctly, but it is very slow.  Since this sub is called hundreds of times for each data set containing 500+ points that needs evaluating, I would appreciate any assistance in making it faster.  I tried to do the inner loop with Ch() notation, but I couldn't get the correct answer out. 

Thanks in advance for the help.

Wayne

sub PolyVal (c,x,yf)
'this sub will evaluate the polynomial with coeficients c at all values of x the result is in yf

Dim a,b,NumCoeff,length

NumCoeff = ubound(c)
length = ChnPropValGet(x,"Length")  

For a = 1 to length
  For b = 0 to NumCoeff
    Call FormulaCalc("CHD(" & a & ",'" & yf & "') :=  " & CStr(c(b)) & " * CHD(" & a & ",'" & x & "')^" & CStr(b) & " + CHD(" & a & ",'" & yf & "')")
  Next 'b
Next 'a


end sub

0 Kudos
Message 6 of 8
(5,384 Views)

Hi Wayne,

This is a really good suggestion.  We should make this easier than it is today in DIAdem 10.x or will be in DIAdem 11.  You don't need to call ChD() in the FormulaCalc() function-- it already implicitly loops over all the x values if you use the Ch() syntax.  I also prefer to reference the channels with DIAdem temporary variables (L1, L2), since I think that makes the formula expression much easier to read and debug.  Finally, you should build up the formula expression in the loop over the coefficients and then execute the resulting expression exactly once.  This should be blazing fast.

x  = "[1]/Time"
yf = "[1]/Fit of Speed"
c = Array(8.47495366905276, 2.28126534943276, 0.0230625271557868, -0.0021154305278767, 1.69053818072781E-05)
Call PolyVal(c,x,yf)


sub PolyVal(c,x,yf)
  Dim b
  IF NOT IsArray(c) THEN Exit Sub
  IF NOT UBound(c) >= 0 THEN Exit Sub
  L1 = CNo(x)
  L2 = CNo(yf)
  IF L1 = 0 OR L2 = 0 THEN Exit Sub
  FormulaTxt = "Ch(L2):= " & c(b)
  For b = 1 to ubound(c)
    FormulaTxt = FormulaTxt & " + " & c(b) & "*Ch(L1)^" & b
  Next 'b
  Call FormulaCalc(FormulaTxt)
end sub

Brad Turpin
DIAdem Product Support Engineer
National Instruments

0 Kudos
Message 7 of 8
(5,353 Views)
Brad,
 
Thanks so much for the help.  That made the processing of my data seconds as opposed to 30 minutes plus.  I do agree that a polynomial evaluation function builtin to Diadem would be very helpful.  In most of the applications I have worked on that has been a need.
 
Thanks again.
 
Wayne
0 Kudos
Message 8 of 8
(5,351 Views)