Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Polynomial Function

I am using 30 day eval version of Measurement Studio . The problem is that I am getting different output from the matlab polyfit. I am using PolynomialFit function. 

I am giving the following input -   

Dim xData() As Double = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0}   Dim yData() As Double = {1.0, 2.7, 7.4, 150.0, 80.0, 220.0}   Dim temp() As Double = NationalInstruments.Analysis.Math.CurveFit.PolynomialFit(xData, yData, 3, NationalInstruments.Analysis.Math.PolynomialFitAlgorithm.LU)  

And getting the following output- 

-4.2809523809519066  8.55476190476141  36.790476190475822  79.509523809524  135.79523809523855   204.73095238095217 

Where matlab code produces the following output -  

-0.15278   8.15833   4.83016  -4.28095 

Also the length of the output is  equal to the input lent where as it should be =order + 1.

 

Thanks,

Arnab

0 Kudos
Message 1 of 5
(4,064 Views)

Hello Arnab -

 

The output you are seeing is the fitted data.  If you would like to get the coefficients back, you should use one of the overloaded methods that contains the output parameter coefficients.  Once you do this, your coefficients should match up.

 

Please let us know if you have any more questions.

 

NickB

National Instruments 

0 Kudos
Message 2 of 5
(4,052 Views)

Nick,

 

Thanks for your help.Can you give me a sample code using the overloaded methods that contains the output parameter coefficients ? 


Thanks,

Arnab

0 Kudos
Message 3 of 5
(4,025 Views)

Hey Arnab,

 

No problem - here's a short snippet, let me know if you have any further questions:

 

 

double[] xData = { 0.0d, 1.0d, 2.0d, 3.0d, 4.0d, 5.0d };
double[] yData = { 1.0d, 2.7d, 7.4d, 150.0d, 80.0d, 220.0d };

// here, coeffs is the variable that will contain the data
// that you are looking for

double[] coeffs, fittedData;
double mse;

fittedData = CurveFit.PolynomialFit(xData, yData, 3, PolynomialFitAlgorithm.LU, out coeffs, out mse);

 

Just for future reference, we do have code snippets for most of our analysis methods in our documentation.  In this case, the most complex overload

 

 

public static double[] PolynomialFit(double[], double[], double[], int, PolynomialFitAlgorithm, int[], double[], out double[], out double);

 

 

has the code snippet.  I've found that the easiest way to find this documentation is to use the index to search for the method name in question.  This will bring up a page of all the available overloads, and generally the most complex overload will have a snippet for your inspection.

 

NickB

National Instruments 

0 Kudos
Message 4 of 5
(4,022 Views)

Nick,

 

Thanks a lot for your help.I now get my required coefficient.

 

Thanks,

Arnab

0 Kudos
Message 5 of 5
(4,008 Views)