是关于非线性最小二乘拟合的问题
在vc的dll中,我这样用
int __stdcall NonLinearFit (double x[], double y[], double z[], int n, ModelFun *modelFunction, double coef[], int ncoef, double *MSE);
typedef double CVICALLBACK ModelFun(double x, double *coef, int ncoef);
在vb公共模块中:
Public Declare Function NonLinearFit Lib "LinkVC.dll" (x As Double, y As Double, z As Double, ByVal n As Long, lpFunc As Any, a As Double, ByVal nCoef As Long, mse As Double) As Long
Public Function NLFitFunc(ByVal x As Double, a() As Double, ByVal nCoef As Long) As Double
Dim y As Double
'ReDim a(1 To nCoef) As Double
y = a(0) * Exp(x) + a(1) * Exp(-x)
NLFitFunc = y
End Function
vb窗体中:
Private Sub Command6_Click()
Dim x(1 To 20) As Double, y(1 To 20) As Double, z(1 To 20) As Double
Dim nCoef As Long, n As Long
Dim a(1 To 2) As Double, mse As Double
On Error Resume Next
n = 20
nCoef = 2
For i = 1 To n
x(i) = i
y(i) = 1# * Exp(x(i)) + 2# * Exp(-x(i)) + 0.1 * Rnd
Next i
a(1) = 0.9
a(2) = 1.85
status = NonLinearFit(x(1), y(1), z(1), n, AddressOf NLFitFunc, a(1), nCoef, mse)
End Sub
不知怎么的,就是出问题