Search code examples
matlabglobalfunc

Fitting method for non-linear function with experimental data


I want to fit the non-linear function (variation is T) with experimental data. In here, I used lsqcurvefit, but I don’t know the exact principle of this function, and even I don't know how to use the command func, also. (so the script I wrote is bad.)

For this, how can I modify the script below? (even w/o using lsqcurve fit, I don’t care. Chi-square, least-square, any form is okay.) Code is as follows (3 parts):

%script 1
x=coefficient1
y=coefficient2
z=coefficient3
A1=x*0.321*T
A2=(y/0.2)+0.5*T
A3=(z+0.3)*0.17/T

%script 2
global A1
global A2
global A3
Result=(A1+0.3)*A2+0.3

%script 3
global Result

Sample=readmatrix(‘experimentaldata’);
XX=sample(1,:)’;
YY=sample(2,:)’;
Xdata=linespace(min(XX),max(XX),2000);
Yadata=(interpl(XX,YY,xdata);
Fitting=lsqscurvefit(Result,T,xdata,ydata,250,2000)

Solution

  • I'm afraid if I had mistake while understanding your script (including bizarre global variables and coefficients...). But until now it seems to me that you want to fit your data into quadratic polynomial (considering T as single variable and x,y,z as constant, inserting A1 & A2 to your Result gives quadratic polynomial)

    %Result = (x*0.321*T+0.3)*((y/0.2)+0.5*T)+0.3
    Result = @(c,T)c(1)*T^2+c(2)*T^3+c(3);
    

    Here, your final equation form will be as above, c stands for your constant, and T stands for your variable(or xdata). After you clarify your equation form, you should set your initial state of constant c. In lsqcurve, as given in their explanation, they find their final answer by iterating until error(or euclidean distance) becomes smaller than given criteria. Therefore setting valid initial value will be critical here, if not answer will converge to local minimum (you can refer to John's perfect answer about this issue here, curve fitting error:nlinfit rank deficient).

    c0=[0.1, 0.2, 0.3];
    

    But in this case I'll just randomly give initial value as above.
    Afterward, answer(or c) is ready to be calculated

    Fitting=lsqscurvefit(Result,c0,xdata,ydata)
    

    Other matlab's non-linear fitting function including nlinfit have similar workflow. I think you can simply understand more detailed information about them if you take more attention to their description.

    But personally, I recommend you to use matlab's polyfit if your final equation form is in polynomial. You don't have to take care tuning your initial value then...