Search code examples
matlabcurve-fittingdata-fittingmodel-fitting

Reproducing the "Evaluate" function from "basic fitting" GUI programmatically in Matlab


When using the "basic fitting" tool, one has the opportunity once the "fitting" done to evaluate/estimate a value at certain points. I was only able to reproduce this until the plotting part. I cannot figure out how to reproduce the "Evaluate" function programmatically so that I can estimate the value of certain points and use them in my code. The only way I can achieve this for now, is through the GUI i.e from the Figure window's main menu: "tools >> basic fitting"

screenshot

I am not sure I am making myself clear enough, but please do not hesitate to ask if you need more information.


Solution

  • The answer to your question depends on the specific type of model you are fitting. It is not clear from your question if you are just interested in polynomial fitting or something more complicated. For polynomials you can use the polyfit function to get the coefficients and the polyval function to evaluate at certain points.

    %construct a test signal
    x = linspace(0,1,100)';
    signal = 5*x.^2 + x + 0.5;
    noise = 0.1*rand(100,1);
    y = signal + noise;
    
    %Plot function
    plot(x,[signal,y]);
    
    %Polynomial fitting
    n = 2; % order of polynomial
    coeff = polyfit(x,y,n) % I get 5.0295, 0.9786, 0.5512
    
    %Evaluate at a certain set of points
    x1 = 2.3;
    polyval(coeff,x1)
    

    If you are fitting a more complicated model, then you will have to use cfit to do the fitting which will give you a fit object. You have to pass that fit object to the function feval to evaluate the function at a specific point. Check out the documentation for these functions to learn more.