Search code examples
matlabcurve-fittingmontecarloleast-squares

Nonlinear fitting function using matlab


I need to fit the curve that you can see in the image, that comes out from a lot of Monte Carlo simulations. I've also uploaded the data to fit in a txt file. graph

I've tryied to fit the curve with a function of the type :

axexp(b(x^k))

with k<1. The results are similar to the experimental points but still far from the fitting function I need.

I've thought to split in different equations the whole range, but I haven't reached a solution yet. I.e. a straight line for the fist part and an exponential for the third. But what about the peak?

Any ideas?


Solution

  • Polynomial fitting of 8th degree:

    close all; clear all;
    
    fid = fopen('output_red.txt','r');
    Z = textscan(fid, '%f %f %f %f %f');
    fclose(fid);
    
    X = log(Z{1});
    Y = log(Z{2});
    
    p = polyfit(X, Y, 8);
    Y2 = polyval(p, X);
    
    plot(exp(X), exp(Y));
    hold on
    plot(exp(X), exp(Y2), 'r')
    legend('Original data','Fitted curve')
    
    print('-dpng','fitted.png')
    

    p contains polynomial coefficients 1.2737e-05 -9.1262e-04 2.7838e-02 -4.7160e-01 4.8482e+00 -3.0958e+01 1.1990e+02 -2.5649e+02 2.3480e+02. Using higher degrees of polynomial will result in better precision.

    Result