0
votes

Having 2 vectors vec_x and vec_y, i perform a fitting with a non-linear least squares like this :

%%myfunction.m
function F = myfun(x,vec_x)
    F = 10*(erfc((x(1)+x(2)*vec_x)/sqrt(2))/2);  
end
%%console 
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
x = lsqcurvefit(@myfun, [0 1], vec_x, vec_y,[],[],options); %here i obtain x(1) and x(2).

When i want to plot the fitting curve with the associated points(vec_x .vs vec_y), i perform it like this :

y_fit=10*erfc((x(1)+x(2)*vec_x)/sqrt(2))/2
plot(vec_x,y_fit)

The problem is that i have a weird curve not similar to the one i have automatically when i'm using the "Curv-fit App" tool of Matlab (i use the same custom function and vectors as the console).

In the Curve-fitting GUI, i got this : see the image below snapshot-curveFitting-GUI

How can i have the right plot so i can more mange the plot ?

1
Does the missing factor of 10 in y_fit affect the plot?TroyHaskin
your function requires two argumnets (x, fmpd...), don't you need to pass the second argument as part of the optimization (pass extra parameters) or freeze the fmpd_dinosaur parameter in myfun a priori...?alexandre iolov
@alexandreiolov lsqcurvefit passes vec_x to myfun as the second argument.TroyHaskin
got it, so lsqcurvefit is not like the other optimizers, which just require function with 1 argument, but both the independent and the data points ).alexandre iolov
@alexandreiolov Indeed. To quote the docs: "The lsqcurvefit function uses the same algorithm as lsqnonlin. lsqcurvefit simply provides a convenient interface for data-fitting problems."TroyHaskin

1 Answers

0
votes

I found the answer on the doc of Matlab. For plotting the curve fit from the Cftool, we should select File->Generate code. The Curve Fitting app generates code from the current session and displays the file in the MATLAB Editor. The file includes all fits and plots in the current session. We have to execute the code to have the plot.

this is the code that was generated by Matlab:

function [fitresult, gof] = createFit_MSSI_Venus(RockerArm, mos)
%CREATEFIT1(ROCKERARM,MOS)
%  Create a fit.
%
%  Data for 'Psychometric curve fitting MSSI-RockeArm 3D mesh' fit:
%      X Input : RockerArm
%      Y Output: mos
%  Output:
%      fitresult : a fit object representing the fit.
%      gof : structure with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.

%  Auto-generated by MATLAB on 08-Jan-2016 15:40:41


%% Fit: 'Psychometric curve fitting MSSI-RockeArm 3D mesh'.
[xData, yData] = prepareCurveData( RockerArm, mos );

% Set up fittype and options.
ft = fittype( 'erfc((a+b*x)/sqrt(2))/2', 'independent', 'x',  
'dependent', 'y' );

opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Levenberg-Marquardt';
opts.Display = 'Off';
opts.StartPoint = [0.655477890177557 0.171186687811562];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'Psychometric curve fitting MSSI-RockeArm 3D mesh' );
h = plot( fitresult, xData, yData);
set(h, 'Markersize',20);
legend( h, 'mos vs. RockerArm', 'Psychometric curve fitting MSSI-        
RockeArm 3D mesh', 'Location', 'NorthEast' );

% Label axes
xlabel MSSI-RockerArm 3D mesh
ylabel mos
grid on

Thanks again