I have the following code in Matlab:
fileID = fopen('input.txt','r'); formatSpec = '%f'; y = fscanf(fileID,formatSpec); step = 0.1; x0 = step; xn = length(y)*0.1; x = x0:step:xn; fitfunc = 'a + exp(x/b)+x^2/3+x' startPoints = [-1 -1]; [f2 f2_info] = fit(x',y,fitfunc, 'Start', startPoints) disp('Coefficients values: '); coeffvalues(f2) disp('Forecasts value on 600s: '); f2(600) This code performs a data fitting and predicts the process for 600 seconds.
The task is as follows: this code needs to be inserted into the device, so I need to convert this code into C ++ code. I see several solutions:
- Search for automatic MATLAB code converters to C ++ code.
- Find in C ++ ready-made fitting libraries, and using them, rewrite the code.
- To program the iterative fitting process yourself, for this you have to immerse yourself in theory.
Option 1: tried using Matlab Coder, which automatically converts the code to C. But, unfortunately, Matlab Coder does not support the fit function.
Option 2 is eliminated, because The use of third-party libraries requires a lot of memory, which the device is not enough for such frills.
There is only 3 options, but I don’t understand the theory, got into the Matlab code, there is a very complex object-oriented language (I looked at the source code of the fit function), I do not have enough competence to understand this.
Then I asked a question at enSO , where people suggested that the least squares method (OLS) is most likely used.
Google English and English-speaking Internet, but there are mainly analytical solutions, and then for the polynomials. In analytical solutions there is a minus: if you change the function of the fitting, then you need to recalculate the derivatives each time and fill in the code. Therefore, I need numerical solutions so that interpolation can be quickly tested using various functions.
Then, it seems, I found what I need: the Levenberg-Marquardt method. I read the articles on Wikipedia and on machinelearning.ru, but I can’t figure out how to practically apply it.
Maybe there are knowledgeable people who could briefly describe the algorithm of the Levenberg-Marquardt method for my function fitfunc = 'a + exp (x / b) + x ^ 2/3 + x'? Or advise any other numerical method to search for the coefficients of the fitting nonlinear function.