Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

NonLinearFit - ModelFunctionCallBack

Dear Sir/Madam,

I would be grateful for a few lines of code that illustrates how to use the NonLinearFit method in VB.NET - In particular, the ModelFunctionCallBack part.

Sincerely yours,

Johan
0 Kudos
Message 1 of 5
(3,941 Views)
There is a NonLinearFit example that ships with Measurement Studio that demonstrates this. You can find this example in your Measurement Studio installation directory in the DotNET\Examples\Analysis\NonLinearFit\VB folder.

- Elton
0 Kudos
Message 2 of 5
(3,931 Views)
Thanks for the reply, but the NonLinearFit example does actually not exist in the Measurement Studio installation directory on my computer. I am using MS Enterprise Edtn. 7.0. Could it be that the mentioned example is only included in Version 7.1?

/Johan
0 Kudos
Message 3 of 5
(3,922 Views)
Sorry, you're right - this is a new example in Measurement Studio 7.1. I would post the example, but it uses new controls and features from Measurement Studio 7.1, so you wouldn't be able to build or run it. If you just want to see some example code that uses NonLinearFit and the ModelFunctionCallback, though, here is the relevant code from the example:


Private Sub GenerateDataToFit(ByVal numSamples As Integer, ByVal noiseAmplitude As Double, ByRef xData As Double(), ByRef yData As Double())
ReDim xData(numSamples - 1)
ReDim yData(numSamples - 1)


Dim noise As WhiteNoiseSignal = New WhiteNoiseSignal(noiseAmplitude, 0)

Dim noiseData As Double() = noise.Generate(numSamples, numSamples)

For x As Integer = 0 To xData.Length - 1
xData(x) = x
yData(x) = System.Math.Exp(-0.1 * x) + 2.0 + noiseData(x)
Next
End Sub

Private Sub OnCalculate(ByVal sender As Object, ByVal e As EventArgs) Handles calculateButton.Click
Dim numSamples As Integer = numberOfSamples.Value
Dim noiseAmplitude As Double = noiseLevel.Value
Dim xData As Double()
Dim yData As Double()
Dim coefficients As Double() = {2, 0, 4}

GenerateDataToFit(numSamples, noiseAmplitude, xData, yData)
scatteredDataPlot.PlotY(yData)

Dim mse As Double
Dim callback As ModelFunctionCallback = New ModelFunctionCallback(AddressOf ModelFunction)
Try
Dim fittedData As Double() = CurveFit.NonLinearFit(xData, yData, callback, coefficients, mse)
fitDataPlot.PlotY(fittedData)
Catch exp As Exception
MessageBox.Show(exp.Message)
End Try
End Sub

Private Function ModelFunction(ByVal x As Double, ByVal a As Double()) As Double
Return (a(0) * System.Math.Exp(a(1) * x)) + a(2)
End Function


- Elton
0 Kudos
Message 4 of 5
(3,895 Views)
Thanks for the help - all clear now.

/Johan
0 Kudos
Message 5 of 5
(3,885 Views)